sensio/SensioFrameworkExtraBundle — pull request #88 by bamarni (2012-01-02 23:07:02+00:00)
Title: [Cache annotation] Added an attribute for cache validation

I have some actions shraing the same cache validation strategy, I'm writing always the same code and have to check if the response is not modified in order to return it for each action, so I thought it could be a good idea to add a validation attribute in the Cache annotation.

Here is an example of a validation class :

class MyValidationStrategy extends ContainerAware implements CacheValidationProviderInterface
{
protected $lastModified;

public function getETag()
{
return null;
}

public function getLastModified()
{
return $this->lastModified;
}

public function process()
{
// retrieve the last modified date
//...

$this->lastModified = $lastModified;

// return parameters that you want to retrieve in the controller
return array('datas' => $doctrineQueryResult);
}
}

And in the controller :

/**
*
* Controller's action
*
* @Cache(
* validation="Company\MainBundle\Cache\ValidationProvider\MyValidationStrategy",
* autoreturn=true,
* public=true
* )
* @Template()
*/
public function indexAction()
{
$someDatas= $this->getRequest()->attributes->get('_cache')->buffer->get('parameters');
}

This would make much less code in controllers and help factorizing the caching validation strategy, what do you think?