beste/in-memory-cache
PSR-6 compliant in-memory cache for PHP—ideal as a lightweight default cache and for fast, predictable tests. Supports standard CacheItemPoolInterface behavior and optional PSR-20 clocks (e.g., frozen clocks) for time-based expiry testing.
Install the package via Composer (composer require beste/in-memory-cache) and start using it immediately—no configuration required. It implements Psr\Cache\CacheItemPoolInterface, so it works out of the box with any PSR-6–aware code. The simplest use case is unit testing: replace Redis, Doctrine Cache, or other persistent drivers with InMemoryCache to ensure fast, isolated, deterministic tests. For TTL-sensitive logic, inject a beste/clock\FrozenClock to control time explicitly. Begin by instantiating new InMemoryCache() or new InMemoryCache($clock) in your tests or dev environment.
InMemoryCache instance per test (e.g., via setUp() in PHPUnit or before() in Pest) to guarantee state isolation—critical when asserting hit/miss behavior or expiration.beste/clock\FrozenClock (or a custom PSR-20 clock) to simulate cache expiration without real delays. Set time to just before expiry, assert hit; advance time past TTL, assert miss. This is essential for validating fallback logic, middleware caching, or rate-limiting.InMemoryCache in a custom CacheInterface (e.g., App\Contracts\Cache) and swap implementations via Laravel’s config (e.g., use InMemoryCache in testing and Redis in production). Laravel’s CacheManager respects PSR-6, so you can even register it as a custom driver via Cache::extend().InMemoryCache instead of external services to avoid flaky network calls or DB fixtures. Ideal for verifying cache-first patterns (e.g., “fetch from cache or compute + store”)._.. If interoperability matters (e.g., dropping in Doctrine Cache later), avoid exotic keys like user:123, foo-bar`, or emojis.new InMemoryCache() uses PHP’s built-in SystemClock. Fine for dev—but always inject a frozen clock in tests. Missing this causes non-deterministic failures when TTLs are close to real time.pest with --parallel or Behat), in-memory items accumulate. Call $cache->clear() in after() or manually delete items post-test to avoid OOM in CLI runs.final—compose, don’t extend: The class is final, so you can’t subclass. Use decorators (e.g., LoggingCache wrapping InMemoryCache) or Laravel’s Cache::extend() to add metrics, logging, or validation. This keeps your tests clean while preserving encapsulation.How can I help you explore Laravel packages today?