Perl Cache

The Perl 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.

Cache::Cache Versus 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 attempts to address
limitations in the Cache::Cache implementation.

Getting Started

The first thing you will need is a copy of the Cache::Cache
modules. The most recent version is 1.05 and can be found
here:

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.