tedivm/stash
Stash is a PHP caching library for speeding up expensive code (DB queries, API calls) by storing results in a cache. It provides Pool and Item APIs with pluggable storage Drivers, including composite setups and maintenance tools like purge.
Begin by installing Stash via Composer (composer require tedivm/stash). The library follows PSR-6, so start with the Pool class and a driver like FileSystem for local development:
use Stash\Pool;
use Stash\Driver\FileSystem;
$pool = new Pool(new FileSystem(['path' => __DIR__ . '/cache']));
$item = $pool->getItem('my_key');
$item->set('Hello, cache!');
$item->expiresAfter(3600); // 1 hour
$pool->save($item);
// Later...
if ($cached = $pool->getItem('my_key')->get()) {
echo $cached; // "Hello, cache!"
}
The official docs at stashphp.com provide deeper examples—start with the "Quick Start" and "Keys" sections to grasp key hierarchy and cache invalidation patterns.
$redisDriver = new Stash\Driver\Redis(['servers' => [['host' => '127.0.0.1', 'port' => 6379]]]);
$fsDriver = new Stash\Driver\FileSystem(['path' => '/var/cache/stash']);
$composite = new Stash\Driver\Composite([$redisDriver, $fsDriver]);
$pool = new Pool($composite);
'users/profiles/123'), then call $pool->invalidate('users/profiles') to clear all user profiles at once.use Stash\Driver\Redis;
use Stash\Pool;
use Stash\Driver\Session;
$pool = new Pool(new Redis([...]));
Session::registerHandler($pool);
Item::extend($ttl) to keep volatile caches alive during extended operations (e.g., API responses during heavy processing).FileSystem’s path config and avoid deeply nested keys. Changelog notes fixes for this (e.g., v0.11.5).'username' => '...' in driver options—don’t overlook this for managed services (e.g., AWS ElastiCache).?DateTime or ?int returns like Item::getExpiration().invalidate() removes items from all drivers (Composite-aware), while clear() only flushes the current driver. Prefer invalidate() for multi-tier setups.$pool->setLogger($logger) to debug cache misses and track hit rates. Logging propagates to all Item instances derived from the pool.FileSystem or Redis in production.How can I help you explore Laravel packages today?