1Cache::Cache(3) User Contributed Perl Documentation Cache::Cache(3)
2
3
4
6 Cache::Cache -- the Cache interface.
7
9 The Cache modules are designed to assist a developer in persisting data
10 for a specified period of time. Often these modules are used in web
11 applications to store data locally to save repeated and redundant
12 expensive calls to remote machines or databases. People have also been
13 known to use Cache::Cache for its straightforward interface in sharing
14 data between runs of an application or invocations of a CGI-style
15 script or simply as an easy to use abstraction of the filesystem or
16 shared memory.
17
18 The Cache::Cache interface is implemented by classes that support the
19 get, set, remove, size, purge, and clear instance methods and their
20 corresponding static methods for persisting data across method calls.
21
23 Cache::Cache is in wide use and very stable, but has not changed in
24 years and is no longer actively developed.
25
26 CHI is the successor to Cache::Cache. It adheres to the basic
27 Cache::Cache API but adds new features and drivers (e.g. FastMmap and
28 Memcached), improves performance, and addresses limitations in the
29 Cache::Cache implementation. The authors recommend the use of CHI going
30 forward.
31
32 Questions about Cache::Cache and CHI may be directed to the perl-cache
33 mailing list at http://groups.google.com/group/perl-cache-discuss.
34
36 First, choose the best type of cache implementation for your needs.
37 The simplest cache is the MemoryCache, which is suitable for
38 applications that are serving multiple sequential requests, and wish to
39 avoid making redundant expensive queries, such as an Apache/mod_perl
40 application talking to a database. If you wish to share that data
41 between processes, then perhaps the SharedMemoryCache is appropriate,
42 although its behavior is tightly bound to the underlying IPC mechanism,
43 which varies from system to system, and is unsuitable for large objects
44 or large numbers of objects. When the SharedMemoryCache is not
45 acceptable, then FileCache offers all of the same functionality with
46 similar performance metrics, and it is not limited in terms of the
47 number of objects or their size. If you wish to maintain a strict
48 limit on the size of a file system based cache, then the
49 SizeAwareFileCache is the way to go. Similarly, the
50 SizeAwareMemoryCache and the SizeAwareSharedMemoryCache add size
51 management functionality to the MemoryCache and SharedMemoryCache
52 classes respectively.
53
54 Using a cache is simple. Here is some sample code for instantiating
55 and using a file system based cache.
56
57 use Cache::FileCache;
58
59 my $cache = new Cache::FileCache( );
60
61 my $customer = $cache->get( $name );
62
63 if ( not defined $customer )
64 {
65 $customer = get_customer_from_db( $name );
66 $cache->set( $name, $customer, "10 minutes" );
67 }
68
69 return $customer;
70
72 $EXPIRES_NEVER
73 The item being set in the cache will never expire.
74
75 $EXPIRES_NOW
76 The item being set in the cache will expire immediately.
77
79 Clear( )
80 Remove all objects from all caches of this type.
81
82 Purge( )
83 Remove all objects that have expired from all caches of this type.
84
85 Size( )
86 Returns the total size of all objects in all caches of this type.
87
88 new( $options_hash_ref )
89 Construct a new instance of a Cache::Cache. $options_hash_ref is a
90 reference to a hash containing configuration options; see the
91 section OPTIONS below.
92
93 clear( )
94 Remove all objects from the namespace associated with this cache
95 instance.
96
97 get( $key )
98 Returns the data associated with $key.
99
100 get_object( $key )
101 Returns the underlying Cache::Object object used to store the
102 cached data associated with $key. This will not trigger a removal
103 of the cached object even if the object has expired.
104
105 purge( )
106 Remove all objects that have expired from the namespace associated
107 with this cache instance.
108
109 remove( $key )
110 Delete the data associated with the $key from the cache.
111
112 set( $key, $data, [$expires_in] )
113 Associates $data with $key in the cache. $expires_in indicates the
114 time in seconds until this data should be erased, or the constant
115 $EXPIRES_NOW, or the constant $EXPIRES_NEVER. Defaults to
116 $EXPIRES_NEVER. This variable can also be in the extended format
117 of "[number] [unit]", e.g., "10 minutes". The valid units are s,
118 second, seconds, sec, m, minute, minutes, min, h, hour, hours, d,
119 day, days, w, week, weeks, M, month, months, y, year, and years.
120 Additionally, $EXPIRES_NOW can be represented as "now" and
121 $EXPIRES_NEVER can be represented as "never".
122
123 set_object( $key, $object )
124 Associates $key with Cache::Object $object. Using set_object (as
125 opposed to set) does not trigger an automatic removal of expired
126 objects.
127
128 size( )
129 Returns the total size of all objects in the namespace associated
130 with this cache instance.
131
132 get_namespaces( )
133 Returns all the namespaces associated with this type of cache.
134
136 The options are set by passing in a reference to a hash containing any
137 of the following keys:
138
139 namespace
140 The namespace associated with this cache. Defaults to "Default" if
141 not explicitly set.
142
143 default_expires_in
144 The default expiration time for objects place in the cache.
145 Defaults to $EXPIRES_NEVER if not explicitly set.
146
147 auto_purge_interval
148 Sets the auto purge interval. If this option is set to a
149 particular time ( in the same format as the expires_in ), then the
150 purge( ) routine will be called during the first set after the
151 interval expires. The interval will then be reset.
152
153 auto_purge_on_set
154 If this option is true, then the auto purge interval routine will
155 be checked on every set.
156
157 auto_purge_on_get
158 If this option is true, then the auto purge interval routine will
159 be checked on every get.
160
162 (get|set)_namespace( )
163 The namespace of this cache instance
164
165 get_default_expires_in( )
166 The default expiration time for objects placed in this cache
167 instance
168
169 get_keys( )
170 The list of keys specifying objects in the namespace associated
171 with this cache instance
172
173 get_identifiers( )
174 This method has been deprecated in favor of get_keys( ).
175
176 (get|set)_auto_purge_interval( )
177 Accesses the auto purge interval. If this option is set to a
178 particular time ( in the same format as the expires_in ), then the
179 purge( ) routine will be called during the first get after the
180 interval expires. The interval will then be reset.
181
182 (get|set)_auto_purge_on_set( )
183 If this property is true, then the auto purge interval routine will
184 be checked on every set.
185
186 (get|set)_auto_purge_on_get( )
187 If this property is true, then the auto purge interval routine will
188 be checked on every get.
189
191 CHI - the successor to Cache::Cache
192
193 Cache::Object, Cache::MemoryCache, Cache::FileCache,
194 Cache::SharedMemoryCache, and Cache::SizeAwareFileCache
195
197 Original author: DeWitt Clinton <dewitt@unto.net>
198
199 Last author: $Author: dclinton $
200
201 Copyright (C) 2001-2003 DeWitt Clinton
202
203
204
205perl v5.36.0 2022-07-22 Cache::Cache(3)