# perldoc > Cache

---
type: CommandReference
command: Cache
mode: perldoc
section: 
source: perldoc
---

## Quick Reference

- `$cache->get($key)` — retrieve data from cache
- `$cache->set($key, $data, [$expiry])` — store data with optional expiry
- `$cache->entry($key)` — get a `Cache::Entry` object for the key
- `$cache->remove($key)` — remove entry from cache
- `$cache->purge()` — remove expired entries
- `$cache->clear()` — remove all entries
- `$cache->count()` — number of entries in cache
- `$cache->size()` — size of cache in bytes

## Name

Cache — the Cache interface for persisting data for a specified period.

## Synopsis

perl
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;
Tie interface:

perl
tie %hash, 'Cache::File', { cache_root => $tempdir };
$hash{'key'} = 'some data';
$data = $hash{'key'};
## Options (Properties passed to `new()`)

- `default_expires` — default expiry time for new entries. Get/set via `$c->default_expires()` and `$c->set_default_expires($expiry)`.
- `removal_strategy` — strategy object for cache size limit enforcement. Either a package name (e.g., `Cache::RemovalStrategy::LRU`) or a blessed object.
- `size_limit` — maximum cache size in bytes. Get/set via `$c->size_limit()`.
- `load_callback` — function called when `get` misses. Set via `$c->set_load_callback($callback)`.
- `validate_callback` — function called to validate cached data. Set via `$c->set_validate_callback($callback)`.

## Examples

See [Synopsis](#synopsis) for basic usage.

## Methods

### Core Methods

- `$c->entry($key)` — return a `Cache::Entry` object for the given key.
- `$c->purge()` — remove all expired data.
- `$c->clear()` — remove all entries regardless of expiry.
- `$c->count()` — number of entries.
- `$c->size()` — size in bytes.

### Shortcut Methods (delegate to `Cache::Entry`)

- `$c->exists($key)` — check if key exists.
- `$c->set($key, $data, [$expiry])` — store data.
- `$c->get($key)` — retrieve data.
- `$c->remove($key)` — delete entry.
- `$c->expiry($key)` — get expiry time.
- `$c->set_expiry($key, $time)` — set expiry.
- `$c->handle($key, [$mode, [$expiry]])` — get filehandle for entry.
- `$c->validity($key)` — get validity data.
- `$c->set_validity($key, $data)` — set validity.
- `$c->freeze($key, $data, [$expiry])` — store serialized data.
- `$c->thaw($key)` — retrieve and deserialize data.

### Removal Strategy Methods (for internal use by implementations)

- `$c->remove_oldest()` — remove oldest entry, return its size.
- `$c->remove_stalest()` — remove least used entry, return its size.
- `$c->check_size($size)` — call by implementations on size increase; invokes removal strategy if limit exceeded.

### Utility Methods

- `Cache::Canonicalize_Expiration_Time($timespec)` — convert a timespec to Unix time.

## Description

The Cache modules persist data for a specified time, often used in web applications to avoid redundant expensive calls. The base interface is implemented by derived classes (e.g., `Cache::File`, `Cache::Memory`). The tie interface allows hash access to the cache (except `keys`/`each`).

The Cache modules are a redesign of `Cache::Cache`. Key differences:
- `get`/`set` do **not** serialize complex data; use `freeze`/`thaw` instead.
- `get_object`/`set_object` replaced by `entry` method and `Cache::Entry`.
- No namespace concept in base interface; implementations may provide it.
- Purging is automatic; no explicit enable.
- `Cache::File` no longer supports separate masks for entries and directories.

## See Also

- [Cache::Entry](https://metacpan.org/pod/Cache::Entry)
- [Cache::File](https://metacpan.org/pod/Cache::File)
- [Cache::RemovalStrategy](https://metacpan.org/pod/Cache::RemovalStrategy)
- [Cache::RemovalStrategy::LRU](https://metacpan.org/pod/Cache::RemovalStrategy::LRU)
- [Cache::RemovalStrategy::FIFO](https://metacpan.org/pod/Cache::RemovalStrategy::FIFO)