Upload
andre-r
View
191
Download
6
Embed Size (px)
DESCRIPTION
Lightning talk from PHPTour Lyon 2014, enhanced with more text for online consumption. Topic is transparent caching, and how context aware enhanced Symfony2 HTTP cache helps to archive that and how it works.
Citation preview
“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
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!
Transparent 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
What kind of cache are we talking about here?
Example of app with transparent cache: eZ Publish PlaBorm
6
“Context aware” HTTP Cache
Persistence (DB access)
Benefit for developers
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)
But lets focus on HTTP Cache, what makes this work?
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
How to use this in controllers?
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
Example: Behind the scenes when publishing content
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
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