Alzabo::ObjectCache - A simple in-memory cache for row objects.
use Alzabo::ObjectCache( store => 'Alzabo::ObjectCache::MemoryStore', sync => 'Alzabo::ObjectCache::BerkeleySync', dbm_file => 'somefile.db' );
This class exists primarily to delegate necessary caching operations to other objects.
It always contains two objects. One is responsible for storing the objects to be cached. This can be done in any way that the storing object sees fit.
The syncing object is responsible for making sure that objects in multiple processes stay in sync with each other, as well as within a single process. For example, if an object in process 1 is deleted and then process 2 attempts to retrieve the same object from the database, process 2 needs to be told (in this case via an exception) that this object is no longer available. Similarly if process 1 updates the database then if there is a cached object in process 2, it needs to know that it should fetch its data again.
The easiest way to understand how the Alzabo caching system works is to outline different scenarios and show the results based on different caching configurations.
In a single process, the following sequence occurs:
- A row object is retrieved.
- The row object's delete method is called, removing the data it represents from the database.
delete
- The program attempts to call the row object's select or update method.
select
update
No caching
An Alzabo::Exception::NoSuchRow exception is thrown.
Alzabo::Exception::NoSuchRow
Any syncing module
An Alzabo::Exception::Cache::Deleted exception is thrown.
Alzabo::Exception::Cache::Deleted
Assume two process, ids 1 and 2.
- Process 1 retrieves a row object.
- Process 2 retrieves a row object for the same database row.
- Process 1 calls that object's delete method.
- Process 2 calls that object's select method.
Alzabo::ObjectCache::NullSync module is in use
If the column(s) have been previously retrieved in process 2, then that data will be returned. Otherwise, an Alzabo::Exception::NoSuchRow exception is thrown.
Any other syncing module is in use
- Process 2 calls that object's update method.
The object will attempt to update the database. This is a potential disaster if, in the meantime, another row with the same primary key has been inserted.
- Process 1 calls that object's update method.
- Process 1 calls that object's select method.
The data from process 2's update is returned.
The data from process 1's update is returned.
An Alzabo::Exception::Cache::Expired exception is thrown when process 2 attempts to update the row. If process 2 were to then attempt the update again it would succeed (as the object is updated before the exception is thrown).
Alzabo::Exception::Cache::Expired
- The row is deleted. In this case, it does not matter whether this happens through Alzabo or not.
- Process 2 inserts a new row, with the same primary key.
- Process 1 or 2 calls that object's select method.
All cases.
The correct data (from process 2's insert) is returned. This is a bit odd if process 1 called the object's delete method, but in that case it shouldn't be reusing the same object anyway.
This example may seem a bit far-fetched but is actually quite likely when using MySQL's auto_increment feature with older versions of MySQL, where numbers could be re-used.
auto_increment
The most important thing to take from this is that you should never use the Alzabo::ObjectCache::NullSync class in a multi-process situation. It is really only safe if you are sure your code will only be running in a single process at a time.
Alzabo::ObjectCache::NullSync
In all other cases, either use no caching or use one of the other syncing classes to ensure that data really is synced across multiple processes.
store => 'Alzabo::ObjectCache::StoringClass'
This should be the name of a class that implements the Alzabo::ObjectCache object storing interface.
Default is Alzabo::ObjectCache::MemoryStore.
Alzabo::ObjectCache::MemoryStore
sync => 'Alzabo::ObjectCache::SyncingClass'
This should be the name of a class that implements the Alzabo::ObjectCache object syncing interface.
Default is Alzabo::ObjectCache::NullSync.
All parameters given will be passed to the import method of the storing and syncing class being used.
A new Alzabo::ObjectCache object.
Alzabo::ObjectCache
The specified object if it is in the cache. Otherwise it returns undef.
Stores an object in the cache. This will not overwrite an existing object in the cache. To do that you must first call the delete_from_cache method.
delete_from_cache
Objects cached in this class are never expired.
This always false for this class because there is no notion of expiration for this cache.
A boolean value indicating whether or not an object has been deleted from the cache.
Tells the cache system that an object has refreshed its data from the database.
Tells the cache system that an object has updated its data in the database.
This tells the cache that the object has been removed from its external data source. This causes the cache to remove the object internally. Future calls to is_deleted for this object will now return true.
is_deleted
This method allows you to remove an object from the cache. This does not register the object as deleted. It is provided solely so that you can call store_object after calling this method and have store_object actually store the new object.
store_object
Call this method to completely clear the cache.
The interface that any object storing module needs to implement is as follows:
A new object.
Stores an object in the cache but should not overwrite an existing object.
This method deletes an object from the cache.
Completely clears the cache.
Any class that implements the syncing interface should inherit from Alzabo::ObjectCache::Sync. This class provides most of the functionality necessary to handle syncing operations.
Alzabo::ObjectCache::Sync
This method will be called when the object is first created.
Clears the process-local sync times (not the times shared between processes).
Returns the time that the object matching the given id was last refreshed.
This is called to update the state of the syncing object in regards to a particularl object. The first parameter is the object's id. The second is the time that the object was last refreshed. The third parameter, which is optional, tells the syncing object whether or not to preserve an existing time for the object if it already has one.
Alzabo::ObjectCache::Store::Memory, Alzabo::ObjectCache::Store::BerkeleyDB, Alzabo::ObjectCache::Store::Null, Alzabo::ObjectCache::Sync::BerkeleyDB, Alzabo::ObjectCache::Sync::SDBM_File, Alzabo::ObjectCache::Sync::DB_File, Alzabo::ObjectCache::Sync::IPC, Alzabo::ObjectCache::Sync::Null, Alzabo::ObjectCache::Store, Alzabo::ObjectCache::Sync, Alzabo::ObjectCache::Sync::DBM
Dave Rolsky, <autarch@urth.org>
To install Alzabo, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Alzabo
CPAN shell
perl -MCPAN -e shell install Alzabo
For more information on module installation, please visit the detailed CPAN module installation guide.