tourze/cache-strategy
最新稳定版本:1.0.0
Composer 安装命令:
composer require tourze/cache-strategy
包简介
Cache strategy interface
README 文档
README
This package provides an interface for defining cache strategies that help determine which database queries should be cached.
Features
- Simple interface for implementing custom cache strategies
- Built-in
NoCacheStrategyimplementation for basic usage - Symfony DependencyInjection integration with autoconfiguration support
- No external dependencies other than Symfony DependencyInjection
- Compatible with PHP 8.2 and above
Installation
You can install this package using Composer:
composer require tourze/cache-strategy
Requirements
- PHP 8.2 or higher
- Symfony DependencyInjection 6.4 or higher
Quick Start
The core of this package is the CacheStrategy interface which provides a method to determine whether a query should be cached:
<?php use Tourze\CacheStrategy\CacheStrategy; // Using the included NoCacheStrategy which caches all queries $strategy = new \Tourze\CacheStrategy\NoCacheStrategy(); // Query and parameters $query = "SELECT * FROM users WHERE status = ?"; $params = ['active']; // Determine if the query should be cached if ($strategy->shouldCache($query, $params)) { // Cache the query result }
Usage
CacheStrategy Interface
The CacheStrategy interface has a single method:
public function shouldCache(string $query, array $params): bool;
This method takes a query string and parameter array, returning a boolean indicating whether the query should be cached.
Predefined Strategies
The package includes one predefined strategy:
NoCacheStrategy: A simple strategy that always returnstrue, indicating all queries should be cached.
Custom Strategies
You can create your own cache strategies by implementing the CacheStrategy interface:
<?php namespace App\Cache; use Tourze\CacheStrategy\CacheStrategy; class SelectQueryStrategy implements CacheStrategy { public function shouldCache(string $query, array $params): bool { // Only cache SELECT queries return str_starts_with(strtoupper(trim($query)), 'SELECT'); } }
Symfony Integration
The CacheStrategy interface is marked with Symfony's AutoconfigureTag attribute, making it automatically configured as a service with the tag doctrine.cache.entity_cache_strategy:
#[AutoconfigureTag(CacheStrategy::SERVICE_TAG)] interface CacheStrategy { const SERVICE_TAG = 'doctrine.cache.entity_cache_strategy'; // ... }
This allows you to use dependency injection to collect all cache strategy services in your application.
Best Practices
- Create specific cache strategies for different types of queries
- Consider performance implications when implementing complex cache strategies
- Use different strategies for read and write operations
- Test your strategies with real-world query patterns
Testing
The package includes unit tests. To run the tests:
./vendor/bin/phpunit packages/cache-strategy/tests
Contributing
Contributions are welcome! Please see the following guidelines:
- Reporting Issues: Please report bugs through the GitHub issue tracker
- Pull Requests:
- Follow PSR-12 coding standards
- Write tests for new features
- Ensure all tests pass before submitting
- Keep commits focused and well-documented
- Documentation: Update README files when adding new features
Changelog
[0.0.1] - Initial Release
- Initial implementation of
CacheStrategyinterface NoCacheStrategyimplementation- Symfony DependencyInjection integration
- Comprehensive test coverage
License
This package is licensed under the MIT License - see the LICENSE file for details.
统计信息
- 总下载量: 272
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-20