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

Revision 2505, 2.9 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 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::Memcached;
8use strict;
9
10sub new {
11    my $class = shift;
12    my $cfg = MT->config;
13
14    if (my @servers = $cfg->MemcachedServers) {
15        my $driver_class = $cfg->MemcachedDriver;
16        eval "require $driver_class;";
17        my $ns = $cfg->MemcachedNamespace;
18        return bless {
19            memcached => $driver_class->new({
20                servers => \@servers,
21                ( $ns ? ( namespace => $ns ) : () ),
22                debug   => 0,
23            })
24        }, $class;
25    } else {
26        return bless { }, $class;
27    }
28}
29
30{
31my $Is_Available;
32sub is_available {
33    return $Is_Available if defined $Is_Available;
34    my $class = shift;
35    my @servers = MT->config->MemcachedServers;
36    my $driver_class = MT->config->MemcachedDriver;
37    $Is_Available = @servers > 0 && eval "require $driver_class;" ? 1 : 0;
38    return $Is_Available;
39}
40}
41
42our $Instance;
43sub instance {
44    return $Instance ||= shift->new;
45}
46
47sub cleanup {
48    undef $Instance;
49    my $driver_class = MT->config->MemcachedDriver;
50    if ($driver_class->can('disconnect_all')) {
51        $driver_class->disconnect_all;
52    }
53}
54
55# method for MT::Cache interface
56sub purge_stale {
57    1;
58}
59
60sub DESTROY { }
61
62sub AUTOLOAD {
63    my $cache = shift;
64    (my $method = our $AUTOLOAD) =~ s/^.*:://;
65    return unless $cache->{memcached};
66    return $cache->{memcached}->$method(@_);
67}
68
691;
70__END__
71
72=head1 NAME
73
74MT::Memcached - Handy wrapper class for Cache::Memcached
75
76=head1 SYNOPSIS
77
78    my $cache = MT::Memcached->instance;
79    my $data = $cache->get($key);
80    $cache->set($key => $value);
81
82=head1 DESCRIPTION
83
84I<MT::Memcached> provides a singleton wrapper interface around
85I<Cache::Memcached>. It automatically loads up the I<Cache::Memcached>
86object with the server information defined in the Movable Type
87configuration file, in the C<MemcachedServers> variable.
88
89If your server environment doesn't have I<Cache::Memcached> installed,
90or if no C<MemcachedServers> are defined in your configuration, all of
91the methods on this class are essentially no-ops. This is handy from the
92perspective of the calling code, because it can act the same way whether
93or not there's a cache defined.
94
95=head1 USAGE
96
97See the I<Cache::Memcached> documentation for information on the
98available methods.
99
100In addition, this class defines the following:
101
102=head2 MT::Memcached->instance
103
104Returns the singleton object (a I<MT::Memcached> object).
105
106=head2 MT::Memcached->is_available
107
108Returns true if there are memcached servers defined in your Movable Type
109configuration and if the I<Cache::Memcached> module can be loaded.
110
111=head2 MT::Memcached->cleanup
112
113Removes the singleton instance of the class, and disconnects any open
114connections to I<memcached>.
115
116=head1 AUTHOR & COPYRIGHT
117
118Please see L<MT/AUTHOR & COPYRIGHT>.
119
120=cut
Note: See TracBrowser for help on using the browser.