{"id":15021938,"url":"https://github.com/paveldanilin/response-cache-bundle","last_synced_at":"2025-04-10T20:33:33.741Z","repository":{"id":57036561,"uuid":"446104116","full_name":"paveldanilin/response-cache-bundle","owner":"paveldanilin","description":"A set of Symfony cache annotations","archived":false,"fork":false,"pushed_at":"2022-02-02T22:18:42.000Z","size":232,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-06T22:32:18.313Z","etag":null,"topics":["annotation","cache","cacheable","cacheevict","php","symfony","symfony-bundle"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/paveldanilin.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-01-09T14:00:06.000Z","updated_at":"2022-08-18T20:53:42.000Z","dependencies_parsed_at":"2022-08-23T21:00:24.459Z","dependency_job_id":null,"html_url":"https://github.com/paveldanilin/response-cache-bundle","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paveldanilin%2Fresponse-cache-bundle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paveldanilin%2Fresponse-cache-bundle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paveldanilin%2Fresponse-cache-bundle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paveldanilin%2Fresponse-cache-bundle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/paveldanilin","download_url":"https://codeload.github.com/paveldanilin/response-cache-bundle/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248290044,"owners_count":21078923,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["annotation","cache","cacheable","cacheevict","php","symfony","symfony-bundle"],"created_at":"2024-09-24T19:57:14.516Z","updated_at":"2025-04-10T20:33:33.712Z","avatar_url":"https://github.com/paveldanilin.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A set of Symfony controller cache annotations\n\n| Annotation  | Description                                                                                                                                                                                                                  |\n|-------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| @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. |\n| @CacheEvict | Removes a cached value from a cache pool.                                                                                                                                                                                    |\n\n## Install\n\n`composer require paveldanilin/response-cache-bundle`\n\n\n## Configuration\n\n```yaml\n# Default values\nresponse_cache:\n  lock:\n    factory: 'lock.response_cache.factory'\n  controller:\n    dir: '%kernel.project_dir%/src'\n```\n\n`lock.factory`: A lock factory service, [read more](https://symfony.com/doc/current/components/lock.html) about lock component configuration.\n\n`controller.dir`: A controller directory for annotation scan.\n\n## Usage\n\n### @Cacheable\nEnables caching behavior for a controller action method.\n\n```php\n@Cacheable(\n    pool=\"\u003ccache.pool.name\u003e\",\n    key=\"\u003citem.key.name\u003e\",\n    ttl=\"\u003cexpires.after.sec\u003e\",\n    condition=\"\u003crequest.condition.expression\u003e\"\n)\n```\n\nThe default parameters:\n- `key` - a concatenated class name with a method name: `App\\Controller\\MyController_doHeavyComputation`\n- `pool` - `cache.app`\n- `ttl` - null (never gets expired)\n- `condition` - null\n\n```php\n/**\n  * @Cacheable()\n  * @Route(\"/api/v1/work\", methods={\"GET\"}, name=\"do.work\")\n  * @return JsonResponse\n  */\npublic function doWork(Request $request): JsonResponse\n{\n    $data = doHeavyComputations();\n    return new JsonResponse($data);\n}\n```\n\n#### Key\nThere are two types of keys:\n- static\n- dynamic\n\n\n**Static key**\n\nThe static key is whether an empty value `\u003cclassName_methodName\u003e` or a string `'\u003cstatic_key\u003e'`.\n\nFor example: `@Cacheable()` or `@Cacheable(key='my_item')`\n\n\n**Dynamic key**\n\nThe dynamic key starts from the '#' sign i.e. `#\u003cexpression\u003e`.\n\n[Symfony expression language](https://symfony.com/doc/current/components/expression_language/syntax.html) is used for the dynamic key computation.\n\nThe following variables can be used in the expression:\n\n| Variable       | Description                | Type                                      |\n|----------------|----------------------------|-------------------------------------------|\n| request        | Inbound request object     | \\Symfony\\Component\\HttpFoundation\\Request |                   |\n | request_method | Http request method        | string                                    |\n | request_uri    | Request URI                | string                                    |\n | request_locale | Request Locale             | string                                    |\n\n\nThe functions can be used in the expression:\n\n| Function                  | Description                                                |\n|---------------------------|------------------------------------------------------------|\n| query_val(key, def = '')  | * *key* string or array of strings\u003cbr/\u003e  * *def* string    |\n| route_val(key, def = '')  | * *key* string or array of strings\u003cbr/\u003e  * *def* string    |\n| body()                    | Returns a request body content                             |\n| body_json(path, def = '') | Decodes a request body as json and returns the path value. |\n\nExamples:\n\n```php\n@Cacheable(key=\"#request.getMethod()~request.getRequestUri()\")\n```\n\n```php\n@Cacheable(key=\"#query_val('id', 'def_value')\")\n```\n\n```php\n@Cacheable(key=\"#query_val(['id', 'x_query_param'])\")\n```\n\n```php\n@Cacheable(key=\"#route_val('id')\")\n```\n\n```php\n@Cacheable(key=\"#route_val(['id', 'x_route_param'])\")\n```\n\n```php\n// Request body: {\"account\": {\"id\": 123}}\n// The resolved key: 123\n@Cacheable(key=\"#body_json('account.id')\")\n```\n\n#### Pool\n\nYou can [define](https://symfony.com/doc/current/cache.html) your own cache pool and use it instead of the default `cache.app`.\nKeep in mind that the cache pool must be defined as public.\n\n#### TTL\n\nTime to live of the cache item in seconds.\nIf not defined will be used a pool default value.\n\n\n#### Condition\n\nIf we want more control over when the annotation is active, we can parameterize @Cacheable with a condition parameter that\ntakes a Symfony expression and ensures that the results are cached based on evaluating that expression.\n\n\n### @CacheEvict\n`@CacheEvict(pool=\u003ccache.pool.name\u003e, key=\u003citem.key.name\u003e)`\n\n\n## Console command\n- `php ./bin/console debug:response-cache`\n![console-output.png](console-output.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaveldanilin%2Fresponse-cache-bundle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpaveldanilin%2Fresponse-cache-bundle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaveldanilin%2Fresponse-cache-bundle/lists"}