Share
Embed
Reverse proxy caching with Symfony
Christian
April 26, 2016
Transcript
42 slides
-
if (!($cache->start('mypage'))) { // output everything as usual echo 'Hello
world! '; echo 'This is cached (' .time() . ') '; $cache->end(); // output buffering ends } echo 'This is never cached (' .time() . ').'; -
$cache = Zend_Cache::factory( 'Function', 'Apc', [ 'lifetime' => 7200, 'automatic_serialization'
=> true, 'cached_functions' => [ 'similar_text' ] ] ); -
$cache->call( 'similar_text', [ 'a really long string ...', 'another really
long string ...' ] ); -
$cache = Zend_Cache::factory( 'Class', 'Apc', [ 'lifetime' => 7200, 'automatic_serialization'
=> true, 'cached_entity' => $anInstance ] ); -
$cache = Zend_Cache::factory( 'Page', 'Apc', [ 'lifetime' => 7200, 'automatic_serialization'
=> true, 'default_options' => [ 'cache_with_get_variables' => true, 'make_id_with_get_variables' => true ], 'regexps' => [ '^/index/' => ['cache' => true], '^/article/' => ['cache' => false], ] ] ); -
Symfony relies on the simplicity and power of the HTTP
cache as defined in the HTTP Specification -
// web/app.php require_once __DIR__.'/../app/bootstrap.php.cache'; require_once __DIR__.'/../app/AppKernel.php'; require_once __DIR__.'/../app/AppCache.php'; use Symfony\Component\HttpFoundation\Request;
$kernel = new AppKernel('prod', false); $kernel->loadClassCache(); // wrap the default AppKernel with the AppCache one $kernel = new AppCache($kernel); $request = Request::createFromGlobals(); $response = $kernel->handle($request); $response->send(); $kernel->terminate($request, $response); -
// app/AppCache.php use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache; class AppCache extends HttpCache { protected
function getOptions() { return [ 'debug' => false, 'default_ttl' => 0, 'private_headers' => ['Authorization', 'Cookie'], 'allow_reload' => false, 'allow_revalidate' => false, 'stale_while_revalidate' => 2, 'stale_if_error' => 60, ]; } } -
$response->setLastModified($date); $response->setETag(computeETag()); // Set response as public. Otherwise it will
be private by default. $response->setPublic(); if ($response->isNotModified($request)) { return $response; } -
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; /** * @Cache(expires="tomorrow", public=true) */
class BlogController extends Controller { /** * @Route("/posts/{id}") * @Cache(expires="+2 days") */ public function showAction($id) { // ... } } -
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; /** * @Cache(expires="tomorrow", public=true) */
class BlogController extends Controller { /** * @Route("/posts/{id}") * @Cache( * lastModified="post.getUpdatedAt()", * ETag="'Post' ~ post.getId() ~ post.getUpdatedAt()" * ) */ public function showAction($id) { // ... } } -
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; /** * @Cache(expires="tomorrow", public=true) */
class BlogController extends Controller { /** * @Route("/posts/{id}") * @Cache( * lastModified="post.getUpdatedAt()", * ETag="'Post' ~ post.getId() ~ post.getUpdatedAt()", * expires="+15 days" * ) */ public function showAction($id) { // ... } } -
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class BlogController extends Controller {
/** * @Route("/posts/{id}") * @Cache( * lastModified="post.getUpdatedAt()", * ETag="'Post' ~ post.getId() ~ post.getUpdatedAt()", * smaxage=3600 * ) */ public function showAction($id) { // ... } } -
<!-- Has a TTL of 7200 seconds --> <!DOCTYPE html>
<html> <body> <!-- ... some content --> <!-- Embed the content of another page here —> <!-- Has a TTL of 500 seconds --> <esi:include src="http://..." /> <!-- ... more content --> </body> </html> -
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class BlogController extends Controller {
/** * @Route("/posts/{id}") * @Cache(smaxage=3600) */ public function showAction($id) { // ... } } -
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class CommentsController extends Controller {
/** * @Cache(smaxage=600) */ public function showAction($id) { // ... } } -
Symfony is able to know if is talking with a
Reverse Proxy Cache like Varnish