CHI-Memoize

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


        If the coderef returns `CHI::Memoize::NO_MEMOIZE' (or `NO_MEMOIZE'
        if you import it), this call won't be memoized. This is useful if
        you have a cache of limited size or if you know certain arguments
        will yield nondeterministic results. e.g.

            memoize('func', key => sub { $is_worth_caching ? @_ : NO_MEMOIZE });

    set and get options
        You can pass any of CHI's set options (e.g. expires_in,
        expires_variance) or get options (e.g. expire_if, busy_lock). e.g.

            # Expire after one hour
            memoize('func', expires_in => '1h');
    
            # Expire when a particular condition occurs
            memoize('func', expire_if => sub { ... });

    cache options
        Any remaining options will be passed to the CHI constructor to
        generate the cache:

lib/CHI/Memoize.pm  view on Meta::CPAN

use base qw(Exporter);

my $no_memoize = {};
sub NO_MEMOIZE { $no_memoize }

our @EXPORT      = qw(memoize);
our @EXPORT_OK   = qw(memoize memoized unmemoize NO_MEMOIZE);
our %EXPORT_TAGS = ( all => \@EXPORT_OK );

my %memoized;
my @get_set_options = qw( busy_lock expire_if expires_at expires_in expires_variance );
my %is_get_set_option = map { ( $_, 1 ) } @get_set_options;

sub memoize {
    my ( $func, %options ) = @_;

    my ( $func_name, $func_ref, $func_id ) = _parse_func_arg( $func, scalar(caller) );
    croak "'$func_id' is already memoized" if exists( $memoized{$func_id} );

    my $passed_key      = delete( $options{key} );
    my $cache           = delete( $options{cache} );

lib/CHI/Memoize.pm  view on Meta::CPAN

limited size or if you know certain arguments will yield nondeterministic
results. e.g.

    memoize('func', key => sub { $is_worth_caching ? @_ : NO_MEMOIZE });

=item set and get options

You can pass any of CHI's L<set|CHI/set> options (e.g.
L<expires_in|CHI/expires_in>, L<expires_variance|CHI/expires_variance>) or
L<get|CHI/get> options (e.g. L<expire_if|CHI/expire_if>,
L<busy_lock|CHI/busy_lock>). e.g.

    # Expire after one hour
    memoize('func', expires_in => '1h');
    
    # Expire when a particular condition occurs
    memoize('func', expire_if => sub { ... });

=item cache options

Any remaining options will be passed to the L<CHI constructor|CHI/CONSTRUCTOR>



( run in 0.275 second using v1.01-cache-2.11-cpan-87723dcf8b7 )