Skip to content

nexphant/cache ​

Cache utility with static APCu helpers plus explicit array, APCu, and Redis stores for persistent PHP applications.

Install ​

bash
composer require nexphant/cache

Basic Usage ​

The static Nexphant\Cache\Cache class provides simple, direct keys caching using APCu:

php
use Nexphant\Cache\Cache;

// Store value for 5 minutes
Cache::set('key', 'value', ttl: 300);

// Retrieve value
$value = Cache::get('key', default: 'fallback');

// Check existence
if (Cache::has('key')) {
    // exists
}

// Delete key
Cache::delete('key');

// Clear all keys
Cache::clear();

Remember ​

Compute and store a value if it does not already exist:

php
$users = Cache::remember('all_users', ttl: 60, function () {
    return DB::table('users')->get();
});

Store Drivers ​

Use store classes when you need an isolated cache instance instead of the static facade:

php
use Nexphant\Cache\Store\ArrayStore;
use Nexphant\Cache\Store\ApcuStore;
use Nexphant\Cache\Store\RedisStore;

$memory = new ArrayStore();
$memory->set('request.local', ['ok' => true]);

$apcu = new ApcuStore(prefix: 'app:');
$apcu->increment('hits');

$redis = new RedisStore(host: '127.0.0.1', port: 6379, prefix: 'app:');
$redis->set('settings', ['theme' => 'dark'], ttl: 3600);

Available stores:

StoreBacking SystemNotes
ArrayStorePHP arrayPer-process memory cache, useful for tests and request-local values.
ApcuStoreAPCuShared inside the current PHP process pool. Requires ext-apcu.
RedisStoreRedisCross-process cache. Requires ext-redis.

Store instances support get, set, has, delete, increment, decrement, and clear.

Cache Warmup ​

CacheWarmer preloads framework metadata and config into APCu when available:

php
use Nexphant\Cache\CacheWarmer;

$stats = CacheWarmer::warm();
CacheWarmer::clear();

The returned stats include whether APCu is enabled, warmed keys, and warmup errors.

Cache API ​

MethodReturn TypeDescription
get(string $key, mixed $default = null)mixedRetrieve value by key.
set(string $key, mixed $value, int $ttl = 3600)boolStore a value with a specific TTL (seconds).
has(string $key)boolCheck if a key exists in cache.
delete(string $key)boolRemove a key from cache.
clear()boolFlush the entire cache store.
remember(string $key, int $ttl, callable $callback)mixedGet cached item, or compute and store it.

Released under the MIT License.