I have a REST API using the FOSREST Bundle. The response is serialized by the JMS Bundle. Now I want to cache some of the responses because serialization takes quite some time. I was thinking of storing the response data after serialization and then, if database hasn't changed, deliver the cached response instead of doing serialization again. Any ideas how to achieve this?
To be more specific: I have a Controller which outputs a lot of data which is unchanged most of the time. Since serialization takes a few seconds I want to speed up things using the cached output.
I think I need to setup an event "afterSerialization" to write the cache. But because fosrestBundle is configured to serialize all data handled by the view, I don't find a method to output pre serialized data without beeing sent to the serializer again.
public function postProductsAction(Request $request)
{
$em = $this->get('doctrine')->getManager();
// TODO check if db is unchanged and cache exists
// if check is true stop here and redirect to cached json response
$products = $em->getRepository('PPApiBundle:Produkte')->findBy(array("status" => 1));
$data = array(
"data" => $products,
"opt" => array()
);
$view = $this->view($data, 200);
$ret = $this->handleView($view);
// TODO cache json string after serialization
return $ret
}