Simple PHP caching library -- by, Chris Kankiewicz (@PHLAK)
Stash is a simple PHP caching library supporting multiple, interchangeable caching back-ends and an expressive (Laravel inspired) API.
Supported caching back-ends:
- File Backed
- Memcached
- Redis
- APCu
- Ephemeral
Like this project? Keep me caffeinated by making a donation.
- PHP >= 5.4
composer require phlak/stashFirst, import Stash:
use PHLAK\Stash;Then instantiate Stash for your back-end of choice with Stash\Cache::make().
$stash = Stash\Cache::make($driver, $config);
The make() method takes two parameters. The first ($driver) should be a
string representing your desired caching driver.
apcu- PHP's native APC User Cache.ephemeral- A transient, in-memory array that only exists for the lifetime of the script.file- File-based caching. Stores cache items as files in a directory on disk.memcached- High-performance, distributed memory object caching system.redis- In-memory data structure store.
The second parameter ($config) accepts a driver-specific closure
for setting configuration options for the chosen driver. Refer to the specific
documentation for each driver below for more info. Some drivers do not require
a config function.
Alternatively you may use one of the named constructors to initialize the cache driver.
Stash\Cache::file($config);
Stash\Cache::memcached($config);
Stash\Cache::redis($config);
Stash\Cache::apcu($config);
Stash\Cache::ephemeral();
The file cache configuration closure must return an array that contains a key
of dir with a string value of a valid directory path in which your cache files
will be stored.
$stash = Stash\Cache::file(function () {
return [
'dir' => 'path/to/cache',
// 'prefix' => 'some_prefix'
];
});The Memcached configuration closure must return an instance of Memcached. The
configuration closure receives an instance of the Memcached object as it's only
parameter, you can use this parameter to connect and configure Memcached. At a
minimum you must connect to one or more Memcached server via the addServer()
or addServers() methods.
Reference the PHP Memcached documentation for additional configuration options.
$stash = Stash\Cache::memcached(function ($memcached) {
$memcached->addServer('localhost', 11211);
// $memcached->setOption(Memcached::OPT_PREFIX_KEY, 'some_prefix');
return $memcached; // Must return the $memcached object
});The Redis configuration closure must return an instance of Redis. The
configuration closure receives an instance of the Redis object as it's only
parameter, you can use this parameter connect to and configure Redis. At a
minimum you must connect to one or more Redis server via the connect() or
pconnect() methods.
Reference the phpredis documentation for additional configuration options.
$stash = Stash\Cache::redis(function ($redis) {
$redis->pconnect('localhost', 6379);
// $redis->setOption(Redis::OPT_PREFIX, 'some_prefix');
return $redis; // Must return the $redis object
});The APCu driver caches items in PHPs APC user cache.
$stash = Stash\Cache::apcu();The APCu driver does not require a configuration closure. However, if you
wish to set a prefix you can pass a configuration closure that returns an array.
The returned array must contain a key of prefix with a string value of the
desired prefix.
$stash = Stash\Cache::apcu(function () {
return [
'prefix' => 'some_prefix'
];
});The Ephemeral driver caches items in a PHP array that exists in memory only for the lifetime of the script. The Ephemeral driver does not take a configuration closure.
$stash = Stash\Cache::ephemeral();Add an item to the cache for a specified duration:
$stash->put($key, $data, $minutes = 0);Add an item to the cache permanently:
$stash->forever($key, $data);Retrieve an item from the cache:
$stash->get($key, $default = false);Check if an item exists in the cache:
$stash->has($key);Retrieve item from cache or, when item does not exist, execute a closure. The result of the closure is then stored in the cache for the specified duration and returned for immediate use.
$stash->remember($key, $minutes, function() {
// return something
});or remember permanently:
$stash->rememberForever($key, function() {
// return something
});Increment an integer:
$stash->increment($key, $value = 1);Decrement an integer:
$stash->decrement($key, $value = 1);Remove an item from the cache:
$stash->forget($key);Delete all items from the cache:
$stash->flush();A list of changes can be found on the GitHub Releases page.
Please report bugs to the GitHub Issue Tracker.
This project is liscensed under the MIT License.