psr/simple-cache
PSR-16 Simple Cache interfaces from PHP-FIG. Defines a common API for cache implementations (get/set/delete, TTL, etc.) but provides no caching itself. Use this package to type-hint against the standard and pair with any compatible cache provider.
Start by requiring the interface package: composer require psr/simple-cache. This installs only the PSR-16 interface definitions—no caching backend. To get working caching, install a concrete implementation (e.g., symfony/cache with CacheItemPoolAdapter, league/cache, or use Laravel’s built-in adapter: cache()->driver() is PSR-16 compatible from Laravel 8.40+). Your first real use case will be injecting Psr\SimpleCache\CacheInterface into a service to cache lightweight results (e.g., a slow DB count or third-party API response) using get()/set().
CacheInterface via DI—e.g., public function __construct(CacheInterface $cache)—and use it directly in domain services, avoiding framework-specific Cache::remember() calls for portable code.$result = $cache->get("user_{$id}_stats", fn() => $this->expensiveStats($id), 300);
(Note: TTL support depends on the underlying implementation; some accept int $ttl as third arg even if not in PSR-16 spec)$key = 'dashboard_' . md5($request->user()->id);
if ($cache->has($key)) {
return response()->setContent($cache->get($key));
}
// ... render, store, return
ArrayCache (from _aryie/array-cache) in phpunit.xml or test containers to avoid filesystem/network side effects.CacheInterface—to maximize adoption.$ttl in set(). While many implementations (like Symfony’s adapter) accept set($key, $value, $ttl), it’s non-portable. For strict portability, use implementation-specific extensions or pass TTL via context (e.g., get($key, null, ['ttl' => 300]))—but this is not standardized.null is a valid cached value, get($key)’s default behavior returns null on miss—leading to false positives. Always use has($key) first or specify a unique sentinel as $default (e.g., get($key, self::CACHE_MISS_SENTINEL)).^2.0 (composer require psr/simple-cache:^2.0). Conflicts with newer implementations often break silently.getMultiple() over looping get()—e.g., getMultiple(['config_a', 'config_b'])—to avoid cache round-trips.symfony/cache may throw InvalidArgumentException for non-string keys or serialize()-unsafe values. Always ensure keys are strings/scalars and values are serializable.How can I help you explore Laravel packages today?