develooper Front page | perl.module-authors | Postings from November 2005

Class::Cache - store instances of classes, eagerly vivifying them

Terrence Brannon
November 4, 2005 16:07
Class::Cache - store instances of classes, eagerly vivifying them
Message ID:
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.

Class::Cache - store instances of classes, eagerly vivifying them, and a
bit more

# 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 ;

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( 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

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.

Usage : ->refill
Purpose : recreates the objects which were returned and deleted
from cache
Returns : nothing
Argument : none
Throws : nothing

Usage : ->loaded
Purpose : shows which classes were used to make objects
Returns : a list of class names
Argument : none
Throws : nothing

None known.

Flexible object construction
It would be neat to create objects with whatever scheme they required,

my $c = Class::Cache->new(
store => [
{ 'Complex::Class' => sub { my $class=shift; $class->new(a => 12) } }

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

Email the author.

Terrence Brannon

Substantial help from mauke on


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.

perl(1). Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About