1 # Copyright (C) 2012 Dean Hamstead
2 # Copyright (C) 2002-2011 Stanislav Sinyagin
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18 # Dean Hamstead <dean@fragfest.com.au>
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
22 package Torrus
::Renderer
::Cache
;
26 use Digest
::MD5
qw(md5_hex);
31 use Torrus
::TimeStamp
;
42 if( not defined $Torrus::Global
::cacheDir
)
44 Error
('$Torrus::Global::cacheDir must be defined');
47 elsif( not -d
$Torrus::Global
::cacheDir
)
49 Error
("No such directory: $Torrus::Global::cacheDir");
53 $self->{'cachedir'} = $Torrus::Global
::cacheDir
;
54 $self->{'cachemaxage'} = $Torrus::Renderer
::cacheMaxAge
|| 60;
56 $self->{'db'} = Torrus
::DB
->new('render_cache', -WriteAccess
=> 1);
57 if( not defined( $self->{'db'} ) )
59 die 'Failed to create render cache';
67 =head2 cacheKey($keystring, [\%options])
69 needs a unique string I<$keystring>. If the I<\%options> hashref
70 is provided it will be used as well to generate the I<$newkeystring>
78 my $keystring = shift;
79 my $options = shift || {};
81 if( ref( $options->{'variables'} ) )
83 for my $name ( sort keys %{$options->{'variables'}} )
85 my $val = $options->{'variables'}->{$name};
86 $keystring .= ':' . $name . '=' . $val;
93 =head2 getCache($keystring)
95 needs I<$keystring> as a parameter, returns the cache values as a I<@list>
102 my $keystring = shift;
103 my $options = shift || {};
105 $keystring = $self->cacheKey($keystring,$options);
107 my $cacheval = $self->{db
}->get( $keystring );
109 if( defined($cacheval) )
111 my ($t_render, $t_expires, $filename, $mime_type) = split(':', $cacheval);
113 my $cachefile = File
::Spec
->catfile($Torrus::Global
::cacheDir
,$filename);
115 if ($t_expires >= time() and -f
$cachefile) { # if we delete the cache files we are ok
117 my $fh = IO
::File
->new($cachefile, '<');
122 return ($t_render, $t_expires, $content, $mime_type)
126 # otherwise we go to the end and return nothing
130 #return ($t_render, $t_expires, $content, $mime_type)
131 return (0, 0, '', '')
135 =head2 setCache($keystring, $t_render, $t_expires, $filename, $mime_type)
137 Sets a value in the cache based on the provided arguments
144 my $keystring = shift;
146 my $t_render = shift;
147 my $t_expires = shift;
149 my $mime_type = shift;
151 $keystring = $self->cacheKey($keystring,$options);
153 my $filename = $self->newCacheFileName($keystring);
155 my $fh = IO
::File
->new($filename, '>') or return;
159 $self->{db
}->put( $keystring,
161 ($t_render, $t_expires, $filename, $mime_type)));
165 sub checkAndClearCache
168 my $config_tree = shift;
170 my $tree = $config_tree->treeName();
172 my $ts = Torrus
::TimeStamp
->new();
173 my $known_ts = $ts->get($tree . ':renderer_cache');
174 my $actual_ts = $config_tree->getTimestamp($ts);
175 if( $actual_ts >= $known_ts or
176 time() >= $known_ts + $self->{'cachemaxage'} )
179 $ts->setNow($tree . ':renderer_cache');
189 Debug
('Clearing renderer cache');
190 my $cursor = $self->{'db'}->cursor( -Write
=> 1 );
192 while( my ($key, $val) = $self->{'db'}->next( $cursor ) )
194 my($t_render, $t_expires, $filename, $mime_type) = split(':', $val);
196 unlink File
::Spec
->catfile($self->{cachedir
},$filename);
197 $self->{'db'}->c_del( $cursor );
199 $self->{'db'}->c_close($cursor);
200 Debug
('Renderer cache cleared');
205 =head newCacheFileName
207 Returns a new cachefilename based on the provided args
$cachekey and
210 It would be far better
if this used File
::Temp
216 my $cachekey = shift;
217 return sprintf('%s_%.5d', md5_hex
($cachekey), rand(1e5
));
226 # indent-tabs-mode: nil
227 # perl-indent-level: 4