root/branches/release-38/lib/MT/Memcached.pm @ 2253

Revision 2253, 2.8 kB (checked in by bchoate, 19 months ago)

Applied optimization for Memcached availablity test. Thanks, Ogawa-san. BugId:79648

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