manually expire an element in Memoize

Discussion in 'Perl Misc' started by danielmcbrearty@gmail.com, Feb 14, 2007.

  1. Guest

    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
    , Feb 14, 2007
    #1
    1. Advertising

  2. gf Guest

    On Feb 14, 3:41 pm, 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.
    gf, Feb 15, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ali
    Replies:
    7
    Views:
    420
  2. Chris Reedy

    How to memoize functions?

    Chris Reedy, Jun 26, 2003, in forum: Python
    Replies:
    3
    Views:
    989
    Chris Reedy
    Jun 27, 2003
  3. Ali
    Replies:
    0
    Views:
    107
  4. Tongass Park Neighborhood Association, Juneau Alas

    Cookies expire immediately, not when set to expire

    Tongass Park Neighborhood Association, Juneau Alas, Oct 1, 2009, in forum: ASP General
    Replies:
    2
    Views:
    1,188
    SQLDude
    Nov 24, 2009
  5. samwyse
    Replies:
    2
    Views:
    92
Loading...

Share This Page