Home > Class-FileCacheable

Class-FileCacheable

Class-FileCacheable is a project mainly written in Perl, it's free.

[DEPRECATED] Make your methods cacheable easily

=head1 NAME

Class::FileCacheable - DEVELOPING

=head1 SYNOPSIS

use base 'Class::FileCacheable';

sub file_cache_expire {
    my ($self, $timestamp) = @_;
    if (some_condifion) {
        return 1;
    }
}

sub file_cache_options {
    return {
        'namespace' => 'MyNamespace',
        'cache_root' => 't/cache',
        #...
    };
}

sub some_sub1 : FileCacheable {

    my $self = shift;
}

sub some_sub2 : FileCacheable(&expire_code_ref) {

    my $self = shift;
}

=head1 DESCRIPTION

This module defines an attribute "FileCacheable" which redefines your functions cacheable. This module depends on L for managing cache files.

To use this, do following steps.

=item use base 'Class::FileCacheable';

=item override the method I

=item override the method I

=item define your subs as follows

sub your_sub : FileCacheable {
    my $self = shift;
    # do something
}

That's it.

=head1 METHODS

=head2 file_cache_expire

This is a callback method for specifying the condition for cache expiretion. Your module can override the method if necessary.

file_cache_exipre will be called as instance method when the target method called. This method takes timestamp of the cache as argument.

sub file_cache_expire {
    my ($self, $timestamp) = @_;
    if (some_condifion) {
        return 1;
    }
}

file_cache_exipre should return 1 or 0. 1 causes the cache EXPIRED

=head2 file_cache_options

This is a callback method for specifying L options. Your module can override the method if necessary.

sub file_cache_options {
    return {
        'namespace' => 'Test',
        'cache_root' => 't/cache',
    };
}

In addition to L options, you can set extra options bellow

=item number_cache_id

This takes 1 or 0 for value. '1' causes the cache ids automatically numbered so the caches doesn't affect in single process. This is useful if you want to cache the function calls as a sequence.

=head2 file_cache_purge

Not implemented yet

=head1 EXAMPLE

package GetExampleDotCom;
use strict;
use warnings;
use base 'Class::FileCacheable';
use LWP::Simple;

    sub new {
        my ($class, $url) = @_;
        return bless {url => $url}, $class;
    }

    sub get_url : FileCacheable {
        my $self = shift;
        return LWP::Simple::get($self->{url});
    }

    sub file_cache_expire {
        my ($self, $timestamp) = @_;
        if (time() - $timestamp > 86400) {
            return 1;
        }
    }

    sub file_cache_options {
        my $self = shift;
        return {
            namespace => 'Test',
            cache_root => 't/cache',
            default_key => $self->{url},
        };
    }

=head1 SEE ALSO

L

=head1 AUTHOR

Sugama Keita, Elt>[email protected]<gt

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2011 by Sugama Keita.

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

Previous:ZeldaLike