GuzzlePeriod - marmot-cn/marmot-framework GitHub Wiki
Period缓存
概述
缓存会首先保存一段时间, 失效后根据请求头If-None-Match
匹配返回数据.
- 获取缓存
- 如果缓存存在
- 缓存没有超时, 返回数据
- 获取
etag
- 如果etag匹配, 则返回
304
, 接口不返回任何数据, 从缓存中获取数据 - 如果etag不匹配, 则返回直接数据.
- 如果etag匹配, 则返回
- 如果缓存存在
- 直接发起请求
示例
1. 引入性状
use PeriodCacheStrategy
get
请求为getWithCache
2. 替换场景:
- 需要将获取单体的请求添加缓存特性
- 其他如搜索请求不添加缓存特性
将需要缓存的请求替换为缓存特性请求
原
protected function fetchOneAction(int $id, INull $null)
{
$this->get(
$this->getResource().'/'.$id
);
return $this->isSuccess() ? $this->translateToObject() : $null;
}
替换为
protected function fetchOneAction(int $id, INull $null)
{
$this->getWithCache(
$this->getResource().'/'.$id
);
return $this->isSuccess() ? $this->translateToObject() : $null;
}
改请求会自动引入period
缓存特性.