Slide 1

Slide 1 text

CACHING IN SYMFONY EIN ÜBERBLICK Denis Brumann // @dbrumann // Caching in Symfony

Slide 2

Slide 2 text

SYMFONY CACHE Denis Brumann // @dbrumann // Caching in Symfony

Slide 3

Slide 3 text

The Cache component provides a strict PSR-6 implementation for adding cache to your applications. Denis Brumann // @dbrumann // Caching in Symfony SYMFONY 3.1 The Cache component provides an extended PSR-6 implementation as well as a PSR-16 "Simple Cache" implementation for adding cache to your applications. SYMFONY 3.3

Slide 4

Slide 4 text

Denis Brumann // @dbrumann // Caching in Symfony ADAPTER • Symfony‘s terminology for concrete cache backends. • Configured once and then reused by pools • Never used directly by the end-user • Developers interact with adapter through a pool

Slide 5

Slide 5 text

Denis Brumann // @dbrumann // Caching in Symfony APCU ADAPTER It is not recommended to use this adapter when performing a large number of write and delete operations, as these operations result in fragmentation of the APCu memory, resulting in significantly degraded performance.
 Note that this adapter's CRUD operations are specific to the PHP SAPI it is running under. This means adding a cache item using the CLI will not result in the item appearing under FPM. Likewise, deletion of an item using CGI will not result in the item being deleted under the CLI. https://symfony.com/doc/current/components/cache/adapters/apcu_adapter.html

Slide 6

Slide 6 text

Denis Brumann // @dbrumann // Caching in Symfony MEMCACHED & REDIS ADAPTER • Unlike APCu adapter not limited to the current server's shared memory • Ability to utilize a cluster of servers to provide redundancy and/or fail-over

Slide 7

Slide 7 text

Denis Brumann // @dbrumann // Caching in Symfony FILESYSTEM ADAPTER • Useful when APCu, Redis or Memcached are not available on the server • Stores content on the local filesystem This adapter is generally the slowest due to the overhead of file IO

Slide 8

Slide 8 text

Denis Brumann // @dbrumann // Caching in Symfony ARRAY ADAPTER • Intended for testing purposes • Items are stored in memory and not persisted outside the running process

Slide 9

Slide 9 text

Denis Brumann // @dbrumann // Caching in Symfony CHAIN ADAPTER • Combine any number of other available adapters • Items are fetched from the first adapter with hit • Items are saved in all the given adapters

Slide 10

Slide 10 text

Denis Brumann // @dbrumann // Caching in Symfony PSR-16 INTEROPERABILITY • From PSR-16 to PSR-6:
 SimpleCacheAdapter (wraps PSR-16 CacheInterface)

Slide 11

Slide 11 text

PSR-6 Denis Brumann // @dbrumann // Caching in Symfony

Slide 12

Slide 12 text

The goal of this PSR is to allow developers to create cache-aware libraries that can be integrated into existing frameworks and systems without the need for custom development. http://www.php-fig.org/psr/psr-6/ Denis Brumann // @dbrumann // Caching in Symfony GOALS

Slide 13

Slide 13 text

Denis Brumann // @dbrumann // Caching in Symfony • Compatibility with all existing caching implementations • Advanced caching features such as namespaces and tags NON-GOALS

Slide 14

Slide 14 text

TERMINOLOGY Denis Brumann // @dbrumann // Caching in Symfony

Slide 15

Slide 15 text

The Pool represents a collection of items in a caching system. The pool is a logical Repository of all items it contains. All cacheable items are retrieved from the Pool as an Item object, and all interaction with the whole universe of cached objects happens through the Pool. http://www.php-fig.org/psr/psr-6/ Denis Brumann // @dbrumann // Caching in Symfony CACHE ITEM POOL

Slide 16

Slide 16 text

http://www.php-fig.org/psr/psr-6/ Denis Brumann // @dbrumann // Caching in Symfony CACHE ITEM An Item represents a single key/value pair within a Pool. The key is the primary unique identifier for an Item and MUST be immutable. The Value MAY be changed at any time.

Slide 17

Slide 17 text

http://www.php-fig.org/psr/psr-6/ Denis Brumann // @dbrumann // Caching in Symfony DEFERRED SAVE A deferred cache save indicates that a cache item may not be persisted immediately by the pool. A Pool object MAY delay persisting a deferred cache item in order to take advantage of bulk-set operations supported by some storage engines.

Slide 18

Slide 18 text

http://www.php-fig.org/psr/psr-6/ Denis Brumann // @dbrumann // Caching in Symfony DEFERRED SAVE A Pool MUST ensure that any deferred cache items are eventually persisted and data is not lost, and MAY persist them before a Calling Library requests that they be persisted.

Slide 19

Slide 19 text

Denis Brumann // @dbrumann // Caching in Symfony KEYS / REQUIREMENTS • UTF-8 encoded string of at least one character • Uniquely identifies a cached item • must support ‘A-Z’, ‘a-z’, ‘0-9’, ‘_’ and ‘.’ • reserved characters: ‘{}()/\@:’ • Length up to 64 characters (supporting more is allowed)

Slide 20

Slide 20 text

Denis Brumann // @dbrumann // Caching in Symfony VALUES / ALLOWED TYPES • Skalare Datentypen: Strings, Integer, Floats, Booleans • NULL • Arrays • Objects
 implement Serializable interface
 or __sleep() & __wakeup()

Slide 21

Slide 21 text

Denis Brumann // @dbrumann // Caching in Symfony DATA All data passed into the Implementing Library MUST be returned exactly as passed. That includes the variable type. That is, it is an error to return (string) 5 if (int) 5 was the value saved. […] If it is not possible to return the exact saved value for any reason, implementing libraries MUST respond with a cache miss rather than corrupted data. http://www.php-fig.org/psr/psr-6/

Slide 22

Slide 22 text

INTERFACES Denis Brumann // @dbrumann // Caching in Symfony

Slide 23

Slide 23 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 24

Slide 24 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 25

Slide 25 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 26

Slide 26 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 27

Slide 27 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 28

Slide 28 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 29

Slide 29 text

USAGE Denis Brumann // @dbrumann // Caching in Symfony

Slide 30

Slide 30 text

Denis Brumann // @dbrumann // Caching in Symfony public function someHeavyMethod() { $key = 'some_unique_key’; /** @var CacheItemInterface $cacheItem */ $cacheItem = $this->cachePool->getItem($key); if ($cacheItem->isHit()) { return $cacheItem->get(); } $data = // Result from heavy operation $cacheItem->set($data); $cacheItem->expiresAfter(3600); $this->cachePool->save($cacheItem); return $cacheItem->get(); }

Slide 31

Slide 31 text

Denis Brumann // @dbrumann // Caching in Symfony public function someHeavyMethod(array $keys) { $cacheItems = $this->cachePool->getItems($keys); $result = []; foreach ($cacheItems as $cacheItem) { if (!$cacheItem->isHit()) { $data = $this->something($cacheItem->getKey()); $cacheItem->set($data); $cacheItem->expiresAt(new DateTime('+1 hour')); $this->cachePool->saveDeferred($cacheItem); } $result[] = $cacheItem->get(); } $this->cachePool->commit(); return $result; }

Slide 32

Slide 32 text

PSR-16 Denis Brumann // @dbrumann // Caching in Symfony

Slide 33

Slide 33 text

This simpler approach aims to build a standardized streamlined interface for common cases. It is independent of PSR-6 but has been designed to make compatibility with PSR-6 as straightforward as possible. http://www.php-fig.org/psr/psr-16/ Denis Brumann // @dbrumann // Caching in Symfony SIMPLE CACHE

Slide 34

Slide 34 text

Denis Brumann // @dbrumann // Caching in Symfony /** * @var CacheInterface */ private $cache; public function someHeavyMethod() { $key = ‘some.unique.key'; if ($this->cache->has($key)) { return $this->cache->get($key); } $data = $this->something(); $this->cache->set($key, $data, 3600); return $data; }

Slide 35

Slide 35 text

Denis Brumann // @dbrumann // Caching in Symfony if ($this->cache->has($key)) { return $this->cache->get($key); } ⚠

Slide 36

Slide 36 text

USAGE IN SYMFONY Denis Brumann // @dbrumann // Caching in Symfony

Slide 37

Slide 37 text

Denis Brumann // @dbrumann // Caching in Symfony framework: cache: app: cache.adapter.filesystem system: cache.adapter.system directory: /path/to/project/var/cache/dev/pools default_redis_provider: 'redis://localhost' default_memcached_provider: 'memcached://localhost' #pools: # add your custom cache pools here APP/CONFIG/CONFIG.YML

Slide 38

Slide 38 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 39

Slide 39 text

Denis Brumann // @dbrumann // Caching in Symfony framework: # … cache: pools: app.cache.api: adapter: cache.app APP/CONFIG/CONFIG.YML

Slide 40

Slide 40 text

Denis Brumann // @dbrumann // Caching in Symfony services: cache.adapter.my_pdo: class: Symfony\Component\Cache\Adapter\PdoAdapter arguments: - '@doctrine.dbal.default_connection' APP/CONFIG/SERVICES.YML

Slide 41

Slide 41 text

Denis Brumann // @dbrumann // Caching in Symfony framework: # … cache: pools: app.cache.api: adapter: cache.adapter.my_pdo APP/CONFIG/CONFIG.YML

Slide 42

Slide 42 text

Denis Brumann // @dbrumann // Caching in Symfony services: app.some_service.cached: class: App\Service\SomeCachedService decorates: '@app.some_service.concrete' arguments: - '@app.some_service.cached.inner' - ‘@app.cache.api’ public: false app.some_service: '@app.some_service.cached' https://symfony.com/doc/current/service_container/service_decoration.html

Slide 43

Slide 43 text

Denis Brumann // @dbrumann // Caching in Symfony App\Meetup\CachedClient: decorates: App\Meetup\Client arguments: $client: '@App\Meetup\CachedClient.inner' $clientPool: '@app.cache.api' App\Meetup\ClientInterface: '@App\Meetup\CachedClient' https://symfony.com/doc/current/service_container/service_decoration.html

Slide 44

Slide 44 text

EXAMPLE Denis Brumann // @dbrumann // Caching in Symfony

Slide 45

Slide 45 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 46

Slide 46 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 47

Slide 47 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 48

Slide 48 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 49

Slide 49 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 50

Slide 50 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 51

Slide 51 text

TAG AWARE ADAPTER Denis Brumann // @dbrumann // Caching in Symfony

Slide 52

Slide 52 text

Denis Brumann // @dbrumann // Caching in Symfony $key = 'unique.identifier'; $cacheItem = $this->cachePool->getItem($key); $cacheItem->set($data); $cacheItem->expiresAfter(3600); $cacheItem->tag(['tag', 'other_tag', 'something1']); $this->cachePool->save($cacheItem); return $cacheItem->get();

Slide 53

Slide 53 text

Denis Brumann // @dbrumann // Caching in Symfony $cache->invalidateTags(['tag', 'another_tag']);

Slide 54

Slide 54 text

NOT PART OF PSR-6 Denis Brumann // @dbrumann // Caching in Symfony ⚠

Slide 55

Slide 55 text

HTTP CACHE Denis Brumann // @dbrumann // Caching in Symfony

Slide 56

Slide 56 text

Denis Brumann // @dbrumann // Caching in Symfony User Browser Web Server Request: / Request: / Response Response

Slide 57

Slide 57 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 58

Slide 58 text

Denis Brumann // @dbrumann // Caching in Symfony namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class HomepageController extends Controller { /** * @Route("/", name="home") * @Cache(maxage=600) */ public function indexAction() { return $this->render('home.html.twig'); } }

Slide 59

Slide 59 text

Denis Brumann // @dbrumann // Caching in Symfony User Browser Web Server Request: / Request: / Response* Response*

Slide 60

Slide 60 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 61

Slide 61 text

Denis Brumann // @dbrumann // Caching in Symfony User Browser Web Server Request: / Response*

Slide 62

Slide 62 text

Denis Brumann // @dbrumann // Caching in Symfony namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class HomepageController extends Controller { /** * @Route("/", name="home") * @Cache(expires=“+1 hours") */ public function indexAction() { return $this->render('home.html.twig'); } }

Slide 63

Slide 63 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 64

Slide 64 text

SERVER-SIDE CACHING Denis Brumann // @dbrumann // Caching in Symfony

Slide 65

Slide 65 text

Denis Brumann // @dbrumann // Caching in Symfony User Browser Web Server Cache

Slide 66

Slide 66 text

Denis Brumann // @dbrumann // Caching in Symfony VARNISH

Slide 67

Slide 67 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 68

Slide 68 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 69

Slide 69 text

Denis Brumann // @dbrumann // Caching in Symfony namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class HomepageController extends Controller { /** * @Route("/", name="home") * @Cache(maxage=600, smaxage=600) */ public function indexAction() { return $this->render('home.html.twig'); } }

Slide 70

Slide 70 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 71

Slide 71 text

Denis Brumann // @dbrumann // Caching in Symfony User Browser Web Server Cache Request: / Response

Slide 72

Slide 72 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 73

Slide 73 text

Denis Brumann // @dbrumann // Caching in Symfony User Browser Web Server Cache Request: / Response

Slide 74

Slide 74 text

VALIDATION CACHING Denis Brumann // @dbrumann // Caching in Symfony

Slide 75

Slide 75 text

Denis Brumann // @dbrumann // Caching in Symfony /** * @Route("/{urlname}/event/{eventId}", name="event") */ public function eventAction(
 Request $request, string $urlname, string $eventId
 ): Response { // Get event data $response = $this->render( 'event/show.html.twig', ['event' => $event] ); $response->setEtag(md5($response->getContent())); $response->setPublic(); $response->isNotModified($request); return $response; }

Slide 76

Slide 76 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 77

Slide 77 text

Denis Brumann // @dbrumann // Caching in Symfony try { $event = // Get Event-data from API-gateway } catch (GatewayException $exception) { throw new ServiceUnavailableHttpException(); } $response = new Response(); $response->setPublic(); $response->setLastModified($event->getUpdated()); if ($response->isNotModified($request)) { return $response; } return $this->render( 'event/show.html.twig', ['event' => $event], $response );

Slide 78

Slide 78 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 79

Slide 79 text

Denis Brumann // @dbrumann // Caching in Symfony User Browser Web Server Cache Request: / Response Response will be an empty
 304 Not Modified
 when cache was hit

Slide 80

Slide 80 text

ESI Denis Brumann // @dbrumann // Caching in Symfony

Slide 81

Slide 81 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 82

Slide 82 text

Denis Brumann // @dbrumann // Caching in Symfony

Slide 83

Slide 83 text

Denis Brumann // @dbrumann // Caching in Symfony {% block content %}

Upcoming Meetups

{{ render(controller('AppBundle:Event:list')) }} {% endblock %} {% block sidebar %}

Featured User Groups

{{ render_esi(controller('AppBundle:Group:list')) }} {% endblock %}

Slide 84

Slide 84 text

Denis Brumann // @dbrumann // Caching in Symfony Expires: 60 Expires: 3600 Expires: 86400

Slide 85

Slide 85 text

THANK YOU Denis Brumann // @dbrumann // Caching in Symfony