Cache the response of the route using a cache rule to define the key.
#[Cache(CacheRule $cacheRule)]
$cacheRule
- The cache rule defines how the cache key is generated.
Attribute::TARGET_METHOD
Code:
#[Method(HttpMethod::GET)]
#[Path('/cache/{cacheId}')]
#[Cache(new CacheRule())]
public function simpleGetWithCacheRule(
#[PathParameter('cacheId')] int $cacheId
): ResponseBuilder {
return (new ResponseBuilder())
->setData($cacheId)
->setContentType(MediaType::APPLICATION_JSON)
->setHttpStatusCode(HttpStatusCode::HTTP_OK);
}
cURL:
curl --request POST \
--location 'http://localhost/cache/123' \
--header 'Content-Type: application/json'