initphp/cache
最新稳定版本:1.0.0
Composer 安装命令:
composer require initphp/cache
包简介
A lightweight PSR-16 (Simple Cache) implementation with file, PDO, Redis, Memcache(d) and WinCache handlers.
关键字:
README 文档
README
A lightweight PSR-16 (Simple Cache) implementation with interchangeable handlers for the filesystem, PDO databases, Redis, Memcache(d) and WinCache.
Requirements
- PHP 8.0 or higher
psr/simple-cache^3.0
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-wincache — deprecated |
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:
nullmeans "store with no expiry"; a positive integer orDateIntervalsets 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 anInvalidArgumentException. - Counters:
increment()/decrement()treat a missing or non-numeric item as0and store the result without an expiry. They behave identically across every handler. - Values: anything
serialize()can handle round-trips exactly — includingnull,falseand objects.
Documentation
- Getting started
- Configuration & options
- PSR-16 behaviour & the handler API
- Handlers: File · PDO · Redis · Memcache(d) · WinCache
- Exceptions
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.
统计信息
- 总下载量: 105
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-17