danilovl / cache-response-bundle
Symfony bundle provides cache controller response
Installs: 70
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.3
- symfony/cache: ^7.0
- symfony/console: ^7.0
- symfony/event-dispatcher: ^7.0
- symfony/framework-bundle: ^7.0
- symfony/http-foundation: ^7.0
- symfony/http-kernel: ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.64
- phpstan/extension-installer: ^1.4.3
- phpstan/phpstan: ^2.0.1
- phpstan/phpstan-symfony: ^2.0.0
- phpunit/phpunit: ^10.2
README
CacheResponseBundle
About
Symfony bundle provides simple cache response.
Before:
After:
Requirements
- PHP 8.3 or higher
- Symfony 7.0 or higher
1. Installation
Install danilovl/cache-response-bundle
package by Composer:
composer require danilovl/cache-response-bundle
Add the CacheResponseBundle
to your application's bundles if does not add automatically:
<?php // config/bundles.php return [ // ... Danilovl\CacheResponseBundle\CacheResponseBundle::class => ['all' => true] ];
2. Usage
You can define custom cache service witch implement CacheItemPoolInterface
.
Default cache service name for all attributes. You can leave it empty. The DI sets the default cache service to CacheItemPoolInterface, which is defined in your application.
# config/packages/danilovl_cache_response.yaml danilovl_cache_response: enable: true/false cache_adapter: 'Class::class' kernel_controller_priority: 0 kernel_response_priority: 0
CacheResponseAttribute
attributes:
| Parameter | Type | Default | Require | Description |
| ----------------- | ---------------------- | ------- | --------------- | ----------------------------------------------------------------------- |
| $key | ?string | null | yes || factory | A custom cache key. If null, a key will be generated automatically. |
| $factory | ?string | null | yes || key | The class of the factory used to generate the value. |
| $cacheAdapter | ?string | null | no | The class of the cache adapter. |
| $expiresAfter | int|DateInterval|null | null | no | Time after which the value expires. Can be seconds or a DateInterval. |
| $expiresAt | ?DateTimeInterface | null | no | Exact expiration time for the value. |
| $useSession | bool | false | no | Whether to include session data in the cache key generation. |
| $useRoute | bool | false | no | Whether to include the current route in the cache key. |
| $useQuery | bool | false | no | Whether to include query parameters in the cache key. |
| $useRequest | bool | false | no | Whether to include full request data in the cache key. |
| $useEnv | bool | false | no | Whether to include environment variables in the cache key. |
| $disableOnQuery | bool | false | no | Disable cache entirely if GET (query) parameters are present. |
| $disableOnRequest | bool | false | no | Disable cache entirely if POST (request body) parameters are present. |
|--------------------------------------------------------------------------------------------------------------------------------------------------|
2.1 Controller
Add attribute CacheResponseAttribute
to controller method.
#[CacheResponseAttribute( key: 'index', expiresAfter: 60, useSession: true, useRoute: true, useQuery: true, useRequest: true, useEnv: true )] public function index(Request $request): Response { return new Response('content'); }
Or better solution if you have duplicate controller name and method name.
#[CacheResponseAttribute( key: __METHOD__, expiresAfter: 60, useQuery: true, useRequest: true, useEnv: true )] public function index(Request $request): Response { return new Response('content'); }
Use custom factory service for create cache key. Must implements interface CacheKeyFactoryInterface
.
#[CacheResponseAttribute(factory: CachKeyFactoryClass::class)] public function index(Request $request): Response { return new Response('content'); }
2.2 Command
Show all used CacheResponseAttribute
cache key names.
php bin/console danilovl:cache-response:list
Clear all CacheResponseAttribute
cache.
php bin/console danilovl:cache-response:clear --all=true
Clear only specific CacheResponseAttribute
cache key name.
php bin/console danilovl:cache-response:clear --cacheKey=index
Clear all similar CacheResponseAttribute
cache key name.
0 => "danilovl.cache_response.8414b2ff0a6fafcddc0f42d6d5a5b908d34925c3" 1 => "danilovl.cache_response.8414b2ff08997b2bd029eaab1a04598a500a0034"
php bin/console danilovl:cache-response:clear --similarCacheKey=8414b2ff0
0 => "danilovl.cache_response.5f9cf7121290f93c" 1 => "danilovl.cache_response.a202b43aa495f0f3"
php bin/console danilovl:cache-response:clear --cacheKey=similar
2.3 EventSubscriber
Clear all cache.
$this->eventDispatcher->dispatch(new ClearCacheResponseAllEvent);
Clear only specific cache key.
$this->eventDispatcher->dispatch(new ClearCacheResponseKeyEvent('cache_key'));
License
The CacheResponseBundle is open-sourced software licensed under the MIT license.