parshikovpavel/array-cache

使用数组的非持久PSR-6/PSR-16兼容缓存。适用于在测试中模拟原始缓存

0.0.1 2019-10-10 12:56 UTC

This package is auto-updated.

Last update: 2024-09-11 00:24:25 UTC


README

使用PHP数组在内存中缓存数据的包。

由于缓存的值仅在当前请求中可用,因此该包适用于在测试中模拟原始缓存。

缓存实现与PSR-6/PSR-16兼容。此外,库中还包括CounterInterface(从PSR-16中排除)(已排除),并在实现中使用。

下表显示了库类与实现接口之间的对应关系。

安装

推荐的安装方法是使用Composer。

从项目根目录运行以下命令

composer require parshikovpavel/array-cache --dev

用法

库中实现的接口的详细描述可以在PSR-6PSR-16中找到。

以下是使用PHPUnit测试您的应用程序时库的常见用法模式。

PSR-6

根据PSR-6,该包提供了\ppCache\CacheItemPool类(实现\PSR\Cache\CacheItemPoolInterface接口)和\ppCache\CacheItem类(实现\PSR\Cache\CacheItemInterface接口)。

缓存测试用例

\ppCache\CacheItemPool实例用作测试用例。将创建缓存测试用例的操作放在setUp()方法中。

final class CacheTest extends TestCase
{
    private $itemPool;

    protected function setUp(): void
    {
        $this->itemPool = new \ppCache\CacheItemPool();
    }
    
    /* ... */
}

依赖注入

如果您的应用程序基于依赖反转原则和依赖注入技术,则用模拟缓存替换真实缓存非常容易。

考虑构造函数注入的情况。假设Client类在构造函数中提供一个参数以注入缓存实例。

final class CacheTest extends TestCase
{
    /* ... */
    
    public function testFeature(): void
    {
        $client = new Client($this->itemPool);
        
        /* ... */
    }
}

从缓存中获取值

最常见的缓存用法是获取值和计算(在缓存未命中时)。

缓存客户端必须首先尝试通过$key从缓存中获取值。

  • 如果尝试成功,则返回$value

  • 否则通过调用耗时的compute()函数来计算$value,并将值缓存一段时间

final class Client {

    private $itemPool;

    public function __construct(\Psr\Cache\CacheItemPoolInterface $itemPool)
    {
        $this->itemPool = $itemPool;
    }

    private function getValue(string $key, int $ttl = 3600)
    {
        $item = $this->itemPool->getItem($key);
        if (!$item->isHit()) {
            $value = $this->compute();
            $item->set($value);
            $item->expiresAfter($ttl);
            $this->itemPool->save($item);
        }
        return $item->get();
    }

    /* ... */
}

PSR-16

根据PSR-16,该包提供了\ppCache\Cache类(实现\Psr\SimpleCache\CacheInterface接口)。

缓存测试用例

\ppCache\Cache实例用作测试用例。将创建缓存测试用例的操作放在setUp()方法中。

final class CacheTest extends TestCase
{
    private $cache;

    protected function setUp(): void
    {
        $this->cache = new \ppCache\Cache();
    }
    
    /* ... */
}

依赖注入

同样,将缓存实例注入客户端构造函数

final class CacheTest extends TestCase
{
    /* ... */
    
    public function testFeature(): void
    {
        $client = new Client($this->cache);
        
        /* ... */
    }
}

从缓存中获取值

算法与\ppCache\CacheItemPool相同,但实现更简单。

final class Client
{
    private $cache;

    public function __construct(\Psr\SimpleCache\CacheInterface $cache)
    {
        $this->cache = $cache;
    }

    private function getValue(string $key, int $ttl = 3600)
    {
        if (null === ($value = $this->cache->get($key))) {
            $value = $this->compute();
            $this->cache->set($key, $value, $ttl);
        }

        return $value;
    }

    /* ... */
}

PSR-16 + CounterInterface

该包提供了\ppCache\CountingCache类,该类实现了PSR-16中引入的\Psr\SimpleCache\CacheInterface和从PSR-16中排除的CounterInterface。从PGP-FIG存储库中获取了CounterInterface的定义,并将其包含在本包中。

\ppCache\CountingCache实现装饰了一个\ppCache\Cache实例,并通过原子增减方法补充了其实现。

缓存测试用例

final class CacheTest extends TestCase
{
    private $countingCache;

    protected function setUp(): void
    {
        $this->countingCache = new \ppCache\CountingCache();
    }
    
    /* ... */
}

依赖注入

final class CacheTest extends TestCase
{
    /* ... */
    
    public function testFeature(): void
    {
        $client = new Client($this->countingCache);
        
        /* ... */
    }
}

值增减

final class Client
{
    private $countingCache;

    public function __construct(\ppCache\CountingCache $countingCache)
    {
        $this->countingCache = $countingCache;
    }

    private function changeValue(string $key): void
    {
        /* ... */ 
        
        $newValue = $this->countingCache->increment($key);
        
        /* ... */
        
        $newValue = $this->countingCache->decrement($key);
        
        /* ... */
    }

    /* ... */
}

单元测试

./tests 目录中存在单元测试。您可以使用以下命令运行所有测试

$ ./vendor/bin/phpunit tests/ --testdox

ppCache\CacheItemPool
 ✔ Throws exception for invalid key
 ✔ Detects missing item
 ✔ Saves detects retrieves an eternal item
 ✔ Detects an expired item

ppCache\Cache
 ✔ Throws exception for invalid key
 ✔ Detects missing item
 ✔ Saves detects retrieves an eternal item
 ✔ Detects an expired item
 ✔ Gets items with a specified expiration time
 ✔ Supports multiple functions
 ✔ Performs deletion and clearing

ppCache\CountingCache
 ✔ Increments a value
 ✔ Decrements a value