tobento/service-cache
Composer 安装命令:
composer require tobento/service-cache
包简介
Providing PSR-6 and PSR-16 caches for PHP applications.
README 文档
README
Providing PSR-6 and PSR-16 caches for PHP applications.
Table of Contents
Getting started
Add the latest version of the cache service project running this command.
composer require tobento/service-cache
Requirements
- PHP 8.4 or greater
Highlights
- Framework-agnostic, will work with any project
- Decoupled design
Documentation
PSR 6 Cache
Available Cache Item Pools
File Storage Cache Item Pool
The file storage cache item pool using the File Storage Service to store the items.
First, you will need to install service-file-storage:
composer require tobento/service-file-storage
Then, create the File Storage you wish to use for pool:
use Tobento\Service\Cache\FileStorageCacheItemPool; use Tobento\Service\Cache\CanDeleteExpiredItems; use Tobento\Service\Clock\SystemClock; use Tobento\Service\FileStorage\StorageInterface; use Psr\Cache\CacheItemPoolInterface; use Psr\Clock\ClockInterface; $pool = new FileStorageCacheItemPool( // Any storage where to store cache items: storage: $storage, // StorageInterface // A path used as the path prefix for the storage: path: 'cache', // string // The clock used for calculating expiration: clock: new SystemClock(), // ClockInterface // The default Time To Live in seconds, null forever: ttl: null, // null|int ); var_dump($pool instanceof CacheItemPoolInterface); // bool(true) var_dump($pool instanceof CanDeleteExpiredItems); // bool(true)
Check out the Can Delete Expired Items interface to learn more about it.
Storage Cache Item Pool
The storage cache item pool using the Storage Service to store the items.
First, you will need to install service-storage:
composer require tobento/service-storage
Then, create the Storage you wish to use for pool:
use Tobento\Service\Cache\StorageCacheItemPool; use Tobento\Service\Cache\CanDeleteExpiredItems; use Tobento\Service\Clock\SystemClock; use Tobento\Service\Storage\StorageInterface; use Tobento\Service\Storage\JsonFileStorage; use Tobento\Service\Storage\Tables\Tables; use Psr\Cache\CacheItemPoolInterface; use Psr\Clock\ClockInterface; // Create the storage: $tables = new Tables(); $tables->add('cache_items', ['id', 'data', 'expiration', 'namespace'], 'id'); $storage = new JsonFileStorage( dir: __DIR__.'/tmp/', tables: $tables ); $pool = new StorageCacheItemPool( // Any storage where to store cache items: storage: $storage, // StorageInterface // A namespace used as prefix for cache item keys: namespace: 'default', // string // The clock used for calculating expiration: clock: new SystemClock(), // ClockInterface // The default Time To Live in seconds, null forever: ttl: null, // null|int // Specify the table name: table: 'cache_items', // string ); var_dump($pool instanceof CacheItemPoolInterface); // bool(true) var_dump($pool instanceof CanDeleteExpiredItems); // bool(true)
Check out the Can Delete Expired Items interface to learn more about it.
Recommended table column types for (database) storage
| Column | Type |
|---|---|
| id | VARCHAR(255) NOT NULL PRIMARY KEY |
| data | MEDIUMBLOB NOT NULL |
| expiration | TIMESTAMP |
| namespace | VARCHAR(100) NOT NULL |
Array Cache Item Pool
The cache items will be stored in memory and not persisted outside the running PHP process in any way. Might be useful for testing purposes.
use Tobento\Service\Cache\ArrayCacheItemPool; use Tobento\Service\Clock\SystemClock; use Psr\Cache\CacheItemPoolInterface; use Psr\Clock\ClockInterface; $pool = new ArrayCacheItemPool( // The clock used for calculating expiration: clock: new SystemClock(), // ClockInterface // The default Time To Live in seconds, null forever: ttl: null, // null|int ); var_dump($pool instanceof CacheItemPoolInterface); // bool(true)
Psr16 Cache Item Pool
The Psr16 cache item pool using the defined Psr16 cache to store items.
use Tobento\Service\Cache\Psr16CacheItemPool; use Tobento\Service\Cache\CanDeleteExpiredItems; use Tobento\Service\Clock\SystemClock; use Psr\SimpleCache\CacheInterface; use Psr\Cache\CacheItemPoolInterface; use Psr\Clock\ClockInterface; $pool = new Psr16CacheItemPool( cache: $cache, // CacheInterface // A namespace used as prefix for cache item keys: namespace: 'default', // string // The clock used for calculating expiration: clock: new SystemClock(), // ClockInterface // The default Time To Live in seconds, null forever: ttl: null, // null|int ); var_dump($pool instanceof CacheItemPoolInterface); // bool(true) var_dump($pool instanceof CanDeleteExpiredItems); // bool(true)
Cache Item Pools
Create Pools
use Tobento\Service\Cache\CacheItemPools; use Tobento\Service\Cache\CacheItemPoolsInterface; $pools = new CacheItemPools(); var_dump($pools instanceof CacheItemPoolsInterface); // bool(true)
Add Pools
add
use Psr\Cache\CacheItemPoolInterface; $pools->add( name: 'primary', pool: $pool, // CacheItemPoolInterface );
register
You may use the register method to only create the pool if requested.
use Psr\Cache\CacheItemPoolInterface; $pools->register( name: 'name', pool: function(string $name): CacheItemPoolInterface { // create the pool: return $pool; }, );
Get Pool
If the pool does not exist or could not get created it throws a CacheException::class.
use Psr\Cache\CacheItemPoolInterface; use Psr\Cache\CacheException; $pool = $pools->get(name: 'file'); var_dump($pool instanceof CacheItemPoolInterface); // bool(true) $pools->get(name: 'unknown'); // throws CacheException
You may use the has method to check if a pool exists.
var_dump($pools->has('name')); // bool(false)
You may use the getNames method to get all pool names.
var_dump($pools->getNames()); // array(1) { [0]=> string(4) "file" }
Default Pools
You may add default pools for your application design.
use Tobento\Service\Cache\CacheItemPools; use Tobento\Service\Cache\CacheItemPoolsInterface; use Psr\Cache\CacheItemPoolInterface; use Psr\Cache\CacheException; $pools = new CacheItemPools(); // add "file" pool: $pools->add(name: 'file', pool: $pool); // add default: $pools->addDefault(name: 'primary', pool: 'file'); // get default pool for the specified name. $primaryPool = $pools->default('primary'); var_dump($primaryPool instanceof CacheItemPoolInterface); // bool(true) var_dump($pools->hasDefault('primary')); // bool(true) var_dump($pools->getDefaults()); // { ["primary"]=> string(4) "file" } $pools->default('unknown'); // throws CacheException
Interfaces
Cache Item Pool Factory Interface
You may use this interface for creating pools.
namespace Tobento\Service\Cache; use Psr\Cache\CacheItemPoolInterface; use Psr\Cache\CacheException; interface CacheItemPoolFactoryInterface { /** * Create a new CacheItemPool based on the configuration. * * @param string $name * @param array $config * @return CacheItemPoolInterface * @throws CacheException */ public function createCacheItemPool(string $name, array $config = []): CacheItemPoolInterface; }
Cache Item Pools Interface
namespace Tobento\Service\Cache; use Psr\Cache\CacheItemPoolInterface; use Psr\Cache\CacheException; interface CacheItemPoolsInterface { /** * Add a pool. * * @param string $name The pool name. * @param CacheItemPoolInterface $pool * @return static $this */ public function add(string $name, CacheItemPoolInterface $pool): static; /** * Register a pool. * * @param string $name The pool name. * @param callable $pool * @return static $this */ public function register(string $name, callable $pool): static; /** * Returns the pool by name. * * @param string $name The pool name * @return CacheItemPoolInterface * @throws CacheException */ public function get(string $name): CacheItemPoolInterface; /** * Returns true if the pool exists, otherwise false. * * @param string $name The pool name. * @return bool */ public function has(string $name): bool; /** * Adds a default name for the specified pool. * * @param string $name The default name. * @param string $pool The pool name. * @return static $this */ public function addDefault(string $name, string $pool): static; /** * Returns the default pools. * * @return array<string, string> ['general' => 'files'] */ public function getDefaults(): array; /** * Returns the pool for the specified default name. * * @param string $name The type such as pdo. * @return CacheItemPoolInterface * @throws CacheException */ public function default(string $name): CacheItemPoolInterface; /** * Returns true if the default pool exists, otherwise false. * * @param string $name The default name. * @return bool */ public function hasDefault(string $name): bool; /** * Returns the names. * * @return array<int, string> */ public function getNames(): array; }
PSR 16 Simple Cache
Available Caches
Psr6 Cache
The Psr6 cache using the defined Psr6 pool to store items.
use Tobento\Service\Cache\Simple\Psr6Cache; use Tobento\Service\Cache\ArrayCacheItemPool; use Tobento\Service\Cache\CanDeleteExpiredItems; use Tobento\Service\Clock\SystemClock; use Psr\Cache\CacheItemPoolInterface; use Psr\SimpleCache\CacheInterface; use Psr\Clock\ClockInterface; $pool = new ArrayCacheItemPool( clock: new SystemClock(), ); $cache = new Psr6Cache( pool: $pool, // CacheItemPoolInterface // A namespace used as prefix for cache item keys: namespace: 'default', // string // The default Time To Live in seconds, null forever: ttl: null, // null|int ); var_dump($cache instanceof CacheInterface); // bool(true) var_dump($cache instanceof CanDeleteExpiredItems); // bool(true)
Check out the Can Delete Expired Items interface to learn more about it.
Caches
Create Caches
use Tobento\Service\Cache\Simple\Caches; use Tobento\Service\Cache\Simple\CachesInterface; $caches = new Caches(); var_dump($caches instanceof CachesInterface); // bool(true)
Add Caches
add
use Psr\SimpleCache\CacheInterface; $caches->add( name: 'primary', cache: $cache, // CacheInterface );
register
You may use the register method to only create the cache if requested.
use Psr\SimpleCache\CacheInterface; $caches->register( name: 'name', cache: function(string $name): CacheInterface { // create the cache: return $cache; }, );
Get Cache
If the cache does not exist or could not get created it throws a CacheException::class.
use Psr\SimpleCache\CacheInterface; use Psr\SimpleCache\CacheException; $cache = $caches->get(name: 'file'); var_dump($cache instanceof CacheInterface); // bool(true) $caches->get(name: 'unknown'); // throws CacheException
You may use the has method to check if a cache exists.
var_dump($caches->has('name')); // bool(false)
You may use the getNames method to get all cache names.
var_dump($caches->getNames()); // array(1) { [0]=> string(4) "file" }
Default Caches
You may add default caches for your application design.
$caches = new Caches(); // add "file" cache: $caches->add(name: 'file', cache: $cache); // add default: $caches->addDefault(name: 'primary', cache: 'file'); // get default cache for the specified name. $primaryPool = $caches->default('primary'); var_dump($primaryPool instanceof CacheInterface); // bool(true) var_dump($caches->hasDefault('primary')); // bool(true) var_dump($caches->getDefaults()); // { ["primary"]=> string(4) "file" } $caches->default('unknown'); // throws CacheException
Interfaces Simple
Cache Factory Interface
You may use this interface for creating caches.
namespace Tobento\Service\Cache\Simple; use Psr\SimpleCache\CacheInterface; use Psr\SimpleCache\CacheException; interface CacheFactoryInterface { /** * Create a new Cache based on the configuration. * * @param string $name * @param array $config * @return CacheInterface * @throws CacheException */ public function createCache(string $name, array $config = []): CacheInterface; }
Caches Interface
namespace Tobento\Service\Cache\Simple; use Psr\SimpleCache\CacheInterface; use Psr\SimpleCache\CacheException; interface CachesInterface { /** * Add a cache. * * @param string $name The cache name. * @param CacheInterface $cache * @return static $this */ public function add(string $name, CacheInterface $cache): static; /** * Register a cache. * * @param string $name The cache name. * @param callable $cache * @return static $this */ public function register(string $name, callable $cache): static; /** * Returns the cache by name. * * @param string $name The cache name * @return CacheInterface * @throws CacheException */ public function get(string $name): CacheInterface; /** * Returns true if the cache exists, otherwise false. * * @param string $name The cache name. * @return bool */ public function has(string $name): bool; /** * Adds a default name for the specified cache. * * @param string $name The default name. * @param string $cache The cache name. * @return static $this */ public function addDefault(string $name, string $cache): static; /** * Returns the default caches. * * @return array<string, string> ['general' => 'files'] */ public function getDefaults(): array; /** * Returns the cache for the specified default name. * * @param string $name The type such as pdo. * @return CacheInterface * @throws CacheException */ public function default(string $name): CacheInterface; /** * Returns true if the default cache exists, otherwise false. * * @param string $name The default name. * @return bool */ public function hasDefault(string $name): bool; /** * Returns the names. * * @return array<int, string> */ public function getNames(): array; }
Shared Interfaces
Can Delete Expired Items
namespace Tobento\Service\Cache; interface CanDeleteExpiredItems { /** * Removes all expired items from the pool or cache. * * @return bool * True if the items were successfully removed. False if there was an error. */ public function deleteExpiredItems(): bool; }
Credits
tobento/service-cache 适用场景与选型建议
tobento/service-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 106 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 06 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「php」 「cache」 「psr-6」 「psr-16」 「tobento」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tobento/service-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tobento/service-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tobento/service-cache 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Laravel 5 - Repositories to the database layer
Simple ASCII output of array data
Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Laravel, used to abstract the data layer, making applications more flexible to maintain.
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 106
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 4
- 推荐数: 3
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-29