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

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

Per-handler extensions

Handler Extension
File none — PHP core only
PDO ext-pdo (plus the matching driver, e.g. pdo_mysql, pdo_sqlite)
Redis ext-redis (phpredis)
Memcache(d) ext-memcached (preferred) or ext-memcache
WinCache ext-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:

<?php

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use InitPHP\Cache\Cache;
use InitPHP\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:

use InitPHP\Cache\Handler\Redis;
use InitPHP\Cache\Handler\File;
use InitPHP\Cache\Cache;

$cache = (new Redis())->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