guentur/cache-wrapper

优先使用 Magento 缓存模块。您可以从缓存中获取数据,如果没有在缓存中找到,CacheWrapper 模块会自动将其保存到缓存中,然后再将数据从缓存中传递给您

安装: 8

依赖: 0

建议者: 0

安全: 0

星星: 1

关注者: 1

分支: 0

开放性问题: 0

类型:magento2-module

1.2.0 2022-06-05 13:42 UTC

This package is auto-updated.

Last update: 2024-09-10 11:08:25 UTC


README

安装

composer require guentur/cache-wrapper

bin/magento module:enable Guentur_CacheWrapper

开始使用

要使用模块的功能,请按如下方式在构造函数中实现

    private $cache;

    public function __construct(
        \Guentur\CacheWrapper\App\CacheInterface $cache
    ) {
        $this->cache = $cache;
    }

模块还重写了 Magento\Framework\App\CacheInterface 的 preference(请参阅 Guentur/CacheWrapper/etc/di.xml),因此可以从标准接口调用模块的方法。

术语表

  • 缓存标识符 - 是唯一的,用于获取一个特定的记录。不应在同一个标识符下保存多个记录。

  • 缓存标签 - 是分类缓存记录的一种方式。当您保存缓存时,您可以设置一个将应用于该记录的标签数组。然后您可以清除所有标记为该标签(或标签组)的缓存记录。标签可以在不同的缓存记录中重复。

要指定您的记录属于哪种缓存类型 - 在保存时传递由缓存类型类定义的标签

示例

在配置 cache.xml 中按标识符找到缓存类

...
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd">
    <type name="full_page" translate="label,description" instance="Magento\PageCache\Model\Cache\Type">
        <label>Page Cache</label>
        <description>Full page caching</description>
    </type>
</config>

传递来自类型类的标签

return $this->cache->getCached(
            'some_cache_key',
            function () {
                ...
                return $data;
            },
            [\Magento\PageCache\Model\Cache\Type::CACHE_TAG],
            86400
        );

保存到缓存

saveToCache(string $index, array $data, array $cacheTags = [], $lifeTime = null): void

  • string $index - 缓存标识符。用于获取所有具有相同标识符的记录。
  • array $data - 要保存的数据。
  • string[] $cacheTags - 缓存标签
  • int $lifeTime = null - 缓存的生命周期。

从缓存获取数据

getCached(string $index, callable $data, array $cacheTags = [], $lifeTime = null): array

  • string $index - 缓存标识符。用于获取数据。
  • callable $data - 匿名函数,返回要保存的 数组 数据。
  • string[] $cacheTags - 缓存标签
  • int $lifeTime = null - 缓存的生命周期。

如果无法从缓存获取数据

调用 callable $data 获取应在缓存中的数据。调用 saveToCache() 保存数据。它传递: $index;由 callable $data 返回的数据;$cacheTags

getCached() 的使用示例

$this->cache->getCached(
            'cache_key_index',
            function () use ($greaterThan) {
                return $this->discountTierCollectionFactory->create()
                    ->addFieldToFilter(self::DISCOUNT_PRODUCT_TYPE, $productTypeId)
                    ->addFieldToFilter(self::SOME_ATTRIBUTE, ["gt" => $greaterThan])
                    ->setOrder(self::DISCOUNT_MIN_QTY, Collection::SORT_ORDER_ASC)
                    ->getFirstItem()
                    ->getData();
            },
            ['cache_key_index' . $greaterThan]
        );

使用匿名函数传递数据可以称为优化。

清除缓存

cleanWithMode(string $mode, array $tags = []): bool

可用的模式

  • all (默认) => 删除所有缓存条目($tags 不使用)
  • old => 删除过旧的缓存条目($tags 不使用)
  • matchingTag => 删除匹配所有给定标签的缓存条目($tags 可以是字符串数组或单个字符串)
  • notMatchingTag => 删除不匹配给定标签之一的缓存条目($tags 可以是字符串数组或单个字符串)
  • matchingAnyTag => 移除匹配任何给定标签的缓存条目($tags 可以是一个字符串数组或单个字符串)

注意事项。Magento 2中的缓存是如何工作的

用于在Magento 2中处理缓存的基本类 - Magento\Framework\Cache\Core 继承自 \Zend_Cache_Core

@todo