marvin255/in-memory-cache

Array based cache for PHP

v2.3.3 2024-12-01 15:46 UTC

This package is auto-updated.

Last update: 2024-12-01 15:53:25 UTC


README

Build Status

Simple PSR-16 implementation which uses internal array to store data.

Usage

use Marvin255\InMemoryCache\InMemoryCache;

$maxCacheSize = 10000;  // only 10000 can be stored by this object
$defaultTTL = 60;       // 60 seconds as default TTL

$cache = new InMemoryCache($maxCacheSize, $defaultTTL);

Decorator

Decorator allows to use two caches in the same time. All data from basic cache (e.g. redis based cache) will be also stored in InMemoryCache. This decorator can reduce requests amount for long-living php processes.

use Marvin255\InMemoryCache\InMemoryCache;
use Marvin255\InMemoryCache\CompositeCache;

$maxCacheSize = 10000;  // only 10000 can be stored by this object
$defaultTTL = 60;       // 60 seconds as default TTL

$inMemoryCache = new InMemoryCache($maxCacheSize, $defaultTTL);
$redisCache = new MyAwesomeRedisCache();
$decorator = new CompositeCache($inMemoryCache, $redisCache);

$decorator->get('test'); // this get will trigger a request to redis and save data to memory
$decorator->get('test'); // this get won't trigger any requests and just return data from memory