Alzabo::ObjectCache - A simple in-memory cache for row objects.


NAME

Alzabo::ObjectCache - A simple in-memory cache for row objects.


SYNOPSIS

  use Alzabo::ObjectCache( store => 'Alzabo::ObjectCache::MemoryStore',
                           sync  => 'Alzabo::ObjectCache::BerkeleySync',
                           dbm_file => 'somefile.db' );


DESCRIPTION

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.


CACHING SCENARIOS

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.

Scenario 1 - Single process - delete followed by select/update

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.

- The program attempts to call the row object's select or update method.

Results

Scenario 2 - Multiple processes - delete followed by select

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.

Results

Scenario 3 - Multiple processes - delete followed by update

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 update method.

Results

Scenario 4 - Multiple processes - update followed by update

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 update method.

- Process 2 calls that object's update method.

- Process 1 calls that object's select method.

Results

Scenario 5 - Multiple processes - delete followed by insert (same primary key)

Assume two process, ids 1 and 2.

- Process 1 retrieves a row object.

- 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.

Results

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.

Summary

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.

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.


METHODS

import

Parameters

All parameters given will be passed to the import method of the storing and syncing class being used.

new

Returns

A new Alzabo::ObjectCache object.

fetch_object ($id)

Returns

The specified object if it is in the cache. Otherwise it returns undef.

store_object ($object)

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.

is_expired ($object)

Objects cached in this class are never expired.

Returns

This always false for this class because there is no notion of expiration for this cache.

is_deleted ($object)

Returns

A boolean value indicating whether or not an object has been deleted from the cache.

register_refresh ($object)

Tells the cache system that an object has refreshed its data from the database.

register_change ($object)

Tells the cache system that an object has updated its data in the database.

register_delete ($object)

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.

delete_from_cache ($object)

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.

clear

Call this method to completely clear the cache.


STORING INTERFACE

The interface that any object storing module needs to implement is as follows:

new

Returns

A new object.

fetch_object ($id)

Returns

The specified object if it is in the cache. Otherwise it returns undef.

store_object ($object)

Stores an object in the cache but should not overwrite an existing object.

delete_from_cache ($object)

This method deletes an object from the cache.

clear

Completely clears the cache.


SYNCING INTERFACE

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.

The interface that any object storing module needs to implement is as follows:

_init

This method will be called when the object is first created.

clear

Clears the process-local sync times (not the times shared between processes).

sync_time ($id)

Returns

Returns the time that the object matching the given id was last refreshed.

update ($id, $time, $overwrite)

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.


SEE ALSO

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


AUTHOR

Dave Rolsky, <autarch@urth.org>

 Alzabo::ObjectCache - A simple in-memory cache for row objects.