]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perlmods/OpenSRF/Utils/Cache.pm
Initial revision
[OpenSRF.git] / 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 sub current { 
29
30         my ( $class, $c_type )  = @_;
31         return undef unless $c_type;
32
33         return $caches{$c_type} if exists $caches{$c_type};
34         return $caches{$c_type} = $class->new( $c_type );
35
36 }
37
38
39 sub new {
40
41         my( $class, $c_type ) = @_;
42         return undef unless $c_type;
43
44         return $caches{$c_type} if exists $caches{$c_type};
45
46         $class = ref( $class ) || $class;
47
48         my $config = OpenSRF::Utils::Config->current;
49         my $cache_servers = $config->mem_cache->$c_type;
50
51         my $instance = Cache::Memcached->new( { servers => $cache_servers } ); 
52
53         return bless( $instance, $class );
54
55 }
56
57
58 1;
59
60
61
62
63