nexphant/cache
Cache utility with static APCu helpers plus explicit array, APCu, and Redis stores for persistent PHP applications.
Install
bash
composer require nexphant/cacheBasic 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:
| Store | Backing System | Notes |
|---|---|---|
ArrayStore | PHP array | Per-process memory cache, useful for tests and request-local values. |
ApcuStore | APCu | Shared inside the current PHP process pool. Requires ext-apcu. |
RedisStore | Redis | Cross-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
| Method | Return Type | Description |
|---|---|---|
get(string $key, mixed $default = null) | mixed | Retrieve value by key. |
set(string $key, mixed $value, int $ttl = 3600) | bool | Store a value with a specific TTL (seconds). |
has(string $key) | bool | Check if a key exists in cache. |
delete(string $key) | bool | Remove a key from cache. |
clear() | bool | Flush the entire cache store. |
remember(string $key, int $ttl, callable $callback) | mixed | Get cached item, or compute and store it. |
