Class::DBI::Cacheable - Class::DBI object cache framework
package YourApp::DB; use base 'Class::DBI::Cacheable'; __PACKAGE__->set_db( Main => 'dbi:Pg:dbname=database', 'username', 'password' );
Class::DBI::Cacheable transparently acts as a cacheing wrapper around Class::DBI, storing retrieved and created data in a local object cache, and returning data out of the cache wherever possible.
Intended for better performance of Class::DBI-based applications, this can prevent unnecessary database queries by using previously-retrieved object data rather than having to go to the database server every time a object is retrieved.
It is highly configurable so you can customize both on an per-application and per-class basis the directory root where objects are stored, expire times, and other important parameters.
This method overrides the
retrieve() method of Class::DBI, and adds caching capabilities. It first constructs a cache key from the supplied arguments, and tries to retrieve that object from the data store. If a valid object is returned, that is given to the caller and the entire Class::DBI->retrieve method is bypassed.
However, in the event the object does not exist in the cache, Class::DBI is used to retrieve the object.
This method overrides the
construct() method of Class::DBI, which is responsible for constructing an object from searched or otherwise retrieved database data. This method circumvents this system to try and retrieve a cached object first. Next, the real
construct() method is called, after which this data is then stored in the cache.
This simple wrapper around Class::DBI's
update() method simply passes the update action on to Class::DBI, after which the object is refreshed in the cache. This ensures that, if database data is altered, the cache will always accurately reflect the database contents.
Note: this will only work properly when updates are made through Class::DBI::Cachable. If changes are made to the database via direct SQL calls the cache will be out-of-sync with the real database.
Most customization for this package is possible by overriding the class methods exposed by Class::DBI::ObjectCache, so visit the documentation for that class to see what all can be changed and customized.
CACHE_ROOT in the base class used to connect to your database, you can indicate a separate cache directory for each of your database connections. In this way, if you need to perform debugging, you are changing database contents outside of your framework, or you simply are not certain if you have some old and tainted data in the cache, you can remove the entire directory structure to start from a clean slate.
If you have data that can change often, override the
EXPIRES class method in your perl modules. Anything within your framework, if your base class inherits from this module, can override
EXPIRES. In fact, by putting logic inside the
EXPIRES method for one of your classes, you can return different expirey times depending on the values stored in the object.
Michael A Nachbaur, <email@example.com>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.