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

Laminas Cache Storage Adapter Memory Laravel Package

laminas/laminas-cache-storage-adapter-memory

In-memory cache storage adapter for Laminas Cache. Provides a simple, fast, non-persistent cache for the current PHP process, useful for testing, prototyping, or per-request caching without external services.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install via Composer: composer require laminas/laminas-cache-storage-adapter-memory.
  2. Create the adapter instance programmatically (no config required):
    use Laminas\Cache\Storage\Adapter\Memory;
    $cache = new Memory();
    
  3. Use immediately for single-request caching:
    $cache->setItem('my_key', $data);
    if ($cache->hasItem('my_key')) {
        $value = $cache->getItem('my_key');
    }
    
  4. Primary use case: Test doubles or lightweight caching in CLI tools/commands where persistence across requests isn’t needed.

Implementation Patterns

  • As a test mock: Inject Memory into services during unit tests to verify caching behavior without side effects or external dependencies.
    $cache = new Memory();
    $service = new MyService($cache);
    $service->doWork(); // internally caches expensive computation
    $this->assertTrue($cache->hasItem('expensive_result'));
    
  • In Laravel applications (despite package being Laminas): Use via a custom binding or manually instantiate in a service provider for ephemeral caching (e.g., within job or command runs). Example for a console command:
    $memoryCache = new Memory();
    $result = $memoryCache->hasItem('user_counts') ? $memoryCache->getItem('user_counts') : $this->computeUserCounts($memoryCache);
    
  • As a drop-in replacement for filesystem/network caches during local development—swap adapters in config without changing application code, thanks to Laminas\Cache\Storage\StorageInterface.

Gotchas and Tips

  • ⚠️ Data is not persistent: Values vanish at the end of the request/CLI run. Do not use for session data, shared state, or anything requiring cross-request reliability.
  • ⚠️ Memory leaks: Because it stores everything in PHP’s request memory, large caches or unbounded growth (e.g., set() without expiry or remove()) may exhaust memory in long-running scripts (e.g., Swoole, ReactPHP).
  • No TTL support: Unlike filesystem or Redis adapters, this adapter ignores expiration times. Keys remain forever within the request unless explicitly removed/cleared.
  • ✅ Debugging tip: Call $cache->getOptions()->toArray() to inspect internal stats (e.g., item count, memory usage) during development.
  • ✅ Extensibility: If needing custom behavior (e.g., size limits), extend Memory and override protected methods like internalSetItem(), but be cautious—Laminas’ internal caching expects strict interface conformance.
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