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);
28 use FreezeThaw
qw(freeze thaw);
31 use Torrus
::TimeStamp
;
42 $self->{store
} = Cache
::FastMmap
->new(
43 expire_time
=> $Torrus::Renderer
::cacheMaxAge
|| 60,
46 die 'Couldnt create cache object?' unless $self->{store
};
51 =head2 cacheKey($keystring, [\%options])
53 needs a unique string I<$keystring>. If the I<\%options> hashref
54 is provided it will be used as well to generate the I<$newkeystring>
62 my $keystring = shift;
63 my $options = shift || {};
65 if( ref( $options->{'variables'} ) )
67 for my $name ( sort keys %{$options->{'variables'}} )
69 my $val = $options->{'variables'}->{$name};
70 $keystring .= ':' . $name . '=' . $val;
77 =head2 getCache($keystring)
79 needs I<$keystring> as a parameter, returns the cache values as a I<@list>
86 my $keystring = shift;
87 my $options = shift || {};
89 $keystring = $self->cacheKey($keystring,$options);
91 my $cacheval = $self->{store
}->get( $keystring );
93 if( defined($cacheval) )
95 my $o = thaw
( $cacheval );
97 return @
{$o}[qw(t_render t_expires content mime_type)]
98 if ($o->{t_expires
} >= time());
100 # otherwise we go to the end and return nothing
104 #return ($t_render, $t_expires, $content, $mime_type)
105 return (0, 0, '', '')
109 =head2 setCache($keystring, $options, $t_render, $t_expires, $filename, $mime_type)
111 Sets a value in the cache based on the provided arguments
118 my $keystring = shift;
120 my $t_render = shift;
121 my $t_expires = shift;
123 my $mime_type = shift;
125 $keystring = $self->cacheKey($keystring,$options);
128 t_render
=> $t_render,
129 t_expires
=> $t_expires,
131 $mime_type => $mime_type,
135 $self->{store
}->set( $keystring, freeze
($o) );
140 =head2 checkAndClearCache
146 sub checkAndClearCache
167 # indent-tabs-mode: nil
168 # perl-indent-level: 4