tourze/symfony-aop-cache-bundle
最新稳定版本:0.2.0
Composer 安装命令:
composer require tourze/symfony-aop-cache-bundle
包简介
基于AOP的Symfony缓存增强包,提供注解式缓存支持和Redis标签清理功能
README 文档
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.
统计信息
- 总下载量: 90
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-08