Reputation: 1665
Ok I have developed a simple API using Symfony to learn the framework, Now that the API is complete and works I would like to add some Caching in enhance the performance and learn how it is done in Symfony framework environment.
I found this: Cache Symfony
So I have added the Cache annotation on top of my IndexController like so:
/**
* @Cache(expires="+5 minutes", public=true)
*/
class IndexController
{
/**
* @var CreateBookFactory
*/
public $create;
/**
* @param CreateBookFactory $createBookFactory
*/
public function __construct(
CreateBookFactory $createBookFactory,
){
$this->create = $createBookFactory;
}
/**
* @param Request $request
* @return JsonResponse
*/
public function createAction(Request $request)
{
return new JsonResponse($this->create->build($request));
}
}
What I dont understand is how can I check if this actually worked I looked into cache folder which only has dev subfolder but there was nothing raleted to IndexController. Maybe there is a way to check it through a profiler or Maybe its better to use different caching tool for Symfony.
OK so now I understand that the annotation Cache I used is for front end Catching:
Cache-Control:public
Connection:close
Content-Type:application/json
Date:Wed, 27 Jan 2016 15:20:04 GMT
Expires:Wed, 27 Jan 2016 15:25:04 GMT
Upvotes: 1
Views: 915
Reputation: 20286
The symfony cache and app cache are not the same. It's more about headers for browser. If you want to test it then check what headers are send to the browser. Looking at your annotation it will probably send header Expires
You need to understand the difference between front and backend caching.
Frontend cache mostly is good for GET request also there is more advanced cache technique based on ESI with Varnish it can significantly speed your site.
You asked for tool. I'd recommend Friends Of Symfony Cache Bundle
For backend cache I would recommend memcache(very easy cache) for basic cache and redis for more advanced caching.
Upvotes: 1
Reputation: 5668
The @Cache
annotation from the SensioFrameworkExtraBundle
does not actually cache the content. Instead, it adds HTTP cache expiration headers to the response, according to the annotations you give. It can also avoid calls to your controller, in some cases, when the client's Last-Modified
and/or ETag
headers match what's provided in the annotation.
If you want to actually implement server-side caching of data, you have to either implement that yourself, or use a different package that provides that functionality.
Upvotes: 2