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