The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

OpenInteract::Cache -- caches objects so we do not need to do a database fetch each time

SYNOPSIS

use OpenInteract::Cache;
use OpenInteract::Object;

my $obj = OpenInteract::Object->new();
$obj->id( 35 );
$obj->name( 'Superior Tecnologies, Inc.' );
$obj->save();

my $cache = OpenInteract::Cache::CacheType->new();
$cache->set( $obj );

...(later)...
my $class = 'OpenInteract::Object';
my $obj = $cache->get( $class, 35 ) || $class->fetch( 35 );

DESCRIPTION

The original purpose of this class is to be a generic holder for various types of objects within the SPOPS (formerly Collection) framework. However, we will be extending it to cache any kind of data, given a key and a chunk of data. That data can be some HTML or a hashref of theme values.

This class is meant to have a simple interface and is really only a wrapper around a functional caching module. These implementations are found in the subclasses.

METHODS

These are the methods for the cache:

get( \%params )

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

Parameters:

If you want to retrieve an object with a particular ID, use:

  • class: Class of object

  • id: ID of object

This module will create a consistent key from these two items.

Otherwise, use:

  • key: Key for data to retrieve

set( \%params )

Saves the data found in the {data} parameter into the cache, referenced by the key {key} or by a key built from metadata if the item in {data} is an object. Returns a true value if successful.

Note that your object may define the methods pre_cache_save and post_cache_save which can act as callbacks during the caching process. Failure to return a true value from either of these callbacks will result in the data not being cached.

clear( [ $obj ] )

If given the request object and a collection object, clears the cache of that object. If not, clears the cache of all objects.

Note that your object may define the methods pre_cache_remove and post_cache_remove which can act as callbacks when the object is removed from the cache.

SUBCLASS METHODS

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

_get_data( $key )

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

_set_data( $data, $key, [ $expires ] )

Returns 1 if successful, undef on failure.

_clear_data( $key )

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

You may also provide the following:

class_initialize( \%params )

This method is called once when the class is first initialized (currently via a mod_perl ChildInitHandler). Generally used to define something (package variables, for instance) that can live throughout the life of the class.

The hashref passed in for parameters typically contains just the config object (using the key 'config'); but you can also pass implementation-specific information to the functional module this way.

initialize( \%params )

The cache object is held in the 'Stash Class' between requests, so it does not need to be recreated every time. The initialize() procedure is only called after the cache object is first created.

size_objects()

Returns the number of objects currently in the cache; undef if not implemented.

size_bytes()

Returns the size in bytes of the objects currently in the cache; undef if not implemented.

TODO

Test and get working!

BUGS

Caching and development mode

Do not use caching while you are in development mode. Old, incorrect versions of objects will inevitably get cached and mess you up.

COPYRIGHT

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

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

AUTHORS

Chris Winters <chris@cwinters.com>