beste/in-memory-cache
PSR-6 in-memory cache for PHP, ideal as a default cache or for tests. Lightweight CacheItemPool implementation with support for expiration and optional PSR-20 clock injection (e.g., frozen clocks) to control time in tests.
Illuminate\Contracts\Cache\Store), enabling seamless integration with the Cache facade, adapters, and third-party libraries. The package’s adherence to PSR-6 ensures compatibility with Laravel’s event system (e.g., Cache::store() events) and artisan commands (e.g., cache:clear).Cache::remember(), Cache::put(), and Cache::get() methods when configured as a PSR-6 driver.cache:clear artisan command (if manually cleared).time package and travel() helper via PSR-20 clock support (e.g., Beste\Clock\FrozenClock).Beste\Clock\FrozenClock, aligning with Laravel’s testing best practices.serialize()/unserialize(), which may fail for complex objects (e.g., closures, resources). Laravel’s serialize() helper can mitigate this, but custom serialization logic may be required for edge cases.-, arbitrary length), reducing friction for Laravel’s dynamic key generation (e.g., Cache::tags()).Cache::put('key', 'value', now()->addMinutes(1))).Cache::forget() or Cache::clear()) in critical paths.Cache::shouldReceive())config/cache.php?Cache::forget() triggers)?config/cache.php:
'in_memory' => [
'driver' => 'cache',
'store' => Beste\Cache\InMemoryCache::class,
],
Cache::store('in_memory') or set CACHE_DRIVER=in_memory in .env.Cache::store()) via PSR-6’s save()/delete() methods.Illuminate\Contracts\Cache\Store with real InMemoryCache instances.Beste\Clock\FrozenClock) for time-based assertions:
use Beste\Clock\FrozenClock;
use Beste\Cache\InMemoryCache;
$clock = FrozenClock::fromUTC();
$cache = new InMemoryCache($clock);
$cache->save($cache->getItem('key')->set('value')->expiresAfter(new DateInterval('PT5M')));
$clock->setTo($clock->now()->add(new DateInterval('PT6M')));
$this->assertFalse($cache->getItem('key')->isHit());
php artisan cache:clear by manually clearing the cache in a service provider:
public function boot()
{
Cache::extend('in_memory', function () {
return new Beste\Cache\InMemoryCache();
});
Cache::store('in_memory')->clear(); // Clear on boot if needed
}
InMemoryCache.isHit(), get()).// Before: Mocking
Cache::shouldReceive('get')->once()->andReturn('value');
// After: Real in-memory cache
$cache = new Beste\Cache\InMemoryCache();
$cache->save($cache->getItem('key')->set('value'));
$this->assertEquals('value', $cache->getItem('key')->get());
composer.json and configure in .env:
CACHE_DRIVER=in_memory
config/cache.php to include the driver (see Stack Fit above).config/cache.php:
'stores' => [
'in_memory' => [
'driver' => 'cache',
'store' => Beste\Cache\InMemoryCache::class,
'priority' => 2, // Lower priority than Redis
],
],
Cache::store() to route requests:
$value = Cache::store('in_memory')->get('fallback_key', function () {
return $this->fetchFromDatabase();
});
Illuminate\Contracts\Cache\Store.Beste\Clock\FrozenClock) for testing.Cache::tags()) with extended PSR-6 key rules (e.g., dashes -).1
How can I help you explore Laravel packages today?