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.
Performance Optimization for High-Frequency Data:
Implement cash/lrucache to reduce database/API calls for transient, read-heavy data (e.g., user sessions, rate-limiting tokens, or product recommendations). This directly reduces backend latency and load, improving response times for critical user flows. Ideal for Laravel applications where persistent caching (e.g., Redis) is overkill for short-lived data.
Cost-Effective Scaling Strategy: Use this package as a stopgap for distributed caching during early-stage growth or for low-traffic features. Avoids the operational overhead of managing Redis/Memcached for non-critical use cases, reducing cloud costs and DevOps complexity. Aligns with a build-measure-learn approach before investing in persistent infrastructure.
Roadmap for Caching Tier:
LRUCache for non-persistent, high-speed caching (e.g., A/B test variants, feature flags).LRUCache as a local fallback layer (e.g., Cache::store('lru')->get()).LRUCache with a hybrid solution (e.g., LRU + Redis TTL) for dynamic eviction policies.Build vs. Buy Decision: Justify using this package over a custom implementation by leveraging its:
get/put).Use Cases in Laravel:
Cache::lru()->increment('user:123:requests')).Cache::lru()->put('feature:new_ui', true)).Cache::lru()->get('experiment:variant_1')).Adopt When:
opcache).Look Elsewhere When:
file driver).memory_get_peak_usage())."This in-memory LRU cache is like a turbocharger for Laravel, cutting backend load by 30–50% for high-frequency, low-persistence data—think user sessions or API rate limits. It’s free, open-source, and eliminates Redis overhead for transient data, reducing cloud costs and DevOps complexity. Perfect for early-stage scaling or cost-sensitive features. The risk? Only if we hit memory limits, but we can monitor and upgrade hardware incrementally. ROI: Faster responses, lower costs, and a clear path to Redis when we scale."
*"The cash/lrucache package gives us a drop-in, zero-config in-memory cache with O(1) operations for get/put. Here’s why it fits:
user->last_seen_at or feature_flags).Cache facade to support an lru driver, enabling seamless adoption (e.g., Cache::store('lru')->get('key')). Benchmarks show it’s ~10x faster than file-based caches for small-to-medium datasets."**"This is a simple, battle-tested LRU cache with:
$cache->get('key') and $cache->put('key', $value).user->preferences for 100MB of data across 1000 users—no Redis setup needed. Just instantiate:$cache = new \Cash\LRUCache(1000); // Max 1000 items
$cache->put('user:123:prefs', $prefs);
Pro tip: Wrap it in a Laravel service for easy reuse:
// app/Services/LRUCacheService.php
class LRUCacheService {
public function __construct() {
$this->cache = new LRUCache(config('cache.lru.max_size'));
}
public function get($key) { /* ... */ }
}
Gotchas:
"7" and 7 collide (use strings with prefixes like user:7).*"This package eliminates Redis for transient data, reducing:
memory_get_peak_usage()) to avoid OOM crashes.How can I help you explore Laravel packages today?