root/branches/release-39/lib/MT/Cache/Session.pm @ 2505

Revision 2505, 3.6 kB (checked in by fumiakiy, 18 months ago)

Purge stale search cache after it finishes processing, and also periodically. BugId:79999

  • Property svn:keywords set to Id Date Author Revision
Line 
1# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
2# This program is distributed under the terms of the
3# GNU General Public License, version 2.
4#
5# $Id$
6
7package MT::Cache::Session;
8
9use strict;
10use MT::Session;
11
12sub new {
13    my $class = shift;
14    my (%param) = @_;
15    $param{'ttl'} ||= 0;
16    $param{'kind'} ||= 'CO';
17    my $self = bless \%param, $class;
18    return $self;
19}
20
21sub get {
22    my MT::Cache::Session $self = shift;
23    my ($key) = @_;
24
25    my $record;
26    if ( $self->{ttl} ) {
27        $record = MT::Session::get_unexpired_value(
28            $self->{ttl}, { id => $key, kind => $self->{kind} } );
29    }
30    else {
31        $record = MT::Session->load( { id => $key, kind => $self->{kind} } );
32    }
33    return unless $record;
34    return $record->data;
35}
36
37sub get_multi {
38    my MT::Cache::Session $self = shift;
39    my @keys = @_;
40
41    my @tmp = MT::Session->load( { id => \@keys, kind => $self->{kind} } )
42        or return;
43
44    my @records;
45    if ( $self->{ttl} ) {
46        foreach my $record (@tmp) {
47            if ( $record->start() < time - $self->{ttl} ) {
48                $record->remove;
49                next;
50            }
51            push @records, $record;
52        }
53    }
54    else {
55        @records = @tmp;
56    }
57    my %values = map { $_->id => $_->data } @records;
58    return \%values;
59}
60
61sub delete {
62    my MT::Cache::Session $self = shift;
63    my ($key, $time) = @_;
64    MT::Session->remove( { id => $key, kind => $self->{kind} } )
65        or return 0;
66    1;
67}
68*remove = \&delete;
69
70sub add {
71    _set("add", @_);
72}
73
74sub replace {
75    _set("replace", @_);
76}
77
78sub set {
79    _set("set", @_);
80}
81
82sub _set {
83    my $cmdname = shift;
84    my MT::Cache::Session $self = shift;
85    my ($key, $val, $exptime) = @_;
86
87    my $cache = MT::Session->load({
88        id   => $key,
89        kind => $self->{kind},
90    });
91    $cache->remove() if $cache;
92    $cache = MT::Session->new;
93    $cache->set_values({
94        id    => $key,
95        kind  => $self->{kind},
96        start => time,
97        data  => $val,
98    });
99    $cache->save();
100}
101
102sub flush_all {
103    my MT::Cache::Session $self = shift;
104    MT::Session->remove({ kind => $self->{kind} });
105}
106
107sub purge_stale {
108    my MT::Cache::Session $self = shift;
109    my ( $ttl ) = @_;
110    MT::Session->remove(
111        { kind => $self->{kind}, start => [ undef, time - $ttl ] },
112        { range => { start => 1 } }
113    );
114    1;
115}
116
117sub DESTROY {}
118
1191;
120__END__
121
122=head1 NAME
123
124MT::Cache::Session - Utility package caching data in MT::Session record.
125
126=head1 SYNOPSIS
127
128    my $cache = MT::Cache::Session->new({ttl => 10});
129    my $data = $cache->get($key);
130    $cache->set($key => $value);
131    my $hash = $cache->get_multi($key1, $key2);
132
133=head1 DESCRIPTION
134
135I<MT::Cache::Session> provides a wrapper interface around I<MT::Session>,
136with the interface similar to Cache::Memcached.
137
138=head1 USAGE
139
140=head2 $cache->get
141
142Retrieves a key from the cache.  Returns the value or undef.
143
144=head2 $cache->get_multi
145
146Retrieves multiple keys from the cache doing just one query.
147Returns a hashref of key/value pairs that were available.
148
149=head2 $cache->set
150
151Unconditionally sets a key to a given value in the memcache.  Returns true
152if it was stored successfully.
153
154=head2 $cache->delete
155
156Deletes a key.  You may also use the alternate method name B<remove>,
157so MT::Cache::Session looks like the L<Cache::Cache> API.
158
159=head2 $cache->flush_all
160
161Empty all the caches.
162
163=head2 $cache->purge_stale
164
165Deletes all the cached objects that are already too old.
166Takes one argument: $ttl specifies the duration that is considered stale.
167
168=head1 AUTHOR & COPYRIGHT
169
170Please see L<MT/AUTHOR & COPYRIGHT>.
171
172=cut
Note: See TracBrowser for help on using the browser.