parshikovpavel/array-cache
Composer 安装命令:
composer require parshikovpavel/array-cache
包简介
Non-persistent PSR-6/PSR-16 compatible cache using an array. Useful for mocking the original cache in tests
README 文档
README
The package for caching data in memory using a PHP array.
As cached data is only available in the current request the package should be used for mocking the original cache in tests.
The cache implementation is compatible with PSR-6/PSR-16.
In addition, CounterInterface (excluded from PSR-16) is included in the library and used for implementation.
The table below shows the correspondence between the library classes and the implemented interfaces.
| PSR | Class | Implements |
|---|---|---|
| PSR-6 | CacheItemPool |
CacheItemPoolInterface |
| PSR-6 | CacheItem |
CacheItemInterface |
| PSR-16 | Cache |
CacheInterface |
PSR-16 + excluded CounterInterface |
CountingCache |
CacheInterface + CounterInterface |
Installation
The recommended method of installing is via Composer.
Run the following command from the project root:
composer require parshikovpavel/array-cache --dev
Usage
A detailed description of the interfaces implemented in the library can be found in PSR-6 and PSR-16.
Here are some common usage patterns of the library for testing your application using PHPUnit.
PSR-6
According to PSR-6, the package provides \ppCache\CacheItemPool class (implementing the \PSR\Cache\CacheItemPoolInterface interface) and
\ppCache\CacheItem class (implementing the \PSR\Cache\CacheItemInterface interface).
Cache fixture
Use \ppCache\CacheItemPool instance as a fixture.
Put the creating of the cache fixture into the setUp() method.
final class CacheTest extends TestCase { private $itemPool; protected function setUp(): void { $this->itemPool = new \ppCache\CacheItemPool(); } /* ... */ }
Dependency injection
If your application based on the dependency inversion principle and the dependency injection technique, it's very easy to replace a real cache with a mock cache.
Consider the case of constructor injection. Let's assume Client class provide a parameter in a constructor to inject a cache instance.
final class CacheTest extends TestCase { /* ... */ public function testFeature(): void { $client = new Client($this->itemPool); /* ... */ } }
Get value from cache
The most common cache use case is a getting value and computing in case of cache miss.
A cache client must first try to get value from cache by $key.
-
If trying is successful, return
$value -
otherwise compute
$valueby calling the time-consumingcompute()function and cache the value for a while
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
According to PSR-16, the package provides \ppCache\Cache class (implementing the \Psr\SimpleCache\CacheInterface interface).
Cache fixture
Use \ppCache\Cache instance as a fixture.
Put the creating of the cache fixture into the setUp() method.
final class CacheTest extends TestCase { private $cache; protected function setUp(): void { $this->cache = new \ppCache\Cache(); } /* ... */ }
Dependency injection
Similarly, inject a cache instance into the client constructor:
final class CacheTest extends TestCase { /* ... */ public function testFeature(): void { $client = new Client($this->cache); /* ... */ } }
Get value from cache
The algorithm is the same as the one for \ppCache\CacheItemPool but the implementation is a bit simpler.
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
The package provides the \ppCache\CountingCache class which implements both the \Psr\SimpleCache\CacheInterface
(introduced PSR-16) and the CounterInterface (excluded from PSR-16).
CounterInterface definition is taken from PGP-FIG repository
and is in this package.
The \ppCache\CountingCache implementation decorates a \ppCache\Cache instance and supplements its implementation with
atomic increment-decrement methods.
Cache fixture
final class CacheTest extends TestCase { private $countingCache; protected function setUp(): void { $this->countingCache = new \ppCache\CountingCache(); } /* ... */ }
Dependency injection
final class CacheTest extends TestCase { /* ... */ public function testFeature(): void { $client = new Client($this->countingCache); /* ... */ } }
Value increment and decrement
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); /* ... */ } /* ... */ }
Unit testing
There are unit tests in the ./tests directory. You can run all tests with the following command:
$ ./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
parshikovpavel/array-cache 适用场景与选型建议
parshikovpavel/array-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 10 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cache」 「mock」 「array」 「mocking」 「psr-6」 「psr6」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 parshikovpavel/array-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 parshikovpavel/array-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 parshikovpavel/array-cache 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
repository php library
Trait providing methods to set class properties with an array.
Traits to build collections of specific objects
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-10