root/branches/release-27/lib/MT/Memcached.pm @ 1197

Revision 1197, 2.7 kB (checked in by mpaschal, 23 months ago)

Support specification of a memcached Cache:: module other than Cache::Memcached through the MemcachedDriver configuration option
(thanks to Hirotaka Ogawa for this submission)
BugzID: 64709

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