arnapou / psr-cache
库 - PSR-6, PSR-16.
v1.0.0
2024-09-09 16:10 UTC
Requires
- php: ~8.2.0 || ~8.3.0
- arnapou/psr-clock: ^1.0
- psr/cache: ^2.0 || ^3.0
- psr/simple-cache: ^2.0 || ^3.0
Requires (Dev)
- ext-memcached: *
- ext-pcntl: *
- ext-redis: *
- friendsofphp/php-cs-fixer: ^3.52
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/php-code-coverage: ^11.0
- phpunit/phpunit: ^11.0
Provides
README
KISS (Keep It Simple Stupid) PSR (PHP Standards Recommendations) 类。
安装
composer require arnapou/psr-cache
packagist 👉️ arnapou/psr-cache
示例 PSR-6 缓存
KISS,这里我们只有 PSR-16 Simple 缓存的 PSR-6 适配器。
注意,您可以注入自己的 PSR-20 Clock 接口。
$cache = new \Arnapou\Psr\Psr6Cache\CacheItemPool(
psr16SimpleCache: $psr16SimpleCache,
defaultTtl: 900,
);
$item = $cache->getItem('my-key');
$item->set('my-value');
$item->expiresAfter(300);
$cache->save($item);
示例 PSR-16 Simple Cache
我没有从 PSR 实现的一件事是 PSR 键字符限制。
因为我在过去 12 年中从未见过需要这种情况的案例。
实施的技术限制只有(例如 memcached)。
实现。
use Arnapou\Psr\Psr16SimpleCache;
// For runtime, full memory
$cache = new Psr16SimpleCache\ArraySimpleCache(defaultTtl: 900);
// Flat file cache with custom serializer and PSR-20 Clock Interface
$cache = new Psr16SimpleCache\FileSimpleCache(path: '/path/of/cache', defaultTtl: 900);
// Memcached cache with custom serializer and PSR-20 Clock Interface
$cache = new Psr16SimpleCache\MemcachedSimpleCache($memcached);
// Redis cache with custom serializer and PSR-20 Clock Interface
$cache = new Psr16SimpleCache\RedisSimpleCache($redis);
// Does nothing at all
$cache = new Psr16SimpleCache\NullSimpleCache();
装饰器。
use Arnapou\Psr\Psr16SimpleCache\Decorated;
// Helps to reduce I/Os on decorated cache by keeping in memory the values already retrieved
$cache = new Decorated\RuntimeSimpleCache($decorated);
// Helps to isolate keys in "namespaces" for instance
$cache = new Decorated\PrefixSimpleCache($decorated, 'some:prefix');
// Forces the ttl to be in the range wanted to avoid silly TTLs
$cache = new Decorated\TtlRestrictedSimpleCache($decorated, ttlMin: 60, ttlMax: 3600);
// Avoid any writing in that cache
$cache = new Decorated\ReadonlySimpleCache($decorated, throwException: true);
PHP 版本
日期 | 参考 | 8.3 | 8.2 |
---|---|---|---|
09/09/2024 | 1.0.x, main | × | × |