cash/lrucache
Memory-based, non-persistent Least Recently Used (LRU) cache for PHP. Supports integer or string keys and any value types, with a fixed max size and automatic eviction of least-recently-used entries when capacity is exceeded.
file/array drivers for non-critical data.Cache::lru()->put('rate_limit_'.$ip, $count)).Illuminate\Contracts\Cache\Store.lru) in config/cache.php.LRUCache to an interface for testability.get/put calls/sec).| Risk | Impact | Mitigation |
|---|---|---|
| Data Loss | Non-persistent; cache clears on restart. | Pair with Redis/database for critical data (e.g., hybrid cache strategy). |
| Key Collisions | String keys like "7" auto-cast to integers. |
Enforce naming conventions (e.g., prefix numeric keys with _id_). |
| Memory Leaks | Unbounded growth if max_size is too large. |
Set conservative defaults (e.g., new LRUCache(1000)) and monitor usage. |
| Laravel Cache API Gap | Missing features like remember(), forever(), or tags. |
Create a decorator class to bridge missing functionality. |
| Testing Complexity | LRU eviction logic requires edge-case testing. | Write unit tests for eviction scenarios (e.g., put beyond capacity). |
flock).symfony/cache or Redis.file/array drivers with an lru driver for non-persistent data.Cache::store('lru')->get('key')).// app/Providers/AppServiceProvider.php
use Cash\LRUCache;
use Illuminate\Cache\Repository;
Cache::extend('lru', function ($app) {
$store = new LRUCacheAdapter(new LRUCache(config('cache.stores.lru.max_size')));
return new Repository($store);
});
config/cache.php:
'stores' => [
'lru' => [
'driver' => 'lru',
'max_size' => 1000, // Adjust based on memory constraints
],
],
Cache::remember() calls for non-critical data with Cache::store('lru').Cache::store('lru')->get('api_response_'.$url)).Cache::store('lru')->put('parsed_data_'.$id, $result)).Cache facade to support LRU-specific methods (e.g., Cache::lru()->get()).composer.json).Store interface.array driver).Cache::store('lru')->put() under high concurrency.memory_get_usage()).max_size based on real-world usage.Cache::store('lru')->stats()) to track usage.var_dump($cache->getKeys()) to inspect contents.Cache::store('lru')->onEvict(fn($key) => Log::debug($key))).user:123 vs. 123).memory_get_peak_usage().max_size cautiously (e.g., new LRUCache(10000)).How can I help you explore Laravel packages today?