thecodingmachine/cache-utils

轻松存储与文件相关的缓存项

v1.0.0 2019-06-20 09:02 UTC

This package is auto-updated.

Last update: 2024-09-15 19:57:45 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Scrutinizer Code Quality Build Status Coverage Status

为什么?

此包包含一些实用类,用于操作PSR-6缓存。

文件绑定缓存

大多数PHP缓存系统(如PSR-6或PSR-16)都在缓存中存储项目,并给项目分配一个生存时间(TTL)。

如果您正在开发一个依赖于反射的PHP框架或PHP分析库,通常会有与PHP文件或PHP类相关的缓存项目。

例如,类中的Doctrine Annotations只有在类文件更改时才会更改。因此,将缓存失效绑定到文件的修改日期是有意义的。《thecodingmachine/cache-utils》正是为此而提供。

use TheCodingMachine\CacheUtils\FileBoundCache;

$fileBoundCache = new FileBoundCache($psr6Cache);

// Put the $myDataToCache object in cache.
// If 'FooBar.php' and 'FooBaz.php' are modified, the cache item is purged.
$fileBoundCache->set('cache_key', $myDataToCache, 
[
    'FooBar.php',
    'FooBaz.php'
]);

// Fetching data
$myDataToCache = $fileBoundCache->get('cache_key');

您还可以使用FileBoundMemoryAdapter将缓存存储在内存中,以便在相同查询中更快地访问。

use TheCodingMachine\CacheUtils\FileBoundCache;
use TheCodingMachine\CacheUtils\FileBoundMemoryAdapter;

$fileBoundCache = new FileBoundMemoryAdapter(new FileBoundCache($psr6Cache));

类绑定缓存

您还可以使用ClassBoundCache类将缓存项目绑定到类/特性/接口。如果类/特性/接口被修改,缓存将过期。

use TheCodingMachine\CacheUtils\FileBoundCache;
use TheCodingMachine\CacheUtils\ClassBoundCache;

$fileBoundCache = new FileBoundCache($psr6Cache);
$classBoundCache = new ClassBoundCache($fileBoundCache);

// Put the $myDataToCache object in cache.
// If the FooBar class is modified, the cache item is purged.
$classBoundCache->set('cache_key', $myDataToCache, new ReflectionClass(FooBar::class));

// Fetching data
$myDataToCache = $classBoundCache->get('cache_key');

ClassBoundCache构造函数接受3个额外的参数

class ClassBoundCache implements ClassBoundCacheInterface
{
    public function __construct(FileBoundCacheInterface $fileBoundCache, bool $analyzeParentClasses = true, bool $analyzeTraits = true, bool $analyzeInterfaces = false)
}
  • $analyzeParentClasses:如果设置为true,如果父类之一被修改,则缓存将失效
  • $analyzeTraits:如果设置为true,如果特性之一被修改,则缓存将失效
  • $analyzeInterfaces:如果设置为true,如果实现的一个接口被修改,则缓存将失效

您还可以使用ClassBoundMemoryAdapter将缓存存储在内存中,以便在相同查询中更快地访问。

use TheCodingMachine\CacheUtils\ClassBoundCache;
use TheCodingMachine\CacheUtils\ClassBoundMemoryAdapter;

$classBoundCache = new ClassBoundMemoryAdapter(new ClassBoundCache($psr6Cache));

与缓存合约更简单的接口

您甚至可以使用ClassBoundCacheContract获得更易于使用的类绑定缓存。

use TheCodingMachine\CacheUtils\FileBoundCache;
use TheCodingMachine\CacheUtils\ClassBoundCache;
use TheCodingMachine\CacheUtils\ClassBoundMemoryAdapter;
use TheCodingMachine\CacheUtils\ClassBoundCacheContract;

$fileBoundCache = new FileBoundCache($psr6Cache);
$classBoundCache = new ClassBoundMemoryAdapter(new ClassBoundCache($psr6Cache));
$classBoundCacheContract = new ClassBoundCacheContract(new ClassBoundCache($fileBoundCache));

// If the FooBar class is modified, the cache item is purged.
$item = $classBoundCache->get(new ReflectionClass(FooBar::class), function() {
    // ...
    // let's return the item to be cached.
    // this function is called only if the item is not in cache yet.
    return $item;
});

使用缓存合约,没有设置器。只有一个接受参数的可调用参数,如果缓存中没有项目,则调用该参数以解析缓存项。