Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reverse proxy caching with Symfony

Reverse proxy caching with Symfony

Christian

April 26, 2016
Tweet

More Decks by Christian

Other Decks in Technology

Transcript

  1. 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() . ').';
  2. $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], ] ] );
  3. Symfony relies on the simplicity and power of the HTTP

    cache as defined in the HTTP Specification
  4. // 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);
  5. // 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, ]; } }
  6. $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; }
  7. 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) {
 // ...
 } }
  8. 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) {
 // ...
 } }
  9. 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) {
 // ...
 } }
  10. 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) {
 // ...
 } }
  11. <!-- 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>
  12. 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) {
 // ...
 } }
  13. Symfony is able to know if is talking with a

    Reverse Proxy
 Cache like Varnish