Giter Site home page Giter Site logo

meshenka / sf-london-2018-httpcache-demo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from andrerom/sf-london-2018-httpcache-demo

0.0 1.0 0.0 6.98 MB

Symfony Demo Application

Home Page: https://symfony.com/

License: MIT License

PHP 71.79% JavaScript 4.18% CSS 3.02% Shell 0.22% VCL 4.08% HTML 16.71%

sf-london-2018-httpcache-demo's Introduction

Symfony Live London 2018 - Take your Http caching to the next level with xkey & Fastly

Talk: https://joind.in/talk/18336

This is a fork of "Symfony Demo Application":

  • In xkey branch it adds code to show FosHttpCache and Varnish xkey usage, and Docker images to demonstrate it.
  • In fastly branch it adapts that to show an example (simple poc) of how a Fastly Proxy Client for FosHttpCache can be made.

Requirements

For full docker setup usage:

  • Docker + Docker Compose in some 2018 flavour or higher
  • For combining use of local PHP + Docker, use either PHP 7.1 or 7.2 (7.2 is default, see docker-composer.yml to change to 7.1)

For plain PHP web server usage (no caching, but able to see raw headers and browse around the demo):

  • PHP 7.1.3 or higher;
  • PDO-SQLite PHP extension enabled;
  • and the [usual Symfony application requirements][1].

Usage

Installation

$ git clone https://github.com/andrerom/sf-london-2018-httpcache-demo.git
$ cd sf-london-2018-httpcache-demo
$ composer install

At this point, or later if you want, adapt generated .env to enable APP_ENV=prod and APP_DEBUG=0 In prod you'll be able to see things being cached, while in dev you'll be able to see all headers.

Option A: Docker usage
$ docker-compose up --abort-on-container-exit --force-recreate

# To debug calls to Varnish in separate console (see bottom of docker-compose.yml for more info):
$ docker-compose exec varnish varnishlog -g request -q "ReqMethod eq 'PURGEKEYS'"

On Docker for Mac you'll for instance end up with:

Check documentation for your docker installation if in doubt.

Option B: PHP webserver usage (mainly just to see headers)

TODO: Enable AppCache in this repo (only when not using Varnish...), to showcase most feature of FosHttpCache still working.

$ php bin/console server:run

Overview of code changes from Symfony Demo (xkey branch)

TIP You can also see this online in comparison view: https://github.com/symfony/demo/compare/master...andrerom:xkey

A Docker setup with Varnish (with xkey VMOD), Nginx, PHP & MariaDB stack, see:

  • docker-compose.yml
  • docker/*

Adds FosHttpCache by means of:

  • composer require friendsofsymfony/http-cache-bundle guzzlehttp/psr7 php-http/guzzle6-adapter
  • Configure it in config/packages/fos.yml

Adjusts frontend src/Controller/BlogController.php to showcase caching + tagging for:

  • index() action (en/blog/)
  • postShow() action (en/blog/posts/*)

For cache invalidation backend src/Controller/Admin/BlogController.php is modified on:

  • new() clear posts tag (frontend index view)
  • edit() and delete() is modified to invalidate post-<id> and post tags for the two affected frontend views.

Fixes bug in Symfony Demo using flash messages triggering session start:

Enabling User Context Hash feature to explore caching pages for users with sessions and logged in users:

  • config/packages/fos.yml (user_context section)
  • config/packages/security.yaml
  • config/routes/fos.yml
  • docker/varnish/default.vcl (adds fos_user_context*.vcl imports)
  • src/Controller/BlogController.php (adds vary={"X-User-Context-Hash"} on Cache annotation)
  • NOTE: This is broken ATM in the code, so you'll need to remember to delete cookie to receive cached pages right now.
CHALLENGE: Adapt demo Blog Post Comment feature to be cache safe!!

Easy pick?

  1. This issue you should be able to figure out with the info found in code + slides here, but since comments are rendered inline in Blog post post_show.html.twig template, commentNew() action needs to be adapted to invalidate tag for post entities.

Medium to hard? _If cache where fully enabled also when having session/logged-in within the demo, we will run into 2 issues:

  1. New Comment form when you are logged in: This contains a CSRF token, this can not be cached. It will end up with error for other users, and is a security issue as it will expose someone else's token.

  2. Let's say we want to add Flash message to commentNew(), how can you do that in a cache safe way?

Hard? (Effort: Rather large, so take this as a taught experiment to suggest upstream how it could be done) 4. By now the talk + code + the answers you have found above have just made it within reach to see how even the admin backend even can be cached with some careful handling.

How would you approach these? Feel free to open issue to discuss, or even a PR so others can have a look. And who knows, maybe all of this at a later point ends up in Symfony Demo in some form to serve as best practice ;)

sf-london-2018-httpcache-demo's People

Contributors

javiereguiluz avatar bocharsky-bw avatar yceruto avatar voronkovich avatar stof avatar andrerom avatar davialexandre avatar xabbuh avatar dmaicher avatar ogizanagi avatar grafikart avatar wouterj avatar mstrom avatar phpeek avatar b-durand avatar dzuelke avatar jzawadzki avatar hason avatar petk avatar thepetermick avatar elkuku avatar sergiu-popa avatar fabpot avatar avinsol avatar rasanga avatar garak avatar julienitard avatar cristi-contiu avatar xelaris avatar flibustier avatar

Watchers

Sylvain G avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.