tourze / symfony-aop-cache-bundle
基于AOP的Symfony缓存增强包,提供注解式缓存支持和Redis标签清理功能
Installs: 90
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/symfony-aop-cache-bundle
Requires
- ext-redis: *
- psr/cache: ^2.0 || ^3.0
- symfony/cache: ^7.3
- symfony/cache-contracts: ^3
- symfony/config: ^7.3
- symfony/console: ^7.3
- symfony/dependency-injection: ^7.3
- symfony/framework-bundle: ^7.3
- symfony/http-kernel: ^7.3
- symfony/property-access: ^7.3
- symfony/var-exporter: ^7.3
- symfony/yaml: ^7.3
- tourze/backtrace-helper: 1.*
- tourze/doctrine-helper: 1.0.*
- tourze/symfony-aop-bundle: 1.0.*
- tourze/symfony-cache-hotkey-bundle: 1.0.*
- tourze/symfony-cron-job-bundle: 1.1.*
- tourze/symfony-dependency-service-loader: 1.0.*
- twig/twig: ^3.21
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
- tourze/phpunit-symfony-kernel-test: 1.0.*
- tourze/phpunit-symfony-unit-test: 1.*
README
AopCacheBundle is a Symfony bundle that provides advanced, annotation-driven caching capabilities using AOP (Aspect-Oriented Programming). It enables developers to add cache logic to methods declaratively, supporting cache tags, TTL, custom keys, and forced cache refresh.
Features
- Annotation-based declarative cache (
#[Cacheble],#[CachePut]) - Custom cache key with Twig syntax
- Cache tags for batch management and cleaning
- TTL (expiration) control
- Forced cache refresh support
- Extensible aspects and cache logic
Dependencies
This package requires the following dependencies:
- PHP >= 8.1
- Symfony Framework Bundle >= 6.4
- Symfony Cache Component >= 6.4
- Redis PHP extension
- Twig template engine
- AOP Bundle for aspect-oriented programming support
Installation
Install via Composer:
composer require tourze/symfony-aop-cache-bundle
Enable the Bundle:
// config/bundles.php return [ // ... Tourze\Symfony\AopCacheBundle\AopCacheBundle::class => ['all' => true], // ... ];
Configuration
The bundle automatically integrates with Symfony's cache configuration. Ensure your cache is properly configured:
# config/packages/cache.yaml framework: cache: app: cache.adapter.redis pools: cache.app: adapter: cache.adapter.redis default_lifetime: 3600
Quick Start
Basic Usage
- Add
#[Cacheble]annotation to your method:
use Tourze\Symfony\AopCacheBundle\Attribute\Cacheble; class UserService { #[Cacheble(ttl: 3600, tags: ["user"])] public function getUserProfile(int $userId): array { // business logic } }
- Use custom cache key and tags:
#[Cacheble(key: "profile_{{ userId }}", tags: ["profile", "user_{{ userId }}"])] public function getUserProfile(int $userId): array { // ... }
- Force cache refresh:
use Tourze\Symfony\AopCacheBundle\Attribute\CachePut; #[CachePut(key: "profile_{{ userId }}")] public function updateProfile(int $userId, array $data): array { // ... }
Cache Key Templates
Cache key template supports:
- Access parameters:
{{ paramName }} - Access join point info:
{{ joinPoint.method }},{{ joinPoint.class }} - Supports all Twig syntax
Advanced Usage
Cache Management Commands
The bundle provides a command for batch cache cleaning by tags:
# Clear Redis cache by tags (runs daily at 5:10 AM by default)
php bin/console cache:redis-clear-tags
Extending Cache Logic
You can extend the bundle's functionality by:
- Custom Cache Aspects: Extend
CachebleAspectorCachePutAspect - Custom Cache Traits: Use
CacheTraitfor reusable cache logic - Custom Attributes: Implement
CacheAttributeInterface
Best Practices
- Supported Return Types: Cache simple types (strings, numbers, arrays) or serializable objects
- Avoid Caching: Resource types, callbacks, or complex entity objects
- Performance: Use cache tags for efficient batch invalidation
- Custom Logic: Extend
CacheTrait,CachebleAspect, orCachePutAspectfor custom behavior
Contributing
- Fork the repository
- Create a feature branch
- Ensure all tests pass:
phpunit - Follow PSR-12 coding standards
- Submit a pull request
Please see CONTRIBUTING.md for detailed contribution guidelines.
License
The MIT License (MIT). Please see License File for more information.
Changelog
See CHANGELOG.md for version history and changes.