| 1 | package MojoX::Plugin::AnyCache; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 8 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 8 | has '_raw'; | |||||||||||||||||||||||||||
| 8 | 8 | has 'app'; | |||||||||||||||||||||||||||
| 9 | 8 | has 'backend'; | |||||||||||||||||||||||||||
| 10 | 8 | has 'config'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 6 | my ($self, $app, $config) = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 6 | $self->app($app); | |||||||||||||||||||||||||||
| 16 | 6 | $self->config($config); | |||||||||||||||||||||||||||
| 17 | 6 | ▶ | $app->helper(cache => sub { $self }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 6 | ▶ | if(exists $config->{backend}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 5 | eval { | |||||||||||||||||||||||||||
| 21 | 5 | eval "require $config->{backend};"; | |||||||||||||||||||||||||||
| 22 | 5 | ▶ | warn "Require failed: $@" if $self->config->{debug} && $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->config->{"debug"} and $@
MC/DC0%0/2
$self->config->{"debug"} and $@
$self->config->{"debug"}
$@
Truth table33%1/3
A and B
A= $self->config->{"debug"}B= $@
| |||||||||||||||||||||||||||||
| 23 | 5 | my $backend = $config->{backend}->new; | |||||||||||||||||||||||||||
| 24 | 5 | $backend->config($config); | |||||||||||||||||||||||||||
| 25 | 5 | $self->backend($backend); | |||||||||||||||||||||||||||
| 26 | 5 | my $method = "init"; | |||||||||||||||||||||||||||
| 27 | 5 | ▶ | $backend->$method() if $backend->can($method); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||||||||||
| 29 | 5 | ▶ | die("Failed to load backend $config->{backend}: $@") if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 71 | ▶ | sub check_mode { | ||||||||||||||||||||||||||
|
Subroutine
check_mode: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 71 | my ($self, $cb) = @_; | |||||||||||||||||||||||||||
| 35 | 71 | ▶ | die("No backend available") if !$self->backend; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 62 | ▶ | die("Backend " . ref($self->backend) ." doesn't support asynchronous requests") if $cb && !$self->backend->support_async; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cb and not $self->backend->support_async
MC/DC100%2/2
$cb and not $self->backend->support_async
$cb
not $self->backend->support_async
Truth table100%3/3
A and not(B)
A= $cbB= not $self->backend->support_async
| |||||||||||||||||||||||||||||
| 37 | 60 | ▶ | die("Backend " . ref($self->backend) ." doesn't support synchronous requests") if !$cb && !$self->backend->support_sync; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cb || $self->backend->support_sync
MC/DC100%2/2
$cb || $self->backend->support_sync
$cb
$self->backend->support_sync
Truth table100%3/3
A || B
A= $cbB= $self->backend->support_sync
| |||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | sub raw { | ||||||||||||||||||||||||||
|
Subroutine
raw: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | my $clone = $self->new; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # Deep copy | ||||||||||||||||||||||||||||
| 46 | 1 | $clone->app ( $self->app ); | |||||||||||||||||||||||||||
| 47 | 1 | $clone->backend( $self->backend ); | |||||||||||||||||||||||||||
| 48 | 1 | $clone->config ( $self->config ); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | $clone->_raw(1); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | return $clone; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 29 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 29 | ▶ | my $cb = ref($_[-1]) eq 'CODE' ? pop : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 29 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 58 | 29 | $self->check_mode($cb); | |||||||||||||||||||||||||||
| 59 | 24 | ▶ | if( !$self->_raw && (my $serialiser = $self->backend->get_serialiser)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $self->_raw and my $serialiser = $self->backend->get_serialiser
MC/DC100%2/2
not $self->_raw and my $serialiser = $self->backend->get_serialiser
not $self->_raw
my $serialiser = $self->backend->get_serialiser
Truth table100%3/3
not(A) and B
A= not $self->_rawB= my $serialiser = $self->backend->get_serialiser
| |||||||||||||||||||||||||||||
| 60 | 7 | ▶ | return $self->backend->get($key, sub { $cb->($serialiser->deserialise(@_)) }) if $cb; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 4 | return $serialiser->deserialise($self->backend->get($key)); | |||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||
| 63 | 17 | ▶ | return $self->backend->get($key, sub { $cb->(@_) }) if $cb; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 12 | return $self->backend->get($key); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 16 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | 16 | ▶ | my $cb = ref($_[-1]) eq 'CODE' ? pop : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 16 | my ($self, $key, $value, $ttl) = @_; | |||||||||||||||||||||||||||
| 71 | 16 | $self->check_mode($cb); | |||||||||||||||||||||||||||
| 72 | 15 | ▶ | if( !$self->_raw && (my $serialiser = $self->backend->get_serialiser)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $self->_raw and my $serialiser = $self->backend->get_serialiser
MC/DC50%1/2
not $self->_raw and my $serialiser = $self->backend->get_serialiser
not $self->_raw
my $serialiser = $self->backend->get_serialiser
Truth table66%2/3
not(A) and B
A= not $self->_rawB= my $serialiser = $self->backend->get_serialiser
| |||||||||||||||||||||||||||||
| 73 | 5 | ▶ | return $self->backend->set($key, $serialiser->serialise($value), $ttl, sub { $cb->(@_) }) if $cb; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 3 | return $self->backend->set($key => $serialiser->serialise($value), $ttl); | |||||||||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||||||||||
| 76 | 10 | ▶ | return $self->backend->set($key, $value, $ttl, sub { $cb->(@_) }) if $cb; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 6 | return $self->backend->set($key => $value, $ttl); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 8 | ▶ | sub incr { | ||||||||||||||||||||||||||
|
Subroutine
incr: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | 8 | ▶ | my $cb = ref($_[-1]) eq 'CODE' ? pop : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 8 | my ($self, $key, $amount) = @_; | |||||||||||||||||||||||||||
| 84 | 8 | $self->check_mode($cb); | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | return $self->backend->incr($key, $amount, sub { $cb->(@_) }) if $cb; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 6 | return $self->backend->incr($key => $amount); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 8 | ▶ | sub decr { | ||||||||||||||||||||||||||
|
Subroutine
decr: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 90 | 8 | ▶ | my $cb = ref($_[-1]) eq 'CODE' ? pop : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 8 | my ($self, $key, $amount) = @_; | |||||||||||||||||||||||||||
| 92 | 8 | $self->check_mode($cb); | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | return $self->backend->decr($key, $amount, sub { $cb->(@_) }) if $cb; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 6 | return $self->backend->decr($key => $amount); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 3 | ▶ | sub del { | ||||||||||||||||||||||||||
|
Subroutine
del: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | 3 | ▶ | my $cb = ref($_[-1]) eq 'CODE' ? pop : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 3 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 100 | 3 | $self->check_mode($cb); | |||||||||||||||||||||||||||
| 101 | 0 | ▶ | return $self->backend->del($key, sub { $cb->(@_) }) if $cb; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 1 | return $self->backend->del($key); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 7 | ▶ | sub ttl { | ||||||||||||||||||||||||||
|
Subroutine
ttl: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | 7 | ▶ | my $cb = ref($_[-1]) eq 'CODE' ? pop : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 7 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 108 | 7 | $self->check_mode($cb); | |||||||||||||||||||||||||||
| 109 | 6 | ▶ | return $self->backend->ttl($key, sub { $cb->(@_) }) if $cb; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 4 | return $self->backend->ttl($key); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1 | ▶ | sub increment { shift->incr(@_) } | ||||||||||||||||||||||||||
|
Subroutine
increment: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 114 | 1 | ▶ | sub decrement { shift->decr(@_) } | ||||||||||||||||||||||||||
|
Subroutine
decrement: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 115 | 1 | ▶ | sub delete { shift->del(@_) } | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 8 | 1; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =encoding utf8 | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 NAME | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | MojoX::Plugin::AnyCache - Cache plugin with blocking and non-blocking support | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | $app->plugin('MojoX::Plugin::AnyCache' => { | ||||||||||||||||||||||||||||
| 128 | backend => 'MojoX::Plugin::AnyCache::Backend::Redis', | ||||||||||||||||||||||||||||
| 129 | server => '127.0.0.1:6379', | ||||||||||||||||||||||||||||
| 130 | }); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # For synchronous backends (blocking) | ||||||||||||||||||||||||||||
| 133 | $app->cache->set('key', 'value'); | ||||||||||||||||||||||||||||
| 134 | my $value = $app->cache->get('key'); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # For asynchronous backends (non-blocking) | ||||||||||||||||||||||||||||
| 137 | $app->cache->set('key', 'value' => sub { | ||||||||||||||||||||||||||||
| 138 | # ... | ||||||||||||||||||||||||||||
| 139 | }); | ||||||||||||||||||||||||||||
| 140 | $app->cache->get('key' => sub { | ||||||||||||||||||||||||||||
| 141 | my $value = shift; | ||||||||||||||||||||||||||||
| 142 | # ... | ||||||||||||||||||||||||||||
| 143 | }); | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | MojoX::Plugin::AnyCache provides an interface to both blocking and non-blocking | ||||||||||||||||||||||||||||
| 148 | caching backends, for example Redis or Memcached. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | It also has a built-in replicator backend (L<MojoX::Plugin::AnyCache::Backend::Replicator>) | ||||||||||||||||||||||||||||
| 151 | which automatically replicates values across multiple backend cache nodes. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head2 SERIALISATION | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | The cache backend module supports an optional serialiser module. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | $app->plugin('MojoX::Plugin::AnyCache' => { | ||||||||||||||||||||||||||||
| 158 | backend => 'MojoX::Plugin::AnyCache::Backend::Redis', | ||||||||||||||||||||||||||||
| 159 | server => '127.0.0.1:6379', | ||||||||||||||||||||||||||||
| 160 | serialiser => 'MojoX::Plugin::AnyCache::Serialiser::MessagePack' | ||||||||||||||||||||||||||||
| 161 | }); | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head4 SERIALISER WARNING | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | If you use a serialiser, C<incr> or C<decr> a value, then retrieve | ||||||||||||||||||||||||||||
| 166 | the value using C<get>, the value returned is deserialised. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | With the FakeSerialiser used in tests, this means C<1> is translated to an C<A>. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | This 'bug' can be avoided by reading the value from the cache backend | ||||||||||||||||||||||||||||
| 171 | directly, bypassing the backend serialiser: | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | $self->cache->set('foo', 1); | ||||||||||||||||||||||||||||
| 174 | $self->cache->backend->get('foo'); | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head2 TTL / EXPIRES | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head3 Redis | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Full TTL support is available with a Redis backend. Pass the TTL (in seconds) | ||||||||||||||||||||||||||||
| 181 | to the C<set> method. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | $cache->set("key", "value", 10); | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | $cache->set("key", "value", 10, sub { | ||||||||||||||||||||||||||||
| 186 | # ... | ||||||||||||||||||||||||||||
| 187 | }); | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | And to get the TTL (seconds remaining until expiry) | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | my $ttl = $cache->ttl("key"); | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | $cache->ttl("key", sub { | ||||||||||||||||||||||||||||
| 194 | my ($ttl) = @_; | ||||||||||||||||||||||||||||
| 195 | # ... | ||||||||||||||||||||||||||||
| 196 | }); | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head3 Memcached | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Full TTL set support is available with a Memcached backend. Pass the TTL (in seconds) | ||||||||||||||||||||||||||||
| 201 | to the C<set> method. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | $cache->set("key", "value", 10); | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | $cache->set("key", "value", 10, sub { | ||||||||||||||||||||||||||||
| 206 | # ... | ||||||||||||||||||||||||||||
| 207 | }); | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | Unlike a Redis backend, 'get' TTL mode in Memcached is emulated, and the time | ||||||||||||||||||||||||||||
| 210 | remaining is calculated using timestamps, and stored in a separate prefixed key. | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | To enable this, set C<get_ttl_support> on the backend: | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | $cache->backend->get_ttl_support(1); | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | This must be done before setting a value. You can then get the TTL as normal: | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | my $ttl = $cache->ttl("key"); | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | $cache->ttl("key", sub { | ||||||||||||||||||||||||||||
| 221 | my ($ttl) = @_; | ||||||||||||||||||||||||||||
| 222 | # ... | ||||||||||||||||||||||||||||
| 223 | }); | ||||||||||||||||||||||||||||