summary refs log tree commit diff
path: root/lib/MooseX/UserAgent/Cache.pm
blob: b5fff188c8166f75b5af51b79a38455a5cbdac7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package MooseX::UserAgent::Cache;

use Moose::Role;
use Cache::FileCache;

has 'ua_cache' => (
    is      => 'rw',
    isa     => 'Object',
    lazy    => 1,
    default => sub {
        my $self = shift;
        Cache::FileCache->new(
            {
                cache_root => $self->useragent_conf->{cache}->{root},
                default_expires_in =>
                    $self->useragent_conf->{cache}->{expires},
                namespace => $self->useragent_conf->{cache}->{namespace}
            }
        );
    }
);

sub get_ua_cache {
    my ( $self, $url ) = @_;
    if ( $self->useragent_conf->{cache}->{use_cache} ) {
        my $ref = $self->ua_cache->get($url);
        if ( defined $ref && $ref->{LastModified} ne '' ) {
            return $ref->{LastModified};
        }
    }
}

sub store_ua_cache {
    my ( $self, $url, $res ) = @_;
    if ( $self->useragent_conf->{ cache }->{ use_cache } ) {
        $self->ua_cache->set(
            $url,
            {   ETag         => $res->header( 'Etag' )          || '',
                LastModified => $res->header( 'Last-Modified' ) || ''
            }
        );
    }
}

1;

__END__

=head1 NAME

RTGI::Role::UserAgent::Cache

=head1 DESCRIPTION

=head1 BUGS AND LIMITATIONS

=head1 AUTHOR

franck cuny  C<< <franck.cuny@rtgi.fr> >>

=head1 LICENCE AND COPYRIGHT

Copyright (c) 2009, RTGI
All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.