-
Notifications
You must be signed in to change notification settings - Fork 0
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.
| 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. |
| 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-wincache — deprecated, 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.
composer require initphp/cacheComposer registers the PSR-4 namespace InitPHP\Cache\, so everything is
available as soon as you require vendor/autoload.php.
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.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.
Rediswithoutext-redis) throws aCacheException. The check above lets you fall back gracefully instead.
- Quick Start — store and read your first values.
-
The Cache Factory — everything
Cache::create()does. - Configuration Options — every option of 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