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