I wrote this in a huge hurry today at work. I'm wondering if there is
some Cache module that has hooks or API that I could've used instead of
writing this from scratch. Any other feedback is welcome as well.
NAME
Class::Cache - store instances of classes, eagerly vivifying them, and a
bit more
SYNOPSIS
# call $module->new and store instance in a Class::Cache
my $c = Class::Cache->new(
store => [qw(nn26::images html::toolbar html::pulldown)]
) ;
# get the object already created by nn26::images->new
# the cache entry for nn26::images is deleted
# and not available until refill is called
my $tree = $c->get('nn26::images');
my $try2 = $c->get('nn26::images'); # EXCEPTION
my $pull = $c->get('html::pulldown');
# rebuild the entries for retrieved keys
$c->refill ;
DESCRIPTION
In mod_perl, one wants to pre-load as many things as possible. However,
the objects created from the classes that I was loading can only be used
once, after which they have to be recreated. For efficiency, I do not
want the recreation to occur until explicitly requested.
USAGE
new
Usage : ->new( store => [qw(module::1 module:: module::3)] )
Purpose : creates a new class cache by calling new on each module
Returns : returns a Class::Cache object
Argument : store
Throws : Exceptions and other anomolies: none
get
Usage : ->get($class_name)
Purpose : returns the class instance. no longer available until
the next time refill() is called. This behavior should
be modifiable and will be in later versions of this
Returns : the pre-created instance
Argument : $class_name
Throws : Exception if class instance doesnt exist. Note that an
instance can not exist for 2 reasons. It never existed
or it was retrieved.
refill
Usage : ->refill
Purpose : recreates the objects which were returned and deleted
from cache
Returns : nothing
Argument : none
Throws : nothing
loaded
Usage : ->loaded
Purpose : shows which classes were used to make objects
Returns : a list of class names
Argument : none
Throws : nothing
BUGS
None known.
TODO
Flexible object construction
It would be neat to create objects with whatever scheme they required,
e.g:
my $c = Class::Cache->new(
store => [
'Simple::Class',
{ 'Complex::Class' => sub { my $class=shift; $class->new(a => 12) } }
'Simple::Class2'
]
);
Timing of object recreation
Right now, once a created object is returned, it is removed from the
cache and is no longer available. There are two other possible scenarios
once an object is returned:
* refill the slot immediately in a separate thread
* to build the new value before returning the old value
SUPPORT
Email the author.
AUTHOR
Terrence Brannon
CPAN ID: TBONE
metaperl.com
[EMAIL PROTECTED]
http://www.metaperl.com
Substantial help from mauke on
irc://irc.efnet.org
COPYRIGHT
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included
with this module.
SEE ALSO
perl(1).