WARNING: You are browsing the documentation for version 2.1 which is not maintained anymore. If some of your projects are still using this version, consider upgrading.

@Cache

2.1 version
Maintained Unmaintained

@Cache

Usage

The @Cache annotation makes it easy to define HTTP caching:

1
2
3
4
5
6
7
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;

/**
 * @Cache(expires="tomorrow")
 */
public function indexAction()
{
}

You can also use the annotation on a class to define caching for all methods:

1
2
3
4
5
6
/**
 * @Cache(expires="tomorrow")
 */
class BlogController extends Controller
{
}

When there is a conflict between the class configuration and the method configuration, the latter overrides the former:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
/**
 * @Cache(expires="tomorrow")
 */
class BlogController extends Controller
{
    /**
     * @Cache(expires="+2 days")
     */
    public function indexAction()
    {
    }
}

Attributes

Here is a list of accepted attributes and their HTTP header equivalent:

Annotation Response Method
@Cache(expires="tomorrow") $response->setExpires()
@Cache(smaxage="15") $response->setSharedMaxAge()
@Cache(maxage="15") $response->setMaxAge()
@Cache(vary=["Cookie"]) $response->setVary()

Note

The expires attribute takes any valid date understood by the PHP strtotime() function.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.