Reputation: 14578
I have this controller method that needs to call many other methods (sometimes in other controllers) with $response = $this->forward()
to generate a bigger response.
How would you go about caching all those responses?
For example, should each method handle its own cache? That would be good if the method is also required in other methods, or even as standalone.
Either way, how do I do this? I guess http cache won't work in this case.
Upvotes: 0
Views: 89
Reputation: 3178
Your can use SonataCacheBundle as Cache provider, and replace forward method:
Default example:
<?php
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
/**
* Controller class
*/
abstract class BaseController extends Controller
{
/**
* {@inheritDoc}
*/
public function forward($controller, array $path = array(), array $query = array())
{
// Create a cache key
$key = $controller . md5(serialize($path)) . md5(serialize($query));
$cacheProvider =// Get cache provider (Can SonataCacheBundle)
if (false !== $data = $cacheProvider->get($key)) {
return $data;
}
$data = parent::forward($controller, $path, $query);
$cacheProvider->set($key, $data, time() + 3600);
return $data;
}
}
Upvotes: 1