Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

In Memory Cache Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Test-specific instantiation: Create a fresh 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.
  • Time-controlled testing: Combine with 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.
  • Environment-based driver swap: Wrap 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().
  • Mocking cache behavior in integration tests: In API or service-level tests, inject 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”).

Gotchas and Tips

  • No cross-request persistence: The cache lives only in the current PHP process—data is lost between HTTP requests or CLI invocations. Never use it for production state; reserve for tests, CLI scripts, or dev-time fallbacks.
  • Key flexibility has limits: While v1.2.0+ allows dashes and removes the 64-char limit, PSR-6 still expects at least alphanumeric + _.. If interoperability matters (e.g., dropping in Doctrine Cache later), avoid exotic keys like user:123, foo-bar`, or emojis.
  • Default clock is implicit but functional: Since v1.1.0, 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.
  • Memory leakage risk in long-running tests: In large test suites (especially 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.
  • It’s 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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport