File Coverage

blib/lib/Cache/Memcached/Sweet.pm
Criterion Covered Total %
statement 15 29 51.7
branch 0 10 0.0
condition n/a
subroutine 5 6 83.3
pod 1 1 100.0
total 21 46 45.6


line stmt bran cond sub pod time code
1             package Cache::Memcached::Sweet;
2              
3 1     1   24143 use 5.006;
  1         7  
  1         64  
4 1     1   6 use strict;
  1         9  
  1         40  
5 1     1   6 use warnings FATAL => 'all';
  1         7  
  1         55  
6              
7 1     1   5 use Exporter 'import';
  1         2  
  1         31  
8              
9 1     1   1551 use Cache::Memcached;
  1         171554  
  1         241  
10              
11             our @EXPORT = qw(memcached);
12              
13             our $VERSION = '0.02';
14              
15             my $memcached = new Cache::Memcached(servers => ['localhost:11211']);
16              
17             sub memcached {
18 0     0 1   my ($k, $v, $ttl) = @_;
19 0 0         if( @_ >= 2) {
    0          
20 0 0         if (ref($v) eq 'CODE') {
21 0           my $code = $v;
22 0           $v = $memcached->get($k);
23 0 0         unless (defined $v) {
24 0           $v = $code->();
25 0           $memcached->set($k, $v, $ttl);
26             }
27 0           return $v;
28             } else {
29 0 0         $memcached->set($k, $v, $ttl) || warn "Setting $k failed";
30 0           return $v;
31             }
32             } elsif (@_ == 1) {
33 0           $v = $memcached->get($k);
34 0           return $v;
35             } else {
36 0           return $memcached;
37             }
38             }
39              
40             =head1 NAME
41              
42             Cache::Memcached::Sweet - sugary memcached with callbacks
43              
44             =head1 SYNOPSIS
45              
46             use Cache::Memcached::Sweet; # exports the subroutine "memcached"
47              
48             my $value = memcached($key, sub {
49             return some_expensive_operation($foo, $bar, $baz);
50             });
51              
52             my $value = memcached($key); # retrieve value
53              
54             memcached($other_key, { a => 1, b => 2 }, 600); # set value with TTL
55              
56              
57             =head1 FUNCTIONS
58              
59             L implements and exports the following function
60              
61             =head2 memcached
62            
63             # Set's a value with no TTL
64             memcached('foo', $cache_this);
65              
66             # Sets a value, with optional ttl
67             memcached('bar', { a => 1, b => 2 }, 600);
68              
69             # Retrieves a value from the cache
70             my $value = memcached('baz');
71              
72             # Gets a value if it exists or sets it with the return value from executing the coderef
73             # Note: The coderef is called in scalar context
74             my $result = memcached('blurgh', sub {
75             buzz($blirp, $blurp)
76             });
77              
78             my $mc = memcached; # Exposes package global instance of Cache::Memcached
79             $mc->set_servers() # ... etc, but not recommended
80            
81             =head1 CAUTION
82              
83             This module is meant to provide convenience, and is hardcoded to localhost:11211. If you're
84             running a multi server memcached cluster, then this module is probably not for you.
85              
86             =head1 AUTHOR
87              
88             Stig Palmquist, C<< >>
89              
90             =head1 BUGS
91              
92             https://github.com/pqx/cache-memcached-sweet
93              
94              
95             =head1 LICENSE AND COPYRIGHT
96              
97             Copyright 2013 pqx Limited.
98              
99             This program is distributed under the MIT (X11) License:
100             L
101              
102             Permission is hereby granted, free of charge, to any person
103             obtaining a copy of this software and associated documentation
104             files (the "Software"), to deal in the Software without
105             restriction, including without limitation the rights to use,
106             copy, modify, merge, publish, distribute, sublicense, and/or sell
107             copies of the Software, and to permit persons to whom the
108             Software is furnished to do so, subject to the following
109             conditions:
110              
111             The above copyright notice and this permission notice shall be
112             included in all copies or substantial portions of the Software.
113              
114             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
115             EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
116             OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
117             NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
118             HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
119             WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
120             FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
121             OTHER DEALINGS IN THE SOFTWARE.
122              
123              
124             =cut
125              
126             42;