Dave Rolsky

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

  • No caching

    An Alzabo::Exception::NoSuchRow exception is thrown.

  • Any syncing module

    An Alzabo::Exception::Cache::Deleted exception is thrown.

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

  • No caching

    An Alzabo::Exception::NoSuchRow exception is thrown.

  • 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

    An Alzabo::Exception::Cache::Deleted exception is thrown.

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

  • No caching

    An Alzabo::Exception::NoSuchRow exception is thrown.

  • Alzabo::ObjectCache::NullSync module is in use

    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.

  • Any other syncing module is in use

    An Alzabo::Exception::Cache::Deleted exception is thrown.

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

  • No caching

    The data from process 2's update is returned.

  • Alzabo::ObjectCache::NullSync module is in use

    The data from process 1's update is returned.

  • Any other syncing module is in use

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

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

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

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

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

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

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.

CAVEATS

This module has no upper limit on how many objects it will store. If you are operating in a persistent environment such as mod_perl, these will have a tendency to eat up memory over time.

In order to prevent processes from growing without stop, it is recommended that you call the clear method at the entry point(s) for your persistent application. This will flush the cache completely.

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::MemoryStore, Alzabo::ObjectCache::BerkeleyDBSync, Alzabo::ObjectCache::DBMSync, Alzabo::ObjectCache::IPCSync, Alzabo::ObjectCache::Sync

AUTHOR

Dave Rolsky, <autarch@urth.org>