May 06

Memoization is a technique for optimizing a function over repeated calls. When you call the function, the return value is cached (based on the arguments passed) before being returned to you. Next time you call the function with the same arguments, you’ll get the value back immediately.

Memoize is the standard Perl memoization solution and after twelve+ years still works well in the common case. However, since Perl caching support has come a long way, and memoization is just a specific form of caching, I wanted to try pairing memoization with modern cache features. Hence, CHI::Memoize.

Here are some of the nice features that came out of this:

  • The ability to cache to any of CHI’s other backends. e.g.
        memoize( 'func', driver => 'File', root_dir => '/path/to/cache' );
        memoize( 'func', driver => 'Memcached', servers => [""] );
  • The ability to expire memoized values based on time or a condition. e.g.

        memoize( 'func', expires_in => '1h' );
        memoize( 'func', expire_if => sub { ... } );
  • A better key normalizer. Memoize just joins the keys into a string, which doesn’t work for references/undef and can generate multiple keys for the same hash. In contrast, C relies on CHI’s automatic serialization of non-scalar keys. So these will be memoized together:

        memoized_function( a => 5, b => 6, c => { d => 7, e => 8 } );
        memoized_function( b => 6, c => { e => 8, d => 7 }, a => 5 );

    and it’s easy to specify your own key, e.g. memoize on just the second and third arguments:

        memoize( 'func', key => sub { $_[1], $_[2] } );

Subsets of these features were already available in separate distributions, but not all in one place.

Now that this available I’m curious to see where I use it in place of the traditional get-and-set cache pattern.

2 Responses to “Memoize: Revisiting a twelve year old API”

  1. Ivan Wills Says:

    have you considered an alternate interface such as attributes.
    sub myfunc :Memoize {


  2. Jonathan Swartz Says:

    I had not, mostly because I consider subroutine attributes a little wonky and haven’t developed with them. But it is a particularly nice looking interface, at least for the simple case.

Leave a Reply

preload preload preload