# Cache


Namespace Chevere\Cache

The Cache package provides a filesystem-based cache for PHP. Its purpose is to enable a richer abstraction around file returns which are cached with OPcache (opens new window). It is intended to be used to cache and accelerate application state.

# Installing

Cache is available through Packagist (opens new window) and the repository source is at chevere/cache (opens new window).

composer require chevere/cache

# Creating Cache

Create a Cache repository by passing the cache working directory. The system will read and write cache from files stored in this directory.

use Chevere\Cache\Cache;
use function Chevere\Filesystem\directoryForPath;

$directory = directoryForPath(__DIR__);
$cache = new Cache($directory);

# With Put

The withPut method is used to cache PHP storable variable.

use Chevere\Cache\Cache;
use Chevere\VariableSupport\StorableVariable;

$storable = new StorableVariable($var);
$cache = $cache->withPut(
    key: $storable,

# Puts

The puts method provides access to an array containing details about With Put operations on the Cache repository.

$array = $cache->puts();

# Exists

The exists method is used to determine if cache exists for $key.

$boolean = $cache->exists($key);

# Get Item

The get method is used to retrieve a cached Item at the given $key.

$item = $cache->get($key);

# Get Item variable

The get method returns the PHP variable for the Cache Item.

$mixed = $item->get();

# Get Item variable typed

The get<Type> methods does the same as get, but it type-hint the return value.

$array => $item->getArray();
$boolean => $item->getBoolean();
$float => $item->getFloat();
$integer => $item->getInteger();
$object => $item->getObject();
$string => $item->getString();

# With Remove

The withRemove method is used to remove a Item from the Cache repository at the given $key.

$cache = $cache->withRemove($key);