Skip to content

marko/cache-array

In-memory cache driver --- stores data for the duration of a single request with zero I/O overhead. The array cache driver keeps all cached data in a PHP array. Data does not persist across requests, making it ideal for development, testing, and single-request deduplication (e.g., avoiding duplicate database queries within one request).

Implements CacheInterface from marko/cache.

Terminal window
composer require marko/cache-array

This automatically installs marko/cache.

Set the cache driver to array in your config:

config/cache.php
return [
'driver' => 'array',
'default_ttl' => 3600,
'path' => 'storage/cache',
];

Once configured, inject CacheInterface as usual --- the array driver is used automatically:

use Marko\Cache\Contracts\CacheInterface;
class ExpensiveService
{
public function __construct(
private CacheInterface $cache,
) {}
public function compute(
string $key,
): array {
if ($this->cache->has($key)) {
return $this->cache->get($key);
}
$result = $this->doExpensiveWork($key);
$this->cache->set($key, $result);
return $result;
}
}
  • Development --- Fast iteration without external dependencies
  • Testing --- Predictable, isolated cache behavior per test
  • Request deduplication --- Cache expensive computations within a single request

For persistent caching, use marko/cache-file or marko/cache-redis.

The package also binds LockStoreInterface to Marko\Cache\Memory\Lock\ArrayLockStore as a singleton, so atomic locks work through LockFactoryInterface. Its scope is LockScope::Process: locks live in memory and exclude callers in the same PHP process only. Use it in tests, and use marko/cache-redis for real cross-worker exclusion. Construct it with a FakeClock to expire locks without sleeping:

use Marko\Cache\Memory\Lock\ArrayLockStore;
use Marko\Testing\Fake\FakeClock;
$clock = new FakeClock('2026-01-01 12:00:00 UTC');
$store = new ArrayLockStore($clock);
$store->acquire('video-pipeline:42', 'owner-a', 30);
$clock->travel('+30 seconds');
$store->currentOwner('video-pipeline:42'); // null: the lock expired

The driver reads the current time through the PSR-20 ClockInterface from marko/clock, which the container injects. In tests, construct it with a FakeClock and call travel() to cross a TTL boundary exactly, without sleep():

use Marko\Cache\CacheOperations;
use Marko\Cache\Config\CacheConfig;
use Marko\Cache\Memory\Driver\ArrayCacheDriver;
use Marko\Core\Path\ProjectPaths;
use Marko\Testing\Fake\FakeClock;
use Marko\Testing\Fake\FakeConfigRepository;
$clock = new FakeClock('2026-01-01 12:00:00 UTC');
$cache = new ArrayCacheDriver(
new CacheConfig(new FakeConfigRepository(['cache.default_ttl' => 3600]), new ProjectPaths()),
$clock,
new CacheOperations(),
);
$cache->set('report', $report, ttl: 300);
$clock->travel('+301 seconds');
$cache->has('report'); // false

Implements all methods from CacheInterface. See marko/cache for the full contract.

MethodDescription
get(string $key, mixed $default = null): mixedRetrieve a value, or $default if missing/expired
set(string $key, mixed $value, ?int $ttl = null): boolStore a value with optional TTL (defaults to default_ttl from config)
has(string $key): boolCheck if a non-expired entry exists
delete(string $key): boolRemove an entry
clear(): boolRemove all entries
getItem(string $key): CacheItemInterfaceRetrieve a CacheItem with hit/miss metadata
getMultiple(array $keys, mixed $default = null): iterableRetrieve multiple values at once
setMultiple(array $values, ?int $ttl = null): boolStore multiple values at once
deleteMultiple(array $keys): boolRemove multiple entries at once
increment(string $key, int $ttl): intAtomically increment an integer counter; TTL applied only on first increment
add(string $key, mixed $value, ?int $ttl = null): boolStore a value only if the key is absent or expired; false when an unexpired value exists
remember(string $key, ?int $ttl, callable $callback): mixedReturn the cached value, or compute and store it on a miss (delegates to CacheOperations)
pull(string $key, mixed $default = null): mixedGet a value and delete it (delegates to CacheOperations)
forever(string $key, mixed $value): boolStore a value with no expiry (delegates to CacheOperations)

add() checks and writes the in-process store with nothing in between, so it is atomic within the process. The store itself is per-process, so use marko/cache-file or marko/cache-redis when add() must be atomic across requests.

TTL behavior: a positive TTL sets an expiration timestamp relative to the injected clock. A null TTL falls back to the configured default_ttl. A TTL of 0 or less means the entry never expires. Expired entries are lazily purged on the next read.