Skip to content

Installation

Muhammet Şafak edited this page Jun 10, 2026 · 1 revision

Installation

initphp/cache is distributed via Packagist. Its only runtime dependency is the PSR-16 interface package; each backend may additionally need its own PHP extension.

Requirements

RequirementNotes
PHP >= 8.0The code uses declare(strict_types=1), mixed, and union types.
psr/simple-cache^3.0Pulled in automatically by Composer.
ComposerFor installation and autoloading.

Per-handler extensions

HandlerExtension
Filenone — PHP core only
PDOext-pdo (plus the matching driver, e.g. pdo_mysql, pdo_sqlite)
Redisext-redis (phpredis)
Memcache(d)ext-memcached (preferred) or ext-memcache
WinCacheext-wincachedeprecated, Windows only

You only need the extension for the handler you actually use. The File handler works on any PHP 8 install with no extra setup.

Install

composer require initphp/cache

Composer registers the PSR-4 namespace InitPHP\Cache\, so everything is available as soon as you require vendor/autoload.php.

Verify the install

Drop this script into your project root as check-cache.php:

<?phpdeclare(strict_types=1);
require__DIR__ . '/vendor/autoload.php';
useInitPHP\Cache\Cache;
useInitPHP\Cache\Handler\File;
$dir = __DIR__ . '/var/cache';
@mkdir($dir, 0775, true);
$cache = Cache::create(File::class, ['path' => $dir]);
$cache->set('check', 'it works', 60);
echo$cache->get('check') === 'it works'
? "InitPHP Cache is working.\n"
: "Something is wrong.\n";
$cache->delete('check');
php check-cache.php
# InitPHP Cache is working.

Checking a handler at runtime

Every handler can tell you whether the current runtime supports it (isSupported()), which is handy for picking a backend dynamically:

useInitPHP\Cache\Handler\Redis;
useInitPHP\Cache\Handler\File;
useInitPHP\Cache\Cache;
$cache = (newRedis())->isSupported()
? Cache::create(Redis::class, ['host' => '127.0.0.1'])
: Cache::create(File::class, ['path' => __DIR__ . '/var/cache']);

Building an unsupported handler through the factory (e.g. Redis without ext-redis) throws a CacheException. The check above lets you fall back gracefully instead.

Next steps

Clone this wiki locally