react/cache
ReactPHP async cache component with Promise-based CacheInterface and an in-memory ArrayCache. Inspired by PSR-16 but designed for non-blocking apps. Supports get/set/delete, bulk operations, clear, has, and common fallback patterns.
Start by installing the package via Composer (composer require react/cache:^3@dev) and using the provided ArrayCache implementation for simple in-memory caching. The core concept is the CacheInterface, which defines async, promise-returning methods like get(), set(), delete(), and their batch variants (getMultiple(), setMultiple(), deleteMultiple()). Begin with basic usage—fetching cached data with fallback logic (e.g., from DB on cache miss)—using promise chaining or async/await (in PHP 8+ with async libraries like amphp/parallel-functions). The README’s "Common usage" section provides immediate patterns: fallback get and fallback get-and-set are the most common first integrations.
get() followed by conditional fallback to a data source. Chain promises to populate cache automatically on cache miss (set() after get() yields null).getMultiple() and setMultiple() for efficient retrieval/persistence of grouped keys—especially useful when hydrating DTOs, user preferences, or configuration slices.ArrayCache($limit) for short-lived, per-request or per-worker caches (e.g., within CLI workers or long-running ReactPHP servers), with automatic eviction of least-recently-used items.react/cache to wrap database or external API calls, avoiding blocking while still supporting caching semantics.CacheInterface into services to decouple cache storage—swap in-memory for Redis/ Memcached later (via community implementations like reactphp/redis-cache or custom wrappers).ArrayCache uses wall-clock time for TTLs, making it prone to premature expiration if the system clock jumps (e.g., via NTP). Prefer PHP ≥ 7.3 for reliable TTLs, or avoid relying on sub-second precision TTLs.has() is Not Safe for Conditional Logic: As noted in the docs, has() can return true just before another process deletes the key—use get() + set() patterns instead for atomic operations.get() resolves with the $default value on cache miss or error, but network-backed implementations may reject promises (e.g., Redis connection down). Always add ->otherwise() or try/catch if wrapping with async utilities.getMultiple() receives iterable<string>, and setMultiple() receives iterable<string,mixed>—PHP 8+ union types help, but legacy code may need careful normalization.delete()/deleteMultiple(): Unlike set(), deletion doesn’t accept a TTL (because deletion is immediate).@dev (last release Nov 2022). Consider v1 (^1.2) for production stability until v3 stabilizes. No direct dependents means ecosystem adoption is niche (ReactPHP-focused).How can I help you explore Laravel packages today?