OpenInteract::Cache -- Caches objects to avoid database hits and content to avoid template processing


 # In $WEBSITE_DIR/conf/server.ini

 [cache_info data]
 default_expire = 600
 use            = 0
 use_spops      = 0
 class          = OpenInteract::Cache::File
 max_size       = 2000000

 # Use implicitly with built-in content caching

 sub listing {
     my ( $class, $p ) = @_;
     my %params = ( cache_key  => 'mypkg::myhandler::listing',
                    cache_time => 1800 );
     return $R->template->handler(
               {}, \%params, { name => 'mypkg::listing' } );

 # Explicitly expire a cached item

 sub edit {
     my ( $class, $p ) = @_;
     eval { $object->save };
     if ( $@ ) {
         # set error message
     else {
         $R->cache->clear({ key => 'mypkg::myhandler::listing' });


This class is the base class for different caching implementations, which are themselves just wrappers around various CPAN modules which do the actual work. As a result, the module is pretty simple.

The only tricky aspect is that we use this for caching content and for caching SPOPS objects. So there is some additional data checking not normally in such a module.


These are the methods for the cache. The following parameters are passed to every method that operates on an individual cached item. Either 'key' or 'class' and 'object_id' are required for these methods.

  • key: Name under which we store data

  • class: Class of SPOPS object

  • object_id: ID of SPOPS object

get( \%params )

Returns the data in the cache associated with a key; undef if data corresponding to the key is not found.

set( \%params )

Saves the data found in the data parameter into the cache, referenced by the key key. If data is an SPOPS object we create a key from its class and ID.


  • data: The data to save in the cache. This can be an SPOPS object or HTML content.

  • expire (optional): Time the item should sit in the cache before being refreshed. This can be in seconds (the default) or in the "[number] [unit]" format outlined by Cache::Cache. For example, '10 minutes'.

Returns a true value if successful.

clear( \%params )

Invalidates the cache for the specified item.


Clears the cache of all items.


These are the methods that must be overridden by a subclass to implement caching.

initialize( \%OpenInteract::Config )

This method is called object is first created. Use it to define and return the object that actually does the caching. It will be passed to all successive methods (get_data(), set_data(), etc.).

Relevant keys in the OpenInteract::Config object passed in:

 cache_info->default_expire - Default expiration time for items
 cache_info->max_size       - Maximum size (in bytes) of cache
 dir->cache_content         - Root directory for content cache

get_data( $cache_object, $key )

Returns an object if it is cached and 'fresh', however that implementation defines fresh.

set_data( $cache_object, $data, $key, [ $expires ] )

Returns 1 if successful, undef on failure. If $expires is undefined or is not set to a valid Cache::Cache value, then the configuration key 'cache_info.default_expire'.

clear_data( $cache_object, $key )

Removes the specified data from the cache. Returns 1 if successful, undef on failure (or inability to do so).

purge_all( $cache_object )

Clears the cache of all items.


Test and get working!


None known.


Copyright (c) 2001-2002, inc.. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Chris Winters <>