| 1 | |||
| 2 | package MooseX::WithCache::Backend::Cache::Memcached; | ||
| 3 | use Moose; | ||
| 4 | use Moose::Util::TypeConstraints; | ||
| 5 | |||
| 6 | extends 'MooseX::WithCache::Backend'; | ||
| 7 | |||
| 8 | foreach my $class (qw(Cache::Memcached Cache::Memcached::Fast Cache::Memcached::libmemcached)) { | ||
| 9 | class_type $class; | ||
| 10 | coerce $class | ||
| 11 | => from 'HashRef' | ||
| 12 | => via { $class->new($_) } | ||
| 13 | ; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub _build_cache_type { | ||
| 17 | return 'Cache::Memcached | Cache::Memcached::Fast | Cache::Memcached::libmemcached '; | ||
| 18 | } | ||
| 19 | |||
| 20 | has '+can_coerce' => ( default => 1 ); | ||
| 21 | |||
| 22 | around _build_methods => sub { | ||
| 23 | my ($next, $self) = @_; | ||
| 24 | my $methods = $next->($self); | ||
| 25 | |||
| 26 | $methods->{ cache_incr } = sub { | ||
| 27 | my ($self, $key) = @_; | ||
| 28 | my $cache = $self->__get_cache(); | ||
| 29 | if ($self->cache_disabled || ! $cache) { | ||
| 30 | if (MooseX::WithCache::DEBUG()) { | ||
| 31 | $self->cache_debug(blessed $self, "cache_incr: Cache disabled"); | ||
| 32 | } | ||
| 33 | return (); | ||
| 34 | } | ||
| 35 | |||
| 36 | my $keygen = $self->cache_key_generator; | ||
| 37 | my $cache_key = $keygen ? $keygen->generate($key) : $key; | ||
| 38 | return $cache->incr($cache_key); | ||
| 39 | }; | ||
| 40 | |||
| 41 | $methods->{ cache_decr } = sub { | ||
| 42 | my ($self, $key) = @_; | ||
| 43 | my $cache = $self->__get_cache(); | ||
| 44 | if ($self->cache_disabled || ! $cache) { | ||
| 45 | if (MooseX::WithCache::DEBUG()) { | ||
| 46 | $self->cache_debug(blessed $self, "cache_decr: Cache disabled"); | ||
| 47 | } | ||
| 48 | return (); | ||
| 49 | } | ||
| 50 | |||
| 51 | my $keygen = $self->cache_key_generator; | ||
| 52 | my $cache_key = $keygen ? $keygen->generate($key) : $key; | ||
| 53 | return $cache->decr($cache_key); | ||
| 54 | }; | ||
| 55 | |||
| 56 | $methods->{ cache_get_multi } = sub { | ||
| 57 | my ($self, @keys) = @_; | ||
| 58 | my $cache = $self->__get_cache(); | ||
| 59 | if ($self->cache_disabled || ! $cache) { | ||
| 60 | if (MooseX::WithCache::DEBUG()) { | ||
| 61 | $self->cache_debug(blessed $self, "cache_get_multi: Cache disabled"); | ||
| 62 | } | ||
| 63 | return (); | ||
| 64 | } | ||
| 65 | |||
| 66 | my $keygen = $self->cache_key_generator; | ||
| 67 | |||
| 68 | my @cache_keys = $keygen ? | ||
| 69 | map { $keygen->generate($_) } @keys : | ||
| 70 | @keys; | ||
| 71 | my %cache_ret = %{ $cache->get_multi(@cache_keys) }; | ||
| 72 | if (MooseX::WithCache::DEBUG()) { | ||
| 73 | foreach my $key (@cache_keys) { | ||
| 74 | $self->cache_debug( | ||
| 75 | blessed $self, | ||
| 76 | "cache_get_multi:\n", | ||
| 77 | " + key =", | ||
| 78 | ($key || '(null)'), | ||
| 79 | "\n + status =", | ||
| 80 | exists $cache_ret{$key} ? "[HIT]" : "[MISS]", | ||
| 81 | ); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | # in scalar context, returns a hashref | ||
| 86 | # { | ||
| 87 | # results => \%results, | ||
| 88 | # missing => \@keys | ||
| 89 | # } | ||
| 90 | my $wantarray = wantarray; | ||
| 91 | if ($wantarray) { | ||
| 92 | return map { $cache_ret{$_} } | ||
| 93 | grep { exists $cache_ret{$_} } @cache_keys; | ||
| 94 | } elsif (defined $wantarray) { | ||
| 95 | return { | ||
| 96 | results => { | ||
| 97 | map {($keys[$_] => $cache_ret{$cache_keys[$_]}) } | ||
| 98 | grep { exists $cache_ret{$cache_keys[$_]} } (0..$#keys) | ||
| 99 | }, | ||
| 100 | missing => [ map { $keys[$_] } grep { ! exists $cache_ret{$cache_keys[$_]} } (0..$#keys) ] | ||
| 101 | } | ||
| 102 | } | ||
| 103 | }; | ||
| 104 | |||
| 105 | return $methods; | ||
| 106 | }; | ||
| 107 | |||
| 108 | no Moose; | ||
| 109 | no Moose::Util::TypeConstraints; | ||
| 110 | |||
| 111 | __PACKAGE__->meta->make_immutable; | ||
| 112 | |||
| 113 | 1; | ||
| 114 | |||
| 115 | __END__ |