-
Notifications
You must be signed in to change notification settings - Fork 0
Quick Start
Five minutes from composer require to a working cache.
Use the Cache::create() factory. Its first argument is a
handler class; the second is an options array.
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use InitPHP\Cache\Cache;
use InitPHP\Cache\Handler\File;
$cache = Cache::create(File::class, [
'path' => __DIR__ . '/var/cache', // the directory must exist and be writable
]);create() returns an object implementing
CacheInterface, which extends
Psr\SimpleCache\CacheInterface. You can type-hint either interface anywhere in
your app.
$cache->set('greeting', 'Hello'); // no TTL → kept until deleted
$cache->set('token', 'abc123', 3600); // expires in 3600 seconds
$cache->get('greeting'); // "Hello"
$cache->get('unknown'); // null (default)
$cache->get('unknown', 'fallback'); // "fallback"
$cache->has('greeting'); // true
$cache->delete('greeting'); // true
$cache->clear(); // wipe everything this handler ownsValues keep their type and structure — strings, ints, floats, booleans, null,
arrays and objects all round-trip exactly:
$cache->set('config', ['debug' => true, 'level' => 5]);
$cache->get('config'); // ['debug' => true, 'level' => 5]The most common use of a cache: return the stored value, or compute and store it on a miss.
$report = $cache->get('daily-report');
if ($report === null) {
$report = build_expensive_report(); // your slow code
$cache->set('daily-report', $report, 86400); // cache for a day
}
return $report;If
nullis a legitimate value for you, usehas()to tell a miss from a storednull— see Keys, TTL & Values.
Because every handler shares the same API, moving from the filesystem to Redis is a one-line change:
use InitPHP\Cache\Handler\Redis;
$cache = Cache::create(Redis::class, [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
]);
// ...the rest of your code is unchanged.
$cache->set('user:42', ['name' => 'Jane'], 3600);See the handler pages for each backend's options.
// Many at once (accepts arrays or any iterable)
$cache->setMultiple(['a' => 1, 'b' => 2, 'c' => 3], 600);
$cache->getMultiple(['a', 'b', 'missing'], 0); // ['a'=>1, 'b'=>2, 'missing'=>0]
$cache->deleteMultiple(['a', 'b']);
// Atomic-style counters (return the new value)
$cache->increment('views'); // 1
$cache->increment('views', 5); // 6
$cache->decrement('views', 2); // 4See Bulk Operations and Counters.
-
create()instantiated the handler, verified the runtime supports it, and merged your options over the defaults. -
set()serialised the value and wrote it to the backend, with the TTL you gave (or none). -
get()read it back, returned your$defaulton a miss, and honoured the expiry.
-
Keys are validated. A key must be a non-empty string with none of the
reserved characters
{}()/\@:; otherwise anInvalidArgumentExceptionis thrown. See Keys, TTL & Values. -
A zero or negative TTL deletes the item (and returns
true). -
The default key prefix is
cache_. It scopesclear()so caches sharing one backend don't wipe each other. Change it with theprefixoption. -
get()never calls your default. A callable default is returned as a plain value, per PSR-16.
- The Cache Factory — instances, options, error cases.
- Keys, TTL & Values — the full rules.
- Recipes — read-through, memoization, rate limiting, and more.
- Configuration Options — every option, every handler.
initphp/cache · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Core Concepts
Handlers
Guides
Other