| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Jifty::CAS::Store::Memcached; | ||
| 5 | use Any::Moose; | ||
| 6 | extends 'Jifty::CAS::Store'; | ||
| 7 | has 'servers' => (is => 'rw'); | ||
| 8 | has 'debug' => (is => 'rw'); | ||
| 9 | has 'namespace' => (is => 'rw'); | ||
| 10 | has 'compress_threshold' => (is => 'rw'); | ||
| 11 | has 'memcached' => (is => 'rw'); | ||
| 12 | |||
| 13 | use Cache::Memcached; | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Jifty::CAS::Store::Memcached - A memcached backend for Jifty's CAS | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | At the bare minimum, add the following to your Jifty config.yml: | ||
| 22 | |||
| 23 | framework: | ||
| 24 | CAS: | ||
| 25 | Default: | ||
| 26 | Class: 'Jifty::CAS::Store::Memcached' | ||
| 27 | |||
| 28 | The options available include: | ||
| 29 | |||
| 30 | framework: | ||
| 31 | CAS: | ||
| 32 | Default: | ||
| 33 | Class: 'Jifty::CAS::Store::Memcached' | ||
| 34 | # any options Cache::Memcached supports | ||
| 35 | Servers: | ||
| 36 | - 10.0.0.2:11211 | ||
| 37 | - 10.0.0.3:11211 | ||
| 38 | Compress_Threshold: 5120 | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | This is a memcached backend for L<Jifty::CAS>. For more information about | ||
| 43 | Jifty's CAS, see L<Jifty::CAS/DESCRIPTION>. | ||
| 44 | |||
| 45 | =head1 METHODS | ||
| 46 | |||
| 47 | =head2 BUILD | ||
| 48 | |||
| 49 | Constructs the L</memcached> object for this object, based on the | ||
| 50 | specified C<servers>, C<debug>, C<namespace>, and C<compress_threshold> | ||
| 51 | arguments in the CAS configuration. | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub BUILD { | ||
| 56 | my $self = shift; | ||
| 57 | $self->memcached( | ||
| 58 | Cache::Memcached->new( | ||
| 59 | servers => $self->servers || [ '127.0.0.1:11211' ], | ||
| 60 | debug => $self->debug, | ||
| 61 | namespace => $self->namespace || Jifty->config->framework('ApplicationName').":CAS:", | ||
| 62 | compress_threshold => $self->compress_threshold || 10240, | ||
| 63 | ) | ||
| 64 | ); | ||
| 65 | } | ||
| 66 | |||
| 67 | =head2 memcached | ||
| 68 | |||
| 69 | Returns the L<Cache::Memcached> object for this class. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | =head2 _store DOMAIN NAME BLOB | ||
| 74 | |||
| 75 | Stores the BLOB (a L<Jifty::CAS::Blob>) in memcached. Returns the key on | ||
| 76 | success or undef on failure. | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub _store { | ||
| 81 | my ($self, $domain, $name, $blob) = @_; | ||
| 82 | |||
| 83 | my $key = $blob->key; | ||
| 84 | my $success = $self->memcached->set("$domain:db:$key", $blob); | ||
| 85 | |||
| 86 | unless ($success) { | ||
| 87 | my $err = "Failed to store content for key '$domain:db:$key' in memcached!"; | ||
| 88 | { | ||
| 89 | use bytes; | ||
| 90 | $err .= " Content length is: " . length($blob->content) . " bytes."; | ||
| 91 | $err .= " Perhaps you need to increase memcached's max item size?"; | ||
| 92 | } | ||
| 93 | Jifty->log->error($err); | ||
| 94 | |||
| 95 | # fail with undef | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | |||
| 99 | $success = $self->memcached->set("$domain:keys:$name", $key); | ||
| 100 | |||
| 101 | unless ($success) { | ||
| 102 | Jifty->log->error("Failed to store key '$domain:keys:$name' in memcached!"); | ||
| 103 | return; | ||
| 104 | } | ||
| 105 | |||
| 106 | return $key; | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 key DOMAIN NAME | ||
| 110 | |||
| 111 | Returns the most recent key for the given pair of C<DOMAIN> and | ||
| 112 | C<NAME>, or undef if none such exists. | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | sub key { | ||
| 117 | my ($self, $domain, $name) = @_; | ||
| 118 | my $key = $self->memcached->get("$domain:keys:$name"); | ||
| 119 | return $key if defined $key; | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | |||
| 123 | =head2 retrieve DOMAIN KEY | ||
| 124 | |||
| 125 | Returns a L<Jifty::CAS::Blob> for the given pair of C<DOMAIN> and | ||
| 126 | C<KEY>, or undef if none such exists. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub retrieve { | ||
| 131 | my ($self, $domain, $key) = @_; | ||
| 132 | my $blob = $self->memcached->get("$domain:db:$key"); | ||
| 133 | return $blob if defined $blob; | ||
| 134 | return; | ||
| 135 | } | ||
| 136 | |||
| 137 | no Any::Moose; | ||
| 138 | __PACKAGE__->meta->make_immutable(inline_constructor => 0); | ||
| 139 | 1; |