updates
[torrus-plus.git] / src / lib / Torrus / Renderer / Cache.pm
blob7889e53e69802d08f69cc09d6d04c8116d2dfcf2
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;
23 use strict;
24 use warnings;
26 use Digest::MD5 qw(md5_hex);
27 use Cache::FastMmap;
28 use FreezeThaw qw(freeze thaw);
30 use Torrus::DB;
31 use Torrus::TimeStamp;
32 use Torrus::Log;
34 our $VERSION = 1.0;
36 sub new
38 my $class = shift;
39 my $self = {};
40 bless $self, $class;
42 $self->{store} = Cache::FastMmap->new(
43 expire_time => $Torrus::Renderer::cacheMaxAge || 60,
46 die 'Couldnt create cache object?' unless $self->{store};
48 return $self
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>
55 that is returned
57 =cut
59 sub cacheKey
61 my $self = shift;
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;
73 return $keystring
77 =head2 getCache($keystring)
79 needs I<$keystring> as a parameter, returns the cache values as a I<@list>
81 =cut
83 sub getCache
85 my $self = shift;
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
113 =cut
115 sub setCache
117 my $self = shift;
118 my $keystring = shift;
119 my $options = shift;
120 my $t_render = shift;
121 my $t_expires = shift;
122 my $content = shift;
123 my $mime_type = shift;
125 $keystring = $self->cacheKey($keystring,$options);
127 my $o = {
128 t_render => $t_render,
129 t_expires => $t_expires,
130 content => $content,
131 $mime_type => $mime_type,
135 $self->{store}->set( $keystring, freeze($o) );
137 return 1
140 =head2 checkAndClearCache
142 Stub.
144 =cut
146 sub checkAndClearCache
148 return 1
151 =head2 clearcache
153 Stub.
155 =cut
157 sub clearcache
159 return 1
165 # Local Variables:
166 # mode: perl
167 # indent-tabs-mode: nil
168 # perl-indent-level: 4
169 # End: