tourze / symfony-cache-hotkey-bundle
自动扩展Cache服务
Installs: 238
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/symfony-cache-hotkey-bundle
Requires
- ext-mbstring: *
- monolog/monolog: ^3.1
- psr/cache: ^2.0 || ^3.0
- psr/log: ^3|^2|^1
- symfony/cache: ^7.3
- symfony/cache-contracts: ^3
- symfony/config: ^7.3
- symfony/dependency-injection: ^7.3
- symfony/framework-bundle: ^7.3
- symfony/http-kernel: ^7.3
- symfony/property-access: ^7.3
- symfony/service-contracts: ^3.6
- symfony/yaml: ^7.3
- tourze/bundle-dependency: 1.*
- tourze/symfony-dependency-service-loader: 1.0.*
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
- tourze/phpunit-symfony-kernel-test: 1.0.*
- tourze/phpunit-symfony-unit-test: 1.*
README
[]
(https://github.com/tourze/php-monorepo/actions)
[]
(https://coveralls.io/github/tourze/php-monorepo?branch=master)
A Symfony Bundle designed to solve cache hotkey issues by automatically distributing hot keys across multiple sub-keys, effectively preventing a single cache node from being overloaded.
Features
- Automatically detects hot keys (keys starting with
hotkey_) - Replicates hot key content to multiple sub-keys for load balancing
- Randomly selects a sub-key for reads
- Supports cache size monitoring and alerting
- Supports cache tag invalidation logging
Installation
Requirements
- PHP 8.1 or higher
- Symfony 6.4 or higher
- PSR-3 Logger
- Symfony Cache component
Using Composer
composer require tourze/symfony-cache-hotkey-bundle
Quick Start
Bundle Registration
Add the bundle to your config/bundles.php:
return [ // ... other bundles Tourze\Symfony\CacheHotKey\CacheHotKeyBundle::class => ['all' => true], ];
Marking a Hot Key
Simply add the hotkey_ prefix to your cache key:
// Normal cache usage $cache->get('normal_key', fn() => 'value'); // Hot key cache usage $cache->get('hotkey_popular_data', fn() => 'value');
This bundle will automatically:
- Replicate the hot key content to 10 sub-keys (
hotkey_popular_data_0_splittohotkey_popular_data_9_split) - Randomly select a sub-key when reading
- Clean up all sub-keys when the main key is deleted
Configuration
Main Configuration (Environment Variables)
CACHE_MARSHALLER_WARNING_VALUE_SIZE=1048576 # Cache serialization warning threshold (bytes) CACHE_MARSHALLER_WARNING_DEMO_SIZE=400 # Cache content preview size (bytes) CACHE_INVALIDATE_TAG_LOG=false # Enable cache tag invalidation logging
Advanced Usage
Cache Tag Invalidation
- Supports cache tag invalidation with optional logging (
CACHE_INVALIDATE_TAG_LOG) - Automatically logs a warning if cache value size exceeds threshold, helping you optimize data structures
Performance Tips
- For high concurrency scenarios, only use the
hotkey_prefix for truly hot data to avoid unnecessary sharding - You can customize the number of shards by adjusting the
MAX_KEYconstant
Contributing
Issues and PRs are welcome. Please follow PSR coding standards and ensure PHPUnit tests pass before submitting.
License
MIT License © Tourze
Changelog
See Releases for details