Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > manually expire an element in Memoize

Reply
Thread Tools

manually expire an element in Memoize

 
 
danielmcbrearty@gmail.com
Guest
Posts: n/a
 
      02-14-2007
hi all

I like and use MJ Dominus' excellent Memoize module to cache some
stuff in my app that hits a db rather hard. It's very simple :


use Memoize;

memoize('my_accessor');

sub my_accessor {
my ($key1, $key2) = @_;
# look up $key1, $key2 in the db and return a scalar
}

and the function is magically cached without the need to do any more.

Now what I'd like is, in another function, to manually invalidate (or
delete) certain cached values at runtime (most likely because I
changed the value in the db).

How can I do this? I looked at the docs and source for
Memoize::Expire, but it's not too clear to me right now. Obviously,
I'd like the simplest possible solution ...

thanks

Daniel

 
Reply With Quote
 
 
 
 
gf
Guest
Posts: n/a
 
      02-15-2007
On Feb 14, 3:41 pm, (E-Mail Removed) wrote:
> hi all
>
> I like and use MJ Dominus' excellent Memoize module to cache some
> stuff in my app that hits a db rather hard. It's very simple :
>
> use Memoize;
>
> memoize('my_accessor');
>
> sub my_accessor {
> my ($key1, $key2) = @_;
> # look up $key1, $key2 in the db and return a scalar
>
> }
>
> and the function is magically cached without the need to do any more.
>
> Now what I'd like is, in another function, to manually invalidate (or
> delete) certain cached values at runtime (most likely because I
> changed the value in the db).
>
> How can I do this? I looked at the docs and source for
> Memoize::Expire, but it's not too clear to me right now. Obviously,
> I'd like the simplest possible solution ...


Memoize is cool, but if you need more flexibility or other features
then rolling a simple version isn't hard.

Wrap your function in an extra set of braces kinda like this ...

{
my %memo_cache;
sub blah {
return $memo_cache{@_} if $memo_cache{@_};
...
my $return_value =
do_some_calculating_to_create_a_return_value...
...
$memo_cache{@_} = $return_value;
return $return_value;
}
}


If you need to be able expire an element in your cache then you add an
additional sub inside the outer braces...

{
my %memo_cache;

sub drop_cache_value {
delete $memo_cache{@_};
}

sub blah {
return $memo_cache{@_} if $memo_cache{@_};
...
my $return_value =
do_some_calculating_to_create_a_return_value...
...
$memo_cache{@_} = $return_value;
return $return_value;
}

}

And call the drop_cache_value() with the same values used to initially
create the memoized entry when you determine that that element in the
hash needs to be deleted.

The downside of doing it yourself is you might not handle conditions
that Memoize or a similar module already handles.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cookies expire immediately, not when set to expire Tongass Park Neighborhood Association, Juneau Alaska ASP General 2 11-24-2009 08:24 PM
problem using both Memoize::Expire and DB_File samwyse Perl Misc 2 07-21-2005 03:11 AM
How to Expire an Authenticatoin Ticket Manually Ali ASP .Net 7 01-29-2004 01:50 PM
How to Expire an Authenticatoin Ticket Manually Ali ASP .Net Mobile 0 01-28-2004 10:48 PM
How to memoize functions? Chris Reedy Python 3 06-27-2003 02:18 PM



Advertisments