]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/perlmods/OpenSRF/Utils/Cache.pm
altered the cache a little to allow for the servers to be passed in
[Evergreen.git] / OpenSRF / src / perlmods / OpenSRF / Utils / Cache.pm
1 package OpenSRF::Utils::Cache;
2 use strict; use warnings;
3 use base qw/Cache::Memcached OpenSRF/;
4 use Cache::Memcached;
5 use OpenSRF::Utils::Config;
6
7
8 =head OpenSRF::Utils::Cache
9
10 This class just subclasses Cache::Memcached.
11 see Cache::Memcached for more options.
12
13 The value passed to the call to current is the cache type
14 you wish to access.  The below example sets/gets data
15 from the 'user' cache.
16
17 my $cache = OpenSRF::Utils::Cache->current("user");
18 $cache->set( "key1", "value1" [, $expire_secs ] );
19 my $val = $cache->get( "key1" );
20
21
22 =cut
23
24 sub DESTROY {}
25 my %caches;
26
27
28 # return a named cache if it exists
29 sub current { 
30         my ( $class, $c_type )  = @_;
31         return undef unless $c_type;
32         return $caches{$c_type} if exists $caches{$c_type};
33         return $caches{$c_type} = $class->new( $c_type );
34 }
35
36
37 # create a new named memcache object.
38 sub new {
39
40         my( $class, $cache_type, $servers ) = @_;
41         return undef unless $cache_type;
42
43         return $caches{$cache_type} if exists $caches{$cache_type};
44
45         $class = ref( $class ) || $class;
46         my $instance = Cache::Memcached->new( { servers => $servers } ); 
47         $caches{$cache_type} = $instance;
48         return bless( $instance, $class );
49 }
50
51
52 1;
53
54
55
56
57