Slide 1

Slide 1 text

Reverse Proxy Caching with Symfony

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Agenda Revisit traditional caching strategies See how Symfony is different

Slide 4

Slide 4 text

There are several caching strategies

Slide 5

Slide 5 text

Output cache

Slide 6

Slide 6 text

$cache = Zend_Cache::factory(
 'Output',
 'Apc', [
 'lifetime' => 7200,
 'automatic_serialization' => true
 ]
 );

Slide 7

Slide 7 text

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() . ').';

Slide 8

Slide 8 text

Function cache

Slide 9

Slide 9 text

$cache = Zend_Cache::factory(
 'Function',
 'Apc', [
 'lifetime' => 7200,
 'automatic_serialization' => true, 'cached_functions' => [ 'similar_text' ]
 ]
 );

Slide 10

Slide 10 text

$cache->call(
 'similar_text',
 [
 'a really long string ...',
 'another really long string ...'
 ]
 );

Slide 11

Slide 11 text

Class cache

Slide 12

Slide 12 text

$cache = Zend_Cache::factory(
 'Class',
 'Apc', [
 'lifetime' => 7200,
 'automatic_serialization' => true, 'cached_entity' => $anInstance
 ]
 );

Slide 13

Slide 13 text

$cache->anExpensiveMethodCall();

Slide 14

Slide 14 text

Full page cache

Slide 15

Slide 15 text

$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], ] ] );

Slide 16

Slide 16 text

$cache->start();

Slide 17

Slide 17 text

But Symfony has a total different approach

Slide 18

Slide 18 text

Symfony relies on the simplicity and power of the HTTP cache as defined in the HTTP Specification

Slide 19

Slide 19 text

Combines the Expiration and Validation models

Slide 20

Slide 20 text

// 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);

Slide 21

Slide 21 text

// 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, ]; } }

Slide 22

Slide 22 text

Symfony provides useful abstractions to make the life easier

Slide 23

Slide 23 text

Symfony\Component\HttpFoundation\Response

Slide 24

Slide 24 text

$response = Response::create('response content');

Slide 25

Slide 25 text

$response->setPublic();
 $response->setPrivate();

Slide 26

Slide 26 text

$date = new DateTime(); $date->modify('+600 seconds'); $response->setExpires($date);

Slide 27

Slide 27 text

$response->setMaxAge(600); $response->setSharedMaxAge(600);

Slide 28

Slide 28 text

$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; }

Slide 29

Slide 29 text

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
 
 /** * @Cache(expires="tomorrow", public=true) */ class BlogController extends Controller {
 // ... }

Slide 30

Slide 30 text

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) {
 // ...
 } }

Slide 31

Slide 31 text

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) {
 // ...
 } }

Slide 32

Slide 32 text

Combining Expiration and Validation

Slide 33

Slide 33 text

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) {
 // ...
 } }

Slide 34

Slide 34 text

Working with Reverse Proxy caches

Slide 35

Slide 35 text

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) {
 // ...
 } }

Slide 36

Slide 36 text

Symfony Reverse Proxy even supports ESI

Slide 37

Slide 37 text

Slide 38

Slide 38 text

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) {
 // ...
 } }

Slide 39

Slide 39 text

{# AppBundle:Posts:show.html.twig #}
 {{ render_esi(controller('AppBundle:Comments:show', { 'id': post.id() })) }}

Slide 40

Slide 40 text

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
 class CommentsController extends Controller {
 /**
 * @Cache(smaxage=600) */ public function showAction($id) { // ... }
 }

Slide 41

Slide 41 text

Symfony is able to know if is talking with a Reverse Proxy
 Cache like Varnish

Slide 42

Slide 42 text

¡Thanks! Questions?