amphp/cache
Non-blocking cache library for Amp-based PHP apps. Provides async cache interfaces and adapters (e.g., in-memory, filesystem, Redis) with TTL support, atomic operations, and PSR-style ergonomics for high-concurrency services.
To begin using amphp/cache, first install it via Composer: composer require amphp/cache. Since it's built for asynchronous PHP using Amp and Revolt fibers, ensure your environment supports async execution (e.g., using Revolt’s event loop or Amp’s runner). Start by creating a cache instance:
use Amp\Cache\MemoryCache;
$cache = new MemoryCache(); // or FileCache, RedisCache, etc.
A common first use case is caching the result of an async operation:
$result = $cache->get('user:123', function () {
return $this->fetchUserFromDatabaseAsync(123);
});
Review the README.md and the src/ directory’s interface definitions (CacheInterface, TaggedCacheInterface) to understand the core contracts.
TaggedCache to invalidate related entries by tag:
$tagged = new TaggedCache($cache);
$tagged->set('post:42', $content, ['posts', 'drafts']);
$tagged->invalidateTags(['drafts']); // invalidates all posts with draft tag
get($key, $resolver)—the resolver (callable returning a coroutine) only executes on a miss.$response = $cache->get('api:external', fn() => $httpClient->request('GET', $url));
Amp\Cache\Redis\RedisCache) without changing client code, thanks to the unified interface.Amp\Promise\cache(), remember it only caches resolved values—use CacheInterface::get() for lazy async resolution.Serializable or JsonSerializable). Non-serializable resources (e.g., streams) will fail silently or throw.Cache by extending AbstractCache or decorating with ProxyCache for metrics/logging (e.g., track hit/miss ratios).SETNX or file locks for distributed environments.How can I help you explore Laravel packages today?