webclient / cache-contract
缓存接口
v1.0.0
2022-07-26 11:50 UTC
Requires
- php: ^7.4 || ^8.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-18 19:59:30 UTC
README
webclient/cache-contract
为 webclient/ext-cache 提供的缓存接口
安装
composer require webclient/cache-contract:^1.0
技巧与窍门
分设置和响应的缓存存储
您可以分开设置和响应的缓存存储。
实现 cache-contract,就像这样
<?php use Webclient\Cache\Contract\CacheInterface; class SplitCache implements CacheInterface { private CacheInterface $settingsCache; private CacheInterface $responsesCache; public function __construct(CacheInterface $settingsCache, CacheInterface $responsesCache) { $this->settingsCache = $settingsCache; $this->responsesCache = $responsesCache; } public function get(string $key) : ?string { $this->getStorage($key)->get($key); } public function set(string $key,string $data,?int $ttl = null) : void { $this->getStorage($key)->get($key, $data, $ttl); } private function getStorage(string $key): CacheInterface { if (strpos($key, 'http.settings.') === 0) { return $this->settingsCache; } if (strpos($key, 'http.response.') === 0) { return $this->responsesCache; } throw new InvalidArgumentException('can not define storage'); } }