GuzzleEtag - marmot-cn/marmot-framework GitHub Wiki

Etag缓存

概述

根据请求头If-None-Match匹配返回数据.

  • 如果etag匹配, 则返回304, 接口不返回任何数据, 从缓存中获取数据
  • 如果etag不匹配, 则返回直接数据.

示例

1. 引入性状

use EtagCacheStrategy

2. 替换get请求为getWithCache

场景:

  • 需要将获取单体的请求添加缓存特性
  • 其他如搜索请求不添加缓存特性

将需要缓存的请求替换为缓存特性请求

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;
}

改请求会自动引入etag特性.