perldoc > Cache

๐Ÿ“› NAME

Cache - the Cache interface

๐Ÿš€ Quick Reference

Use CaseCommandDescription
๐Ÿ”ง Instantiate a file cacheCache::File->new( cache_root => '/tmp/cacheroot' )Create a new file-based cache object
๐Ÿ’พ Set data with expiry$cache->set( $key, $data, $expiry )Store data in the cache
๐Ÿ“‚ Get data$cache->get( $key )Retrieve data from the cache
๐Ÿ” Check existence$cache->exists( $key )Check if a key exists in the cache
๐Ÿ—‘๏ธ Remove data$cache->remove( $key )Remove a specific key from the cache
๐Ÿงน Purge expired$cache->purge()Remove all expired data from the cache
๐Ÿงน Clear all$cache->clear()Remove all entries from the cache
๐Ÿ”— Tie interfacetie %hash, 'Cache::File', { cache_root => $tempdir }Access cache as a hash

๐Ÿ“– 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.

The Cache interface is implemented by derived classes that store cached data in different manners (such as files on a filesystem, or in memory).

๐Ÿ’ก USAGE

To use the Cache system, a cache implementation must be chosen to suit your needs. The most common is Cache::File, which is suitable for sharing data between multiple invocations and even between concurrent processes.

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

  use Cache::File;

  my $cache = Cache::File->new( cache_root => '/tmp/cacheroot' );
  my $customer = $cache->get( $name );

  unless ($customer) {
      $customer = get_customer_from_db( $name );
      $cache->set( $name, $customer, '10 minutes' );
  }

  return $customer;

Of course, far more powerful methods are available for accessing cached data. Also see the TIE INTERFACE below.

โš™๏ธ METHODS

๐Ÿ”ง PROPERTIES

When a cache is constructed these properties can be supplied as options to the new() method.

โšก SHORTCUT METHODS

These methods all have counterparts in the Cache::Entry package, but are provided here as shortcuts. They all default to just wrappers that do '$c->entry($key)->method_name()'. For documentation, please refer to Cache::Entry.

๐Ÿ”— TIE INTERFACE

  tie %hash, 'Cache::File', { cache_root => $tempdir };

  $hash{'key'} = 'some data';
  $data = $hash{'key'};

The Cache classes can be used via the tie interface, as shown in the synopsis. This allows the cache to be accessed via a hash. All the standard methods for accessing the hash are supported , with the exception of the 'keys' or 'each' call.

The tie interface is especially useful with the load_callback to automatically populate the hash.

๐Ÿ—‘๏ธ REMOVAL STRATEGY METHODS

These methods are only for use internally (by concrete Cache implementations).

These methods define the interface by which the removal strategy object can manipulate the cache (the Cache is the 'context' of the strategy). By default, methods need to be provided to remove the oldest or stalest objects in the cache - thus allowing support for the default FIFO and LRU removal strategies. All derived Cache implementations should support these methods and may also introduce additional methods (and additional removal strategies to match).

๐Ÿ› ๏ธ UTILITY METHODS

These methods are only for use internally (by concrete Cache implementations).

๐Ÿ“š SEE ALSO

๐Ÿ†š DIFFERENCES FROM CACHE::CACHE

The Cache modules are a total redesign and reimplementation of Cache::Cache and thus not directly compatible. It would be, however, quite possible to write a wrapper module that provides an identical interface to Cache::Cache.

The semantics of use are very similar to Cache::Cache, with the following exceptions:

โœ๏ธ AUTHOR

Chris Leishman <chris AT leishman.org>
Based on work by DeWitt Clinton <dewitt AT unto.net>

ยฉ๏ธ COPYRIGHT

Copyright (C) 2003-2006 Chris Leishman. All Rights Reserved.

This module is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed or implied. This program is free software; you can redistribute or modify it under the same terms as Perl itself.

$Id: Cache.pm,v 1.7 2006/01/31 15:23:58 caleishm Exp $

Cache
๐Ÿ“› NAME ๐Ÿš€ Quick Reference ๐Ÿ“– DESCRIPTION ๐Ÿ’ก USAGE โš™๏ธ METHODS ๐Ÿ”ง PROPERTIES โšก SHORTCUT METHODS ๐Ÿ”— TIE INTERFACE ๐Ÿ—‘๏ธ REMOVAL STRATEGY METHODS ๐Ÿ› ๏ธ UTILITY METHODS ๐Ÿ“š SEE ALSO ๐Ÿ†š DIFFERENCES FROM CACHE::CACHE โœ๏ธ AUTHOR ยฉ๏ธ COPYRIGHT

Generated by phpman v4.9.26-5-g7740029 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-12 05:25 @2600:1f28:365:80b0:194e:56dd:d3a6:4135
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Valid XHTML 1.0 Transitional!Valid CSS!

^_top_^