rnr1721 / le7-cache
PSR-16 SimpleCache for Le7 框架或任何其他 PHP 项目
1.0.6
2023-05-20 15:53 UTC
Requires
- php: >=8.1
- psr/simple-cache: ^3.0
Requires (Dev)
- phpunit/phpunit: ^10.0
- vimeo/psalm: ^5.7
Suggests
- rnr1721/le7-cache-memcache: Needed to support Memcache and memcached
Provides
README
需求
- PHP 8.1 或更高版本。
- Composer 2.0 或更高版本。
它能做什么?
- PSR-16 Simple Cache 的标准实现
- 开箱即用,可以使用文件系统、内存和会话适配器
- 作为另一个包(rnr1721/le7-cache-memcache)提供 memcache 和 memcached
安装
composer require rnr1721/le7-cache
它是如何工作的?
use Core\Cache\SCFactoryGeneric; $cacheFactory = new SCFactoryGeneric(); $cache = $cacheFactory->getFileCache('./cache'); $data = [ 'value1' => 'The 1 value', 'value2' => 'The 2 value' ]; // Put data in cache // Set cache key, value and time-to-live $cache->set('mykey', $data, 5000); // Get value from cache $result = $cache->get('mykey'); print_r($result);
实现的方法
use Psr\SimpleCache\CacheInterface; public function get(string $key, mixed $default = null): mixed; public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool; public function delete(string $key): bool; public function clear(): bool; public function getMultiple(iterable $keys, mixed $default = null): iterable; public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool; public function deleteMultiple(iterable $keys): bool; public function has(string $key): bool;
工厂方法
use Core\Interfaces\SCFactory; /** * Get filesysyem cache * @param string $folder Folder for caache * @return CacheInterface */ public function getFileCache(string $folder): CacheInterface; /** * Get memory cache * @return CacheInterface */ Public function getMemory(): CacheInterface; /** * Get Session cache * @return CacheInterface */ public function getSession(): CacheInterface;