freshcells/cache

带有 keyGenerator & 压缩的 PSR-6 缓存实现

1.0.0 2018-12-21 13:52 UTC

This package is auto-updated.

Last update: 2024-09-03 14:22:12 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

PSR-6 Cache 实现带有 KeyGenerator 和压缩。

KeyGeneration

这是 PSR-6 的扩展。
通过给定的 var 作为键缓存数据。
这可以是对象、数组或任何 var。
使用 SerializeKeyGenerator 进行键生成将序列化和 md5 给定的 var,并且你可以添加自定义前缀。

你也可以通过实现接口根据你的需求定义自己的 KeyGenerator。

它被设计用来通过变量 resp. 查询对象来缓存数据,键将基于这个变量生成。如果你事先知道缓存键,你可以直接使用缓存池实例。

$keyVar      = new \stdClass();
$keyVar->foo = ['baz' => 'bif'];
$item = $cache->getItemByVar($keyVar);
$item->set($data);
$cache->save($item);
$itemFromCache = $cache->getItemByVar($keyVar);

压缩

如果可能,数据可以压缩,在传递给缓存之前数据将被序列化。

$compression    = new GzCompression();
$cache = CompressionCachePool($innerCachePool, $compression);

特质

为普通的 PSR-6 CacheItemPoolInterface 和扩展的 GeneratableKeyCache 提供缓存感知特质。

CacheableTraitGenerateableKeyCacheableTrait 提供了获取和保存缓存数据的包装方法。

    ...
    use CachePoolAwareTrait;
    use CacheableTrait;

    public function load()
    {
        return $this->cacheable(
            self::CACHE_KEY,
            function () {
                $entity = new TestEntity();

                return $entity;
            },
            function ($data) {
                return $data instanceof TestEntity;
            }
        );
    }
    ...
待办事项

Symfony Bridge Cache Bundle 带有用于标签 cacher.aware 的 CompilerPass

  • name: 'cacher.aware', cache: cache.pool.service, enabled: %cache.enabled%

许可证

MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。