alyakin/dictionary-cache-service
Composer 安装命令:
composer require alyakin/dictionary-cache-service
包简介
Dictionary caching service for Redis-compatible stores (Redis, KeyDB, Valkey, etc.).
README 文档
README
Dictionary caching based on Redis-compatible stores (Redis, KeyDB, Valkey, Dragonfly, Ardb, etc.).
Contents
- Installation
- Examples
- Methods
- Creating an object
- Setup scope for cache
- Set data provider
- Set cache Time To Live (TTL)
- Get cache Time To Live (TTL)
- Manually preload cache
- Check one item in cache
- What items from the list are in the cache
- Get all elements from cache
- Checking if cache is loaded
- Manually add elements to cache
- Manually remove element from the cache
- Reset TTL countdown
- Clear cache for the scope
- Supported Databases
- Requirements
- Contributing
- License
Installation
Install via Composer:
composer require alyakin/dictionary-cache-service
This package is framework-agnostic: it does not bootstrap a Redis connection on its own. Provide an implementation of Alyakin\DictionaryCache\Contracts\RedisClientInterface (or one of the bundled adapters) when constructing the service.
Available adapters:
Alyakin\DictionaryCache\Adapters\IlluminateRedisClient- wrap Laravel'sRedis::connection().Alyakin\DictionaryCache\Adapters\PhpRedisClient- reuse the native\Redis/\RedisClusterextension.- Custom implementations - implement the interface if you rely on another Redis library.
Examples
flowchart LR
A[Provider / manual wiring] --> B(DictionaryCacheService)
B --> C[setContext / setTTL]
B --> D[setDataProvider]
D -->|optional| E[(Data source)]
B --> F[RedisClientInterface]
F --> G[IlluminateRedisClient]
F --> H[PhpRedisClient]
G --> I[(Laravel Redis connection)]
H --> J[(Redis / KeyDB / Valkey / Dragonfly)]
Loading
sequenceDiagram
participant Client
participant Cache as DictionaryCacheService
participant Provider as Data provider / DB
Client->>Cache: hasItems(ids)
Cache-->>Client: cache miss (not initialized)
Cache->>Provider: preload() fetch dataset
Provider-->>Cache: array of items
Cache-->>Client: first response (stored in Redis)
Client->>Cache: hasItems(ids)
Cache-->>Client: hit from Redis (no DB call)
Client->>Cache: hasItems(ids)
Cache-->>Client: hit from Redis (microseconds latency)
Loading
Laravel: warm a dictionary from the database
Register the bundled service provider (auto-discovered in Laravel packages) and resolve the service from the container. The provider injects an IlluminateRedisClient that wraps Redis::connection().
use Alyakin\DictionaryCache\Adapters\PhpRedisClient; use Alyakin\DictionaryCache\Services\DictionaryCacheService; $cartCache = app(DictionaryCacheService::class); $cartCache ->setContext("user_{$userId}", 'cart') ->setDataProvider(function () use ($userId) { return UserCart::whereUserId($userId) ->pluck('product_id') ->map(fn ($id) => (string) $id) ->all(); }) ->setTTL(3600); $cartCache->preload(); if ($cartCache->hasItem((string) $productId)) { // product already in cart }
Plain PHP: manual dictionary without a data provider
use Alyakin\DictionaryCache\Adapters\PhpRedisClient; use Alyakin\DictionaryCache\Services\DictionaryCacheService; $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); $flagsCache = new DictionaryCacheService( redisInstance: new PhpRedisClient($redis) ); $flagsCache->setContext("tenant_{$tenantId}", 'feature_flags') ->setTTL(600) ->addItems(['new_checkout', 'promo_banner']); $enabledFlags = $flagsCache->hasItems(['new_checkout', 'beta_feed']); if (in_array('new_checkout', $enabledFlags, true)) { // enable experiment } $flagsCache->keepAlive(); // refresh TTL to keep feature flags hot
Methods
__construct
Initializes the service with optional context, data provider, and Redis connection.
Parameters:
contextId(optional, string): Unique identifier for the cache.dataProvider(optional, Closure): Function that returns an array of items to be cached.redisInstance(optional, RedisClientInterface|\Redis|\RedisCluster): Provide your own Redis client or adapter. When omitted inside Laravel, the bundled service provider injects the default connection.
use Alyakin\DictionaryCache\Services\DictionaryCacheService; // $redis is an existing \Redis or compatible connection. $userCartCache = new DictionaryCacheService( contextId: $userId, dataProvider: $myDataProviderCallback, redisInstance: new PhpRedisClient($redis) );
setContext
Sets the cache key using a context ID and an optional prefix. All class methods use the scope set by this method.
Parameters:
contextId(required, string): Unique identifier for the context.key(optional, string): Prefix for the cache key (default:dictionary).
$userCartCache->setContext('user_'.$userId, 'cart'); $userFavoriteProductsCache->setContext('user_'.$userId, 'favorite_products');
setDataProvider
Sets a function that provides data for cache preloading. This method will only be called if the cache has not been initialized yet.
Parameters:
dataProvider(required, Closure): Function returning an array of items.
$userCartCache->setDataProvider( function () use ($userId) { return UserCart::whereUserId($userId)->pluck('id')->toArray(); } );
setTTL
Sets the TTL (time-to-live) for the cache key.
Parameters:
ttl(required, int): TTL in seconds (must be >= 1).
$userCartCache = new DictionaryCacheService(); $userCartCache ->setContext('user_'.$userId, 'cart') ->setDataProvider(fn () => ['19', '33', '7']) ->setTTL(3600 * 24);
getTTL
Retrieves the TTL of the cache key. If not set, returns default (3600).
preload
Loads data into the cache using the data provider if it is not initialized. Throws a RuntimeException when a data provider was not configured.
hasItem
Checks if a specific item exists in the cache.
Parameters:
itemId(required, string): Item to check.
$inCart = $userCartCache->hasItem($productId); return $inCart;
hasItems
Checks which items from the list exist in the cache.
Parameters:
itemIds(required, array): List of item IDs.
$productList = Product::recommendedFor($productId)->get()->pluck('id')->toArray(); $productsInCart = $userCartCache->hasItems($productList); $recommendations = array_diff($productList, $productsInCart); return $recommendations;
getAllItems
Retrieves all cached items.
exists
Checks if the cache exists for the scope.
addItems
Adds multiple items to the cache.
public function handle(ProductAddedToCart $event): void { $this->cartCache->setContext("user_{$event->userId}", 'cart'); if ($this->cartCache->exists()) { $this->cartCache->addItems([(string) $event->productId]); } }
Parameters:
items(required, array): Items to add.
removeItem
Removes a specific item from the cache.
Parameters:
item(required, string): Item to remove.
$this->cartCache->removeItem((string) $event->productId);
keepAlive
Refreshes the expiration time of the cache key without modifying TTL.
$this->cartCache->removeItem((string) $event->productId)->keepAlive();
clear
Clears the cached data but keeps TTL settings.
Supported Databases
The service works with Redis-compatible databases supported by Laravel's Redis driver:
- Redis (all versions)
- KeyDB
- Valkey
- Dragonfly (tested with Redis-compatible API)
- Ardb (not covered by CI; no maintained public Docker image available)
Requirements
- PHP 7.4+
- Redis-compatible storage
- Laravel 8+ (optional, only for automatic service provider registration)
Development
Install dependencies and run the local toolchain:
composer install
composer check # runs lint + phpstan + tests
Individual commands:
composer lint– Laravel Pint (pint --testin CI).composer stan– PHPStan level 9.composer test– PHPUnit (unit + integration; requires a running Redis-compatible server and ext-redis).
GitHub Actions (.github/workflows/ci.yml) runs a matrix of PHP 8.1–8.3 against Redis/KeyDB/Valkey/Dragonfly. Ardb is excluded because there is no publicly available Docker image; test it manually if your project relies on it.
Want to Contribute?
Read CONTRIBUTING.md for setup instructions, coding style, and testing requirements. Please follow the Code of Conduct and report security issues privately via SECURITY.md.
Check out the open issues - especially those labeled good first issue!
Feel free to fork the repo, open a PR, or suggest improvements.
License
This package is open-source and available under the MIT License.
alyakin/dictionary-cache-service 适用场景与选型建议
alyakin/dictionary-cache-service 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「cache」 「dictionary」 「caching」 「DragonFly」 「valkey」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alyakin/dictionary-cache-service 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alyakin/dictionary-cache-service 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alyakin/dictionary-cache-service 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple, immutable data structures
repository php library
Microservice RPC through message queues.
The CodeIgniter Redis package
Dictionary implementation for PHP
贝嘟分布式缓存扩展
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-07