paveldanilin / response-cache-bundle
A Symfony ResponseCache Bundle
Installs: 774
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/paveldanilin/response-cache-bundle
Requires
- php: >=7.4
- ext-json: *
- doctrine/annotations: 1.11.*|1.12.*|1.13.*
- paveldanilin/reflection-scanner: ^0.0
- symfony/console: ^4.4|^5.0
- symfony/expression-language: ^4.4|^5.0
- symfony/framework-bundle: ^4.4|^5.0
- symfony/lock: ^4.4|^5.0
Requires (Dev)
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^9
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2025-10-29 03:18:02 UTC
README
| Annotation | Description |
|---|---|
| @Cacheable | If a key exists in the cache pool and it is not expired returns a cached value instead of a controller action invocation. Otherwise passes to a controller action and puts into the cache pool the result of the invocation. |
| @CacheEvict | Removes a cached value from a cache pool. |
Install
composer require paveldanilin/response-cache-bundle
Configuration
# Default values response_cache: lock: factory: 'lock.response_cache.factory' controller: dir: '%kernel.project_dir%/src'
lock.factory: A lock factory service, read more about lock component configuration.
controller.dir: A controller directory for annotation scan.
Usage
@Cacheable
Enables caching behavior for a controller action method.
@Cacheable(
pool="<cache.pool.name>",
key="<item.key.name>",
ttl="<expires.after.sec>",
condition="<request.condition.expression>"
)
The default parameters:
key- a concatenated class name with a method name:App\Controller\MyController_doHeavyComputationpool-cache.appttl- null (never gets expired)condition- null
/** * @Cacheable() * @Route("/api/v1/work", methods={"GET"}, name="do.work") * @return JsonResponse */ public function doWork(Request $request): JsonResponse { $data = doHeavyComputations(); return new JsonResponse($data); }
Key
There are two types of keys:
- static
- dynamic
Static key
The static key is whether an empty value <className_methodName> or a string '<static_key>'.
For example: @Cacheable() or @Cacheable(key='my_item')
Dynamic key
The dynamic key starts from the '#' sign i.e. #<expression>.
Symfony expression language is used for the dynamic key computation.
The following variables can be used in the expression:
| Variable | Description | Type |
|---|---|---|
| request | Inbound request object | \Symfony\Component\HttpFoundation\Request |
| request_method | Http request method | string |
| request_uri | Request URI | string |
| request_locale | Request Locale | string |
The functions can be used in the expression:
| Function | Description |
|---|---|
| query_val(key, def = '') | * key string or array of strings * def string |
| route_val(key, def = '') | * key string or array of strings * def string |
| body() | Returns a request body content |
| body_json(path, def = '') | Decodes a request body as json and returns the path value. |
Examples:
@Cacheable(key="#request.getMethod()~request.getRequestUri()")
@Cacheable(key="#query_val('id', 'def_value')")
@Cacheable(key="#query_val(['id', 'x_query_param'])")
@Cacheable(key="#route_val('id')")
@Cacheable(key="#route_val(['id', 'x_route_param'])")
// Request body: {"account": {"id": 123}} // The resolved key: 123 @Cacheable(key="#body_json('account.id')")
Pool
You can define your own cache pool and use it instead of the default cache.app.
Keep in mind that the cache pool must be defined as public.
TTL
Time to live of the cache item in seconds. If not defined will be used a pool default value.
Condition
If we want more control over when the annotation is active, we can parameterize @Cacheable with a condition parameter that takes a Symfony expression and ensures that the results are cached based on evaluating that expression.
@CacheEvict
@CacheEvict(pool=<cache.pool.name>, key=<item.key.name>)
