perldoc > Cache::Cache

πŸ“– NAME

Cache::Cache β€” the Cache interface.

πŸš€ Quick Reference

Use CaseCommandDescription
Create a cache instancemy $cache = new Cache::FileCache( )Instantiate a file-based cache with default options
Store data with expiration$cache->set( $key, $data, "10 minutes" )Cache data with a 10‑minute timeout
Retrieve cached datamy $val = $cache->get( $key )Get the value associated with $key
Remove a single key$cache->remove( $key )Delete the data for $key
Purge expired objects$cache->purge( )Remove all expired entries from the cache
Clear entire cache$cache->clear( )Remove all objects in the cache namespace

πŸ“ DESCRIPTION

The Cache modules are designed to assist a developer in persisting data for a specified period of time. Often these modules are used in web applications to store data locally to save repeated and redundant expensive calls to remote machines or databases. People have also been known to use Cache::Cache for its straightforward interface in sharing data between runs of an application or invocations of a CGI-style script or simply as an easy to use abstraction of the filesystem or shared memory.

The Cache::Cache interface is implemented by classes that support the get, set, remove, size, purge, and clear instance methods and their corresponding static methods for persisting data across method calls.

πŸ”„ Cache::Cache vs CHI

Cache::Cache is in wide use and very stable, but has not changed in years and is no longer actively developed.

CHI is the successor to Cache::Cache. It adheres to the basic Cache::Cache API but adds new features and drivers (e.g. FastMmap and Memcached), improves performance, and addresses limitations in the Cache::Cache implementation. The authors recommend the use of CHI going forward.

Questions about Cache::Cache and CHI may be directed to the perl-cache mailing list at http://groups.google.com/group/perl-cache-discuss.

πŸ”§ USAGE

First, choose the best type of cache implementation for your needs. The simplest cache is the MemoryCache, which is suitable for applications that are serving multiple sequential requests, and wish to avoid making redundant expensive queries, such as an Apache/mod_perl application talking to a database. If you wish to share that data between processes, then perhaps the SharedMemoryCache is appropriate, although its behavior is tightly bound to the underlying IPC mechanism, which varies from system to system, and is unsuitable for large objects or large numbers of objects. When the SharedMemoryCache is not acceptable, then FileCache offers all of the same functionality with similar performance metrics, and it is not limited in terms of the number of objects or their size. If you wish to maintain a strict limit on the size of a file system based cache, then the SizeAwareFileCache is the way to go. Similarly, the SizeAwareMemoryCache and the SizeAwareSharedMemoryCache add size management functionality to the MemoryCache and SharedMemoryCache classes respectively.

Using a cache is simple. Here is some sample code for instantiating and using a file system based cache.

  use Cache::FileCache;

  my $cache = new Cache::FileCache( );

  my $customer = $cache->get( $name );

  if ( not defined $customer )
  {
    $customer = get_customer_from_db( $name );
    $cache->set( $name, $customer, "10 minutes" );
  }

  return $customer;

πŸ“Œ CONSTANTS

βš™οΈ METHODS

🧹 Clear( )

Remove all objects from all caches of this type.

🧹 Purge( )

Remove all objects that have expired from all caches of this type.

πŸ“ Size( )

Returns the total size of all objects in all caches of this type.

πŸ†• new( $options_hash_ref )

Construct a new instance of a Cache::Cache. $options_hash_ref is a reference to a hash containing configuration options; see the section OPTIONS below.

🧹 clear( )

Remove all objects from the namespace associated with this cache instance.

πŸ” get( $key )

Returns the data associated with $key.

πŸ” get_object( $key )

Returns the underlying Cache::Object object used to store the cached data associated with $key. This will not trigger a removal of the cached object even if the object has expired.

🧹 purge( )

Remove all objects that have expired from the namespace associated with this cache instance.

❌ remove( $key )

Delete the data associated with the $key from the cache.

πŸ’Ύ set( $key, $data, [$expires_in] )

Associates $data with $key in the cache. $expires_in indicates the time in seconds until this data should be erased, or the constant $EXPIRES_NOW, or the constant $EXPIRES_NEVER. Defaults to $EXPIRES_NEVER. This variable can also be in the extended format of "[number] [unit]", e.g., "10 minutes". The valid units are s, second, seconds, sec, m, minute, minutes, min, h, hour, hours, d, day, days, w, week, weeks, M, month, months, y, year, and years. Additionally, $EXPIRES_NOW can be represented as "now" and $EXPIRES_NEVER can be represented as "never".

πŸ’Ύ set_object( $key, $object )

Associates $key with Cache::Object $object. Using set_object (as opposed to set) does not trigger an automatic removal of expired objects.

πŸ“ size( )

Returns the total size of all objects in the namespace associated with this cache instance.

πŸ“‹ get_namespaces( )

Returns all the namespaces associated with this type of cache.

βš™οΈ OPTIONS

The options are set by passing in a reference to a hash containing any of the following keys:

πŸ“‹ PROPERTIES

πŸ“š SEE ALSO

πŸ‘€ AUTHOR

Original author: DeWitt Clinton <dewitt AT unto.net>

Last author: $Author: dclinton $

Copyright (C) 2001-2003 DeWitt Clinton

Cache::Cache
πŸ“– NAME πŸš€ Quick Reference πŸ“ DESCRIPTION
πŸ”„ Cache::Cache vs CHI
πŸ”§ USAGE πŸ“Œ CONSTANTS βš™οΈ METHODS
🧹 Clear( ) 🧹 Purge( ) πŸ“ Size( ) πŸ†• new( $options_hash_ref ) 🧹 clear( ) πŸ” get( $key ) πŸ” get_object( $key ) 🧹 purge( ) ❌ remove( $key ) πŸ’Ύ set( $key, $data, [$expires_in] ) πŸ’Ύ set_object( $key, $object ) πŸ“ size( ) πŸ“‹ get_namespaces( )
βš™οΈ OPTIONS πŸ“‹ PROPERTIES πŸ“š SEE ALSO πŸ‘€ AUTHOR

Generated by phpman v4.9.26-5-g7740029 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-24 17:36 @216.73.217.23
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!

^_top_^