root/trunk/plagger/lib/Plagger/Cache.pm

Revision 2041 (checked in by miyagawa, 1 month ago)

fixed typo

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Cache;
2 use strict;
3 use File::Path;
4 use File::Spec;
5 use HTTP::Cookies;
6 use UNIVERSAL::require;
7
8 sub new {
9     my($class, $conf, $name) = @_;
10
11     mkdir $conf->{base}, 0700 unless -e $conf->{base} && -d _;
12
13     # Cache default configuration
14     $conf->{class}  ||= 'Cache::FileCache';
15     $conf->{params} ||= {
16         cache_root => File::Spec->catfile($conf->{base}, 'cache'),
17         default_expires_in => $conf->{expires} || 'never',
18         directory_umask => 0077,
19     };
20
21     $conf->{class}->require;
22
23     # If class is not loadable, falls back to on memory cache
24     if ($@) {
25         Plagger->context->log(error => "Can't load $conf->{class}. Fallbacks to Plagger::Cache::Null");
26         require Plagger::Cache::Null;
27         $conf->{class} = 'Plagger::Cache::Null';
28     }
29
30     my $self = bless {
31         base  => $conf->{base},
32         cache => $conf->{class}->new($conf->{params}),
33         to_purge => $conf->{expires} ? 1 : 0,
34     }, $class;
35 }
36
37 sub path_to {
38     my($self, @path) = @_;
39     if (@path > 1) {
40         my @chunk = @path[0..$#path-1];
41         mkpath(File::Spec->catfile($self->{base}, @chunk), 0, 0700);
42     }
43     File::Spec->catfile($self->{base}, @path);
44 }
45
46 sub get {
47     my $self = shift;
48
49     my $value;
50     if ( $self->{cache}->isa('Cache') ) {
51         eval { $value = $self->{cache}->thaw(@_) };
52         if ($@ && $@ =~ /Storable binary/) {
53             $value = $self->{cache}->get(@_);
54         }
55     } else {
56         $value = $self->{cache}->get(@_);
57     }
58
59     my $hit_miss = defined $value ? "HIT" : "MISS";
60     Plagger->context->log(debug => "Cache $hit_miss: $_[0]");
61
62     $value;
63 }
64
65 sub get_callback {
66     my $self = shift;
67     my($key, $callback, $expiry) = @_;
68
69     my $data = $self->get($key);
70     if (defined $data) {
71         return $data;
72     }
73
74     $data = $callback->();
75     if (defined $data) {
76         $self->set($key => $data, $expiry);
77     }
78
79     $data;
80 }
81
82 sub set {
83     my $self = shift;
84     my($key, $value, $expiry) = @_;
85
86     my $setter = $self->{cache}->isa('Cache') && ref $value ? 'freeze' : 'set';
87     $self->{cache}->$setter(@_);
88 }
89
90 sub remove {
91     my $self = shift;
92     $self->{cache}->remove(@_);
93 }
94
95 sub cookie_jar {
96     my($self, $ns) = @_;
97     my $file = $ns ? "$ns.dat" : "global.dat";
98
99     my $dir = File::Spec->catfile($self->{base}, 'cookies');
100     mkdir $dir, 0700 unless -e $dir && -d _;
101
102     return HTTP::Cookies->new(
103         file => File::Spec->catfile($dir, $file),
104         autosave => 1,
105     );
106 }
107
108 sub DESTROY {
109     my $self = shift;
110     $self->{cache}->purge() if $self->{to_purge};
111 }
112
113 1;
Note: See TracBrowser for help on using the browser.