Class::FileCacheable::Lite - Make you methods cacheable easily

TABLE OF CONTENTS

NAME

Class::FileCacheable::Lite - Make you methods cacheable easily

SYNOPSIS

    use base 'Class::FileCacheable::Lite';
    
    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({key => $key, expire => \&expire_code_ref}) {
        
        my $self = shift;
    }

DESCRIPTION

This module defines an attribute "FileCacheable" which redefines your functions cacheable.

To use this, do following steps.

use base 'Class::FileCacheable';
override the method file_cache_expire
override the method file_cache_option
define your subs as follows
    sub your_sub : FileCacheable {
        my $self = shift;
        # do something
    }

That's it.

METHODS

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*

file_cache_options

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

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

you can set options bellow

cache_root

The location in the filesystem that will hold the root of the cache. Defaults to the 'FileCache' under the OS default temp directory ( often '/tmp' on UNIXes ) unless explicitly set.

cache_depth

The number of subdirectories deep to cache object item. This should be large enough that no cache directory has more than a few hundred objects. Defaults to 3 unless explicitly set.

directory_umask

The directories in the cache on the filesystem should be globally writable to allow for multiple users. While this is a potential security concern, the actual cache entries are written with the user's umask, thus reducing the risk of cache poisoning. If you desire it to only be user writable, set the 'directory_umask' option to '077' or similar. Defaults to '000' unless explicitly set.

namespace

The namespace associated with this cache. Defaults to "Default" if not explicitly set.

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.

file_cache_purge

Not implemented yet

EXAMPLE

    package GetExampleDotCom;
    use strict;
    use warnings;
    use base 'Class::FileCacheable::Lite';
    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},
            };
        }

SEE ALSO

Class::FileCacheable

AUTHOR

Sugama Keita, <sugama@jamadam.com>

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.