-
Notifications
You must be signed in to change notification settings - Fork 0
The Cache Factory
InitPHP\Cache\Cache is a tiny factory with a single static method. It builds a
handler, checks the runtime supports it, applies your options, and hands you back
a CacheInterface.
namespace InitPHP\Cache;
final class Cache
{
public static function create(
string|CacheInterface $handler,
array $options = []
): CacheInterface;
}The usual form — pass a handler class and its options:
use InitPHP\Cache\Cache;
use InitPHP\Cache\Handler\File;
$cache = Cache::create(File::class, [
'path' => __DIR__ . '/var/cache',
'prefix' => 'app_',
]);The class is instantiated for you, so it must be one of the built-in handlers or
your own class that implements CacheInterface
and has a no-argument-compatible constructor (extending
BaseHandler gives you exactly that).
If you have already built a handler, pass it straight through. create() simply
applies the options and returns it:
$handler = new File(['path' => __DIR__ . '/var/cache']);
$cache = Cache::create($handler, ['prefix' => 'app_']);
// $cache === $handlerThe factory is convenience, not ceremony. Every handler's constructor accepts the same options array, so this is equivalent to the first example:
use InitPHP\Cache\Handler\File;
$cache = new File([
'path' => __DIR__ . '/var/cache',
'prefix' => 'app_',
]);The factory earns its keep when the handler class is dynamic (e.g. read from config) and when you want a single place that validates the class.
Option keys are normalised to lower case, so these are all the same option:
Cache::create(File::class, ['PATH' => '/tmp/cache']);
Cache::create(File::class, ['Path' => '/tmp/cache']);
Cache::create(File::class, ['path' => '/tmp/cache']);Options you don't set fall back to the handler's defaults (and the shared
prefix default of cache_). See Configuration Options
for every default.
Handlers expose two helpers from CacheInterface:
$cache->getOption('prefix'); // "app_"
$cache->getOption('missing', '-'); // "-" (default when unset)
$cache->setOptions(['prefix' => 'v2_']); // merge more options in; returns $thissetOptions() merges (case-insensitively) over the current options and returns
the handler, so it chains:
$cache = (new Redis())->setOptions(['host' => 'cache.internal', 'database' => 2]);create() throws a CacheException in three cases:
use InitPHP\Cache\Exception\CacheException;
try {
// 1. The class does not exist
Cache::create('App\\Cache\\Nope');
// 2. The class exists but is not a CacheInterface
Cache::create(\stdClass::class);
// 3. The handler's runtime requirement is missing
// (e.g. Redis without ext-redis)
Cache::create(\InitPHP\Cache\Handler\Redis::class);
} catch (CacheException $e) {
// log / fall back to another handler
}The third case happens inside the handler constructor, which calls
isSupported() and rejects an unsupported environment. You can pre-check with
(new Redis())->isSupported() — see Installation.
- Keys, TTL & Values — the rules for what you store.
- Configuration Options — every option of every handler.
-
Custom Handlers — pass your own class to
create().
initphp/cache · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Core Concepts
Handlers
Guides
Other