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.

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

TTL behavior: a positive TTL sets an expiration timestamp. A TTL of 0 or null falls back to the configured default_ttl. Expired entries are lazily purged on the next read.