| 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 | |
|---|
| 7 | package MT::Cache::Session; |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | use MT::Session; |
|---|
| 11 | |
|---|
| 12 | sub new { |
|---|
| 13 | my $class = shift; |
|---|
| 14 | my (%param) = @_; |
|---|
| 15 | $param{'ttl'} ||= 0; |
|---|
| 16 | my $self = bless \%param, $class; |
|---|
| 17 | return $self; |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | sub get { |
|---|
| 21 | my MT::Cache::Session $self = shift; |
|---|
| 22 | my ($key) = @_; |
|---|
| 23 | |
|---|
| 24 | my $record; |
|---|
| 25 | if ( $self->{ttl} ) { |
|---|
| 26 | $record = MT::Session::get_unexpired_value( |
|---|
| 27 | $self->{ttl}, { id => $key, kind => 'CO' } ); |
|---|
| 28 | } |
|---|
| 29 | else { |
|---|
| 30 | $record = MT::Session->load( { id => $key, kind => 'CO' } ); |
|---|
| 31 | } |
|---|
| 32 | return unless $record; |
|---|
| 33 | return $record->data; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | sub get_multi { |
|---|
| 37 | my MT::Cache::Session $self = shift; |
|---|
| 38 | my @keys = @_; |
|---|
| 39 | |
|---|
| 40 | my @tmp = MT::Session->load( { id => \@keys, kind => 'CO' } ) |
|---|
| 41 | or return; |
|---|
| 42 | |
|---|
| 43 | my @records; |
|---|
| 44 | if ( $self->{ttl} ) { |
|---|
| 45 | foreach my $record (@tmp) { |
|---|
| 46 | if ( $record->start() < time - $self->{ttl} ) { |
|---|
| 47 | $record->remove; |
|---|
| 48 | next; |
|---|
| 49 | } |
|---|
| 50 | push @records, $record; |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | else { |
|---|
| 54 | @records = @tmp; |
|---|
| 55 | } |
|---|
| 56 | my %values = map { $_->id => $_->data } @records; |
|---|
| 57 | return \%values; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | sub delete { |
|---|
| 61 | my MT::Cache::Session $self = shift; |
|---|
| 62 | my ($key, $time) = @_; |
|---|
| 63 | MT::Session->remove( { id => $key, kind => 'CO' } ) |
|---|
| 64 | or return 0; |
|---|
| 65 | 1; |
|---|
| 66 | } |
|---|
| 67 | *remove = \&delete; |
|---|
| 68 | |
|---|
| 69 | sub add { |
|---|
| 70 | _set("add", @_); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | sub replace { |
|---|
| 74 | _set("replace", @_); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | sub set { |
|---|
| 78 | _set("set", @_); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | sub _set { |
|---|
| 82 | my $cmdname = shift; |
|---|
| 83 | my MT::Cache::Session $self = shift; |
|---|
| 84 | my ($key, $val, $exptime) = @_; |
|---|
| 85 | |
|---|
| 86 | my $cache = MT::Session->load({ |
|---|
| 87 | id => $key, |
|---|
| 88 | kind => 'CO', |
|---|
| 89 | }); |
|---|
| 90 | $cache->remove() if $cache; |
|---|
| 91 | $cache = MT::Session->new; |
|---|
| 92 | $cache->set_values({ |
|---|
| 93 | id => $key, |
|---|
| 94 | kind => 'CO', |
|---|
| 95 | start => time, |
|---|
| 96 | data => $val, |
|---|
| 97 | }); |
|---|
| 98 | $cache->save(); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | sub flush_all { |
|---|
| 102 | my MT::Cache::Session $self = shift; |
|---|
| 103 | MT::Session->remove({ kind => 'CO' }); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | sub DESTROY {} |
|---|
| 107 | |
|---|
| 108 | 1; |
|---|
| 109 | __END__ |
|---|
| 110 | |
|---|
| 111 | =head1 NAME |
|---|
| 112 | |
|---|
| 113 | MT::Cache::Session - Utility package caching data in MT::Session record. |
|---|
| 114 | |
|---|
| 115 | =head1 SYNOPSIS |
|---|
| 116 | |
|---|
| 117 | my $cache = MT::Cache::Session->new({ttl => 10}); |
|---|
| 118 | my $data = $cache->get($key); |
|---|
| 119 | $cache->set($key => $value); |
|---|
| 120 | my $hash = $cache->get_multi($key1, $key2); |
|---|
| 121 | |
|---|
| 122 | =head1 DESCRIPTION |
|---|
| 123 | |
|---|
| 124 | I<MT::Cache::Session> provides a wrapper interface around I<MT::Session>, |
|---|
| 125 | with the interface similar to Cache::Memcached. |
|---|
| 126 | |
|---|
| 127 | =head1 USAGE |
|---|
| 128 | |
|---|
| 129 | =head2 $cache->get |
|---|
| 130 | |
|---|
| 131 | Retrieves a key from the cache. Returns the value or undef. |
|---|
| 132 | |
|---|
| 133 | =head2 $cache->get_multi |
|---|
| 134 | |
|---|
| 135 | Retrieves multiple keys from the cache doing just one query. |
|---|
| 136 | Returns a hashref of key/value pairs that were available. |
|---|
| 137 | |
|---|
| 138 | =head2 $cache->set |
|---|
| 139 | |
|---|
| 140 | Unconditionally sets a key to a given value in the memcache. Returns true |
|---|
| 141 | if it was stored successfully. |
|---|
| 142 | |
|---|
| 143 | =head2 $cache->delete |
|---|
| 144 | |
|---|
| 145 | Deletes a key. You may also use the alternate method name B<remove>, |
|---|
| 146 | so MT::Cache::Session looks like the L<Cache::Cache> API. |
|---|
| 147 | |
|---|
| 148 | =head2 $cache->flush_all |
|---|
| 149 | |
|---|
| 150 | Empty all the caches. |
|---|
| 151 | |
|---|
| 152 | =head1 AUTHOR & COPYRIGHT |
|---|
| 153 | |
|---|
| 154 | Please see L<MT/AUTHOR & COPYRIGHT>. |
|---|
| 155 | |
|---|
| 156 | =cut |
|---|