File Coverage

blib/lib/Class/DBI/Cacheable.pm
Criterion Covered Total %
statement 12 32 37.5
branch 0 10 0.0
condition n/a
subroutine 4 7 57.1
pod 3 3 100.0
total 19 52 36.5


line stmt bran cond sub pod time code
1             package Class::DBI::Cacheable;
2              
3 1     1   634 use strict;
  1         2  
  1         26  
4 1     1   4 use warnings;
  1         2  
  1         24  
5 1     1   4 use base qw( Class::DBI::ObjectCache Class::DBI );
  1         4  
  1         610  
6 1     1   75457 use CLASS;
  1         3  
  1         8  
7              
8             our $VERSION = 0.03;
9              
10             =head1 NAME
11              
12             Class::DBI::Cacheable - Class::DBI object cache framework
13              
14             =head1 SYNOPSIS
15              
16             package YourApp::DB;
17             use base 'Class::DBI::Cacheable';
18             __PACKAGE__->set_db( Main => 'dbi:Pg:dbname=database', 'username', 'password' );
19              
20             =head1 DESCRIPTION
21              
22             Class::DBI::Cacheable transparently acts as a cacheing wrapper around L, storing
23             retrieved and created data in a local object cache, and returning data out of the cache
24             wherever possible.
25              
26             Intended for better performance of L-based applications, this can prevent unnecessary
27             database queries by using previously-retrieved object data rather than having to go to the
28             database server every time a object is retrieved.
29              
30             It is highly configurable so you can customize both on an per-application and per-class basis
31             the directory root where objects are stored, expire times, and other important parameters.
32              
33             =head1 Method Reference
34              
35             =cut
36              
37             =head2 CLASS->retrieve( [args] )
38              
39             This method overrides the C method of L, and adds
40             caching capabilities. It first constructs a cache key from the supplied
41             arguments, and tries to retrieve that object from the data store. If a
42             valid object is returned, that is given to the caller and the entire
43             L->retrieve method is bypassed.
44              
45             However, in the event the object does not exist in the cache, Class::DBI
46             is used to retrieve the object.
47              
48             =cut
49              
50             sub retrieve {
51 0     0 1   my $class = shift;
52              
53 0           my $key = $class->getCacheKey(\@_);
54 0 0         if ($class->can('getCache')) {
55 0           my $obj = $class->getCache($key);
56 0 0         return $obj if (defined($obj));
57             }
58              
59 0           return $class->SUPER::retrieve(@_);
60             }
61              
62             =head2 CLASS->construct(data)
63              
64             This method overrides the C method of L, which is responsible
65             for constructing an object from searched or otherwise retrieved database data. This
66             method circumvents this system to try and retrieve a cached object first. Next, the
67             real C method is called, after which this data is then stored in the cache.
68              
69             =cut
70              
71             sub construct {
72 0     0 1   my $class = shift;
73 0           my $data = shift;
74              
75 0 0         if ($class->can('getCache')) {
76 0           my $obj = $class->getCache($data);
77 0 0         if (defined($obj)) {
78 0           return $obj;
79             }
80             }
81              
82 0           my $obj = $class->SUPER::construct($data);
83 0 0         $obj->setCache() if ($obj->can('setCache'));
84 0           return $obj;
85             }
86              
87             =head2 CLASS->update([args])
88              
89             This simple wrapper around L's C method simply passes the
90             update action on to L, after which the object is refreshed in the
91             cache. This ensures that, if database data is altered, the cache will always
92             accurately reflect the database contents.
93              
94             Note: this will only work properly when updates are made through L.
95             If changes are made to the database via direct SQL calls the cache will be out-of-sync
96             with the real database.
97              
98             =cut
99              
100             sub update {
101 0     0 1   my $self = shift;
102 0           my $key = $self->getCacheKey;
103 0           my $result = $self->SUPER::update(@_);
104 0           $self->setCache($key);
105 0           return $result;
106             }
107              
108             =head1 TIPS AND USAGE NOTES
109              
110             Most customization for this package is possible by overriding the class
111             methods exposed by L, so visit the documentation
112             for that class to see what all can be changed and customized.
113              
114             =head2 USE A DIFFERENT CACHE_ROOT FOR EACH APPLICATION
115              
116             By overriding C in the base class used to connect to your
117             database, you can indicate a separate cache directory for each of your
118             database connections. In this way, if you need to perform debugging,
119             you are changing database contents outside of your framework, or you
120             simply are not certain if you have some old and tainted data in the
121             cache, you can remove the entire directory structure to start from a
122             clean slate.
123              
124             =head2 OVERRIDE EXPIRES FOR YOUR OBJECTS
125              
126             If you have data that can change often, override the C class
127             method in your perl modules. Anything within your framework, if your
128             base class inherits from this module, can override C. In fact,
129             by putting logic inside the C method for one of your classes,
130             you can return different expirey times depending on the values stored
131             in the object.
132              
133             =head1 SEE ALSO
134              
135             L, L, L
136              
137             =head1 AUTHOR
138              
139             Michael A Nachbaur, Emike@nachbaur.comE
140              
141             =head1 COPYRIGHT AND LICENSE
142              
143             This library is free software; you can redistribute it and/or modify
144             it under the same terms as Perl itself.
145              
146             =cut
147              
148             1;