initphp/cache

A lightweight PSR-16 (Simple Cache) implementation with file, PDO, Redis, Memcache(d) and WinCache handlers.

Maintainers

Package info

github.com/InitPHP/Cache

Documentation

pkg:composer/initphp/cache

Fund package maintenance!

muhammetsafak

Statistics

Installs: 105

Dependents: 0

Suggesters: 0

Stars: 2

Open Issues: 0

1.0.0 2026-06-10 04:27 UTC

This package is auto-updated.

Last update: 2026-06-11 17:32:14 UTC


README

A lightweight PSR-16 (Simple Cache) implementation with interchangeable handlers for the filesystem, PDO databases, Redis, Memcache(d) and WinCache.

CI Latest Stable Version Total Downloads License PHP Version Require

Requirements

Each handler may need its own PHP extension:

Handler Class Backend Needs
File InitPHP\Cache\Handler\File Filesystem — (core only)
PDO InitPHP\Cache\Handler\PDO SQL database (MySQL, SQLite, PostgreSQL…) ext-pdo
Redis InitPHP\Cache\Handler\Redis Redis ext-redis (phpredis)
Memcache InitPHP\Cache\Handler\Memcache Memcached ext-memcached or ext-memcache
WinCache InitPHP\Cache\Handler\Wincache WinCache user cache ext-wincachedeprecated

Installation

composer require initphp/cache

Quick start

require 'vendor/autoload.php';

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

$cache = Cache::create(File::class, [
    'path' => __DIR__ . '/var/cache',
]);

// Read-through pattern: compute and store on a miss.
$posts = $cache->get('posts');
if ($posts === null) {
    $posts = [
        ['id' => 12, 'title' => 'Post 12'],
        ['id' => 15, 'title' => 'Post 15'],
    ];
    $cache->set('posts', $posts, 120); // cache for 120 seconds
}

print_r($posts);

Cache::create() returns a handler that implements InitPHP\Cache\CacheInterface, which extends Psr\SimpleCache\CacheInterface. You can equally type-hint and pass any PSR-16 cache around your application.

Switching handlers

Only the factory call changes; the cache API is identical for every backend.

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

$cache = Cache::create(Redis::class, [
    'host'     => '127.0.0.1',
    'port'     => 6379,
    'database' => 0,
]);

$cache->set('user:42', ['name' => 'Jane'], 3600);
$cache->get('user:42');

API at a glance

Call Returns Purpose
get(string $key, mixed $default = null) mixed Read a value, or $default on a miss.
set(string $key, mixed $value, null|int|DateInterval $ttl = null) bool Store a value, optionally with a TTL.
delete(string $key) bool Remove one item.
clear() bool Remove every item this handler owns.
has(string $key) bool Whether a live item exists.
getMultiple(iterable $keys, mixed $default = null) iterable Read many items at once.
setMultiple(iterable $values, null|int|DateInterval $ttl = null) bool Store many items at once.
deleteMultiple(iterable $keys) bool Remove many items at once.
increment(string $key, int $offset = 1) int Add to a counter and return the new value.
decrement(string $key, int $offset = 1) int Subtract from a counter and return the new value.

See docs/ for the full guide, including per-handler configuration and the exact semantics of TTLs, keys and counters.

Notes

  • TTL: null means "store with no expiry"; a positive integer or DateInterval sets a lifetime; a zero or negative TTL deletes the item.
  • Keys: any non-empty string that does not contain the PSR-16 reserved characters {}()/\@:. An invalid key throws an InvalidArgumentException.
  • Counters: increment()/decrement() treat a missing or non-numeric item as 0 and store the result without an expiry. They behave identically across every handler.
  • Values: anything serialize() can handle round-trips exactly — including null, false and objects.

Documentation

Contributing

Bug reports and pull requests are welcome. CI runs PHP-CS-Fixer, PHPStan (max level) and PHPUnit across PHP 8.0–8.4, plus a Redis/Memcached integration job. Run the same static bundle locally with:

composer ci

See the changelog for release history.

Credits

License

Copyright © 2022 InitPHP — released under the MIT License.