Home | Wiki | OI 1.x Docs | OI 2.x Docs |
OpenInteract2::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 = OpenInteract2::Cache::File max_size = 2000000 # Use implicitly with built-in content caching sub listing { my ( $self ) = @_; return $self->generate_content( \%params, { name => 'mypkg::listing' } ); } # Explicitly expire a cached item sub edit { my ( $self ) = @_; ... eval { $object->save }; if ( $@ ) { # set error message } else { CTX->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.
Parameters:
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.
purge()
Clears the cache of all items.
These are the methods that must be overridden by a subclass to implement caching.
initialize( \%OpenInteract2::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 OpenInteract2::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.
Nothing known.
None known.
Copyright (c) 2001-2003 Chris Winters. 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 <chris@cwinters.com>
Generated from the OpenInteract 1.99_03 source.