Apparently, all of my focus on HOW to cache is
completely different than most of yours. :-)
Specifically, it seems to me that calling a get()
operation on the cache should go GET whatever you're
looking for, and put it in the cache, right???
So, why doesn't the Cache::* API have a place to put a
function reference that gets called for a cache miss?
pseudocode follows, hopefully more clear.
<pre>
# new, empty cache here
my $cache = new Cache::Fetchit \&amp;fetch;
my $looked_it_up = $cache->fetch(TEST_KEY);
#now has one item in it.
package Cache::Fetchit;
@ISA = qw/Cache::Cache/;
sub fetch{
my ($self, $key) = @_;
my ($retval);
if (! exists $self->{_cache}{$key} ) {
my $lookup = &$fetch($key);
$self->set($key, $lookup);
$retval = $lookup;
} else {
$retval = $self->{_cache}{$key};
}
</pre>
Logged In: YES
user_id=11107
I see exactly what you are getting at here. I highly
recommend you check out Sam Tregar's excellent
IPC::SharedCache module on CPAN. He allows you to do this
with a callback routine on failed gets. Even better, he
allows a pluggable validation method that enables you define
any sort of expiration criteria you want.
Of course, you can easily do this with Cache::Cache, as
well. Simply pick your favourite implementation (say
Cache::FileCache) and overload the get method.
Send me an email and we can discuss.