Skip to content

Repository files navigation

InitPHP Cache

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

CILatest Stable VersionTotal DownloadsLicensePHP Version Require

Requirements

Each handler may need its own PHP extension:

HandlerClassBackendNeeds
FileInitPHP\Cache\Handler\FileFilesystem— (core only)
PDOInitPHP\Cache\Handler\PDOSQL database (MySQL, SQLite, PostgreSQL…)ext-pdo
RedisInitPHP\Cache\Handler\RedisRedisext-redis (phpredis)
MemcacheInitPHP\Cache\Handler\MemcacheMemcachedext-memcached or ext-memcache
WinCacheInitPHP\Cache\Handler\WincacheWinCache user cacheext-wincachedeprecated

Installation

composer require initphp/cache

Quick start

require'vendor/autoload.php';
useInitPHP\Cache\Cache;
useInitPHP\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.

useInitPHP\Cache\Cache;
useInitPHP\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

CallReturnsPurpose
get(string $key, mixed $default = null)mixedRead a value, or $default on a miss.
set(string $key, mixed $value, null|int|DateInterval $ttl = null)boolStore a value, optionally with a TTL.
delete(string $key)boolRemove one item.
clear()boolRemove every item this handler owns.
has(string $key)boolWhether a live item exists.
getMultiple(iterable $keys, mixed $default = null)iterableRead many items at once.
setMultiple(iterable $values, null|int|DateInterval $ttl = null)boolStore many items at once.
deleteMultiple(iterable $keys)boolRemove many items at once.
increment(string $key, int $offset = 1)intAdd to a counter and return the new value.
decrement(string $key, int $offset = 1)intSubtract 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.

About

PSR-16 (Simple Cache) implementation for PHP 8 with File, PDO, Redis, Memcache(d) and WinCache handlers.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages