root/branches/release-30/lib/MT/Memcached.pm @ 1414

Revision 1414, 2.7 kB (checked in by bchoate, 21 months ago)

Use capability test to invoke disconnect_all on memcached class.

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