Home > Plack-Middleware-CacheLite

Plack-Middleware-CacheLite

Plack-Middleware-CacheLite is a project mainly written in Perl, it's free.

[DEPRECATED]

=head1 NAME

Plack::Middleware::CacheLite - Stand alone implementation of cache

=head1 SYNOPSIS

use Mojolicious::Plugin::CacheLite;

builder {
    enable "CacheLite", {
        max_keys  => 100,    
        threshold => 0.08,   # second
        keygen => sub {
            my $env = shift;

            # generate key here maybe with $env
            # return undef causes cache disable

            return $key;
        },
    }
    $app;
};

=head1 DESCRIPTION

Plack::Middleware::CacheLite is a stand alone cache middleware for plack

This plugin caches whole response into key-value object and returns it for next request instead of invoking app. You can specify the cache key by giving code reference which gets env for argument.

You can also specify one or more expiration conditions for each cache key from anywhere in your app by giving code references. In many case, a single page output involves not only one data model and each of the models may should have own cache expiration conditions. To expire a cache exactly right timing, the cache itself must know when to expire. The feature of this class provides the mechanism.

=head1 OPTIONS

=head2 keygen => code reference [optional]

Key generator for cache entries. This must be given in code reference. The following is the default.

$self->plugin(cache_lite => {
    keygen => sub {
        $env = shift;
        if (uc $env->{REQUEST_METHOD} eq 'GET') {
            return $env->{REQUEST_URI} || $env->{PATH_INFO}
        }
    }
});

returning undef causes both cache generation and reference disabled.

=head2 max_keys => number [optional]

Maximum number of cache keys, defaults to 100.

$self->plugin(cache_lite => {max_keys => 100});

=head2 threshold => number [optional]

Threshold time interval for page generation to activate cache generation. You can give it a floating number of second. This plugin measures how long the page generation spent and compares to the threshold.

$self->plugin(cache_lite => {threshold => 0.8});

=head1 METHODS

=head2 Plack::Middleware::CacheLite->set_expire($code_ref)

This appends a code reference for cache expiration control.

package Model::NewsRelease;

my $sqlite_file = 'news_release.sqlite';

sub list {
    ...

    Plack::Middleware::CacheLite->set_expire(sub {
        my $cache_timestamp = shift;
        return $cache_timestamp - (stat($sqlite_file))[9] > 0;
    });
    ...
}

=head2 call

Internal use.

=head2 prepare_app

Internal use.

=head1 AUTHOR

Sugama, E[email protected]

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2011 by jamadam.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=cut