Transcript
Page 1: PhpTour Lyon 2014 - Transparent caching & context aware http cache

“Symfony2  &  eZ  Publish  5  jouent  à  Cache  Cache”Transparent  Caching  &  Context  aware  HTTP  Cache

PHPTour  Lyon  2014  Lightning  talk,  Presented  by  Bertrand  Dunogier  &  André  Rømcke  Expanded  for  Online  consumpQon

Page 2: PhpTour Lyon 2014 - Transparent caching & context aware http cache

About  eZ

•eZ  Systems  AS  :    ez.no  •Used  PHP  since  the  start  in  1999  •Offices  in:  Lyon,  Paris,  New  York,  Oslo,  Skien,  Köln,  Katowice,  Tokyo,  +  suppliers  •Partners:  100+  spread  across  Europe,  America  and  Asia  •Customers:  500+  (EMI,  Orange,  Canal+,  Financial  Times,  Madsack,  ..)  !

•eZ  Publish  v5.x  :    hQps://github.com/ezsystems/ezpublish-­‐community  •5.x  is  Dual  Kernel:  

• “PlaWorm”:  Symfony2,  Doctrine,  Stash..  +  Future  proof  Public  API’s  • “Legacy”:  Latests  version  of  kernel  that  has  powered  eZ  Publish  since  v3.0  (2001)  

•Flexible:  content  model,  workflows,  permission  system,  ..  everything..  •Many  web-­‐features  out  of  the  box  “making  your  life  easier”  •Code  &  Data  BC  kept  since  v3.0,  so@  code  migraeon  from  “legacy”  to  “PlaWorm”  unel  2019  using  last  LTS  release

2With new UI as “eZ Platform”, early alpha summer 2014!

Page 3: PhpTour Lyon 2014 - Transparent caching & context aware http cache

Transparent  cache?

Page 4: PhpTour Lyon 2014 - Transparent caching & context aware http cache

Transparent  cache?

•Not  visible  to  End-­‐,  Editor-­‐,  Admin-­‐User:  “Content  always*  fresh”  •Not  visible  to  Developer:  By  using  predefined  API  &  extension  points  

•Almost,  we’ll  get  back  to  this  one.. *  Recommended  purge  strategy  with  Varnish  will  mark  cache  as  expired  but  keep  serving  the  stale  content  unMl  cache  is  refreshed  from  backend,  this  is  a  must  for  high  traffic  scenarios.

4

Page 5: PhpTour Lyon 2014 - Transparent caching & context aware http cache

What  kind  of  cache  are  we  talking  about  here?

Page 6: PhpTour Lyon 2014 - Transparent caching & context aware http cache

Example  of  app  with  transparent  cache:  eZ  Publish  PlaBorm

6

“Context  aware”  HTTP  Cache

Persistence  (DB  access)

Page 7: PhpTour Lyon 2014 - Transparent caching & context aware http cache

Benefit  for  developers

Page 8: PhpTour Lyon 2014 - Transparent caching & context aware http cache

Benefit  for  developers

               //  Create  a  new  Content  drag  for  the  what  we  want  to  update                  $drag  =  $contentService-­‐>createContentDrag(

               $contentService-­‐>loadContentInfo(15042)        );

               //  Set  a  new  etle                  $struct  =  $contentService-­‐>newContentUpdateStruct();                  $struct-­‐>setField('etle',  ‘PHP  in  2015:  ZendPHPng  vs  HHVM');

                 //  Update  and  publish  the  Content  drag                  $drag  =  $contentService-­‐>updateContent($drag-­‐>versionInfo,  $struct);                  $contentService-­‐>publishVersion($drag-­‐>versionInfo);

8=  No  code  dealing  with  cache  (in  your  business  logic)

Page 9: PhpTour Lyon 2014 - Transparent caching & context aware http cache

But  lets  focus  on  HTTP  Cache,  what  makes  this  work?

Page 10: PhpTour Lyon 2014 - Transparent caching & context aware http cache

Context  Aware  HTTP  Cache  takes  HTTP  Headers  as  context  info

10

X-­‐LocaQon-­‐Id:  42

X-­‐LocaQon-­‐Id:  2Vary:  X-­‐User-­‐Hash

Unique  idenQfier  for  Purge  needs

Vary  Cache  per  User  Rights

Page 11: PhpTour Lyon 2014 - Transparent caching & context aware http cache

How  to  use  this  in  controllers?

Page 12: PhpTour Lyon 2014 - Transparent caching & context aware http cache

Custom  controller  example:  Menu  Controller

     /**            *  Renders  the  top  menu,  with  cache  control            *  @return  \Symfony\Component\HQpFoundaeon\Response            */          public  funceon  topMenuAceon()          {                  //  Get  root  locaeon  id  for  current  site                  $rootLocaeonId  =  $this-­‐>getConfigResolver()-­‐>getParameter('content.tree_root.locaeon_id');

               //  Sewng  HTTP  cache  for  the  response  to  be  public  and  with  a  TTL  of  1  day.                  $response  =  new  Response;                  $response-­‐>setPublic();                  $response-­‐>setSharedMaxAge(86400);

               //  Menu  will  expire  when  top  locaeon  cache  is  purged.                  $response-­‐>headers-­‐>set('X-­‐Locaeon-­‐Id',  $rootLocaeonId);

               //  Menu  might  vary  depending  on  user  permissions,  so  make  the  cache  vary  on  the  user  hash.                  $response-­‐>setVary('X-­‐User-­‐Hash');

12

Page 13: PhpTour Lyon 2014 - Transparent caching & context aware http cache

Example:  Behind  the  scenes  when  publishing  content

Page 14: PhpTour Lyon 2014 - Transparent caching & context aware http cache

Example:  Behind  the  scenes  when  Publishing  Content

•For  affected  Content:  •Clear  Persistence  cache  •Purge  (Stale)  HTTP  caches  using  X-­‐Locaeon-­‐id  (the  “placement(s)”  of  a  Content  object)  across  all  User  variants  

•By  configuraeon:  •Opeonal  rules  for  addieonal  HTTP  caches  to  clear  (Content  Relaeons,  Locaeon  Children,  Locaeon  Parent,  ..),  useful  when  inline  rendering  is  used  instead  of  only  ESI

14

Page 15: PhpTour Lyon 2014 - Transparent caching & context aware http cache

Fin:  Thats  it!

•Recently:  •Collaboraeon  with  JoliCode  and  FOS  community  brought  context  aware  caching  to  FOSHQpCacheBundle  

!

•Next  up:  •Plan  to  parecipate  more  acevely  further  on  adding  more  features  •Start  to  use  in  eZ  Publish  PlaWorm  !

•TL;DR;  Use  FOSHQpCacheBundle  or  use  eZ  Publish  PlaWorm  which  does  all  of  this  for  you  ;)

15