mesavolt / simple-cache
简单的缓存服务。
v2.2.0
2023-07-27 13:34 UTC
Requires
- php: >=7.4
- ext-json: *
- symfony/cache: ^4.0||^5.4
Requires (Dev)
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ^7.2
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2024-09-27 16:09:33 UTC
README
基于 Symfony 缓存组件的简单缓存系统。根据 TTL,总是返回值,无论是新鲜的还是缓存的。
安装
composer require mesavolt/simple-cache
用法
<?php use Mesavolt\SimpleCache; $cache = new SimpleCache(sys_get_temp_dir()); $ttl = 10; // values expire after 10 seconds // use the cache for the 1st, the callable is executed, a fresh value is cached and returned $value1 = $cache->get('key', function () { return time(); }, $ttl); // use the cache again before TTL has passed. // the callable is *not* executed again, the previously cached value is returned $value2 = $cache->get('key', function () { return time(); }, $ttl); assert($value2 === $value1); // true because TTL hasn't expired // sleep 20 seconds, this is longer that our TTL of 10 seconds sleep(20); // use the cache for the 3rd time, TTL has expired so a new fresh value is cached and returned $value3 = $cache->get('key', function () { return time(); }); assert($value3 !== $value1); // true because `sleep(20)` expired the TTL // so the callable has been executed // and a fresh value has been returned (and cached)