SlideShare a Scribd company logo
Symfony Cache Component
speed-up your application with
a new layer of cache
simone d’amico
software engineer @
@dymissy
sd@ideato.it
Symfony Cache Component
Symfony Cache Component: speed up your application with a new layer of cache
Symfony Cache Component: speed up your application with a new layer of cache
Agenda
How did we cache before
Symfony 3.1?
Symfony Cache Component
Symfony Simple Cache
Real Use Case
In the beginning there was…
Cache before Symfony 3.1
➔ Apc
➔ Memcache / Memcached
➔ Redis
➔ …
Doctrine Cache
doctrine_cache:
class:
DoctrineCommonCacheFilesystemCac
he
arguments:
- '/tmp/doctrine_cache/'
public: true
$cache = $this->get('doctrine_cache');
$feed = $cache->fetch('feed');
if (!$feed) {
$feed = $feedReader->load();
$cache->save('feed', $feed, 60);
}
return $feed;
…then Symfony 3.1 was released
Symfony Cache Component
Symfony Cache Component
➔ Strict implementation of PSR-6: Caching Interface
➔ Provides adapters for the most common caching
backends
➔ Compatible with every Doctrine Cache adapter
Symfony Cache Component
Symfony Cache Component
Some of adapters provided:
➔ Apcu Cache adapter
➔ Array Cache adapter
➔ Chain Cache adapter
➔ Doctrine Cache adapter
➔ Filesystem Cache adapter
➔ Memcached Cache adapter
➔ PDO & Doctrine Cache DBAL adapter
➔ Php Array Cache adapter
➔ Proxy Cache adapter
➔ Redis Cache adapter
PSR-6: Caching Interface
PSR6: Caching Interface
➔ Goal
Allow developers to create cache-aware libraries that can be
integrated into existing frameworks and systems without the need
for custom development.
➔ Data
Implementing libraries MUST support all serializable PHP data types,
including: strings, integers, floats, boolean, null, arrays, object
PSR-6: Key Concepts
PSR6: Caching Interface
➔ Pool
Collection of items in a caching system. The pool is a
logical Repository of all items it contains
➔ Item
Single key/value pair within a Pool
PSR-6: CacheItemPoolInterface
public function getItem($key);
public function getItems(array $keys = []);
public function hasItem($key);
public function clear();
public function deleteItem($key);
public function deleteItems(array $keys);
public function save(CacheItemInterface
$item);
public function
saveDeferred(CacheItemInterface $item);
public function commit();
PSR-6: CacheItemInterface
public function getKey();
public function get();
public function isHit();
public function set($value);
public function expiresAt($expiration);
public function expiresAfter($time);
Symfony Cache Component: Key Concepts
Symfony Cache Component
➔ Pool
➔ Item
➔ Adapter
SymfonyComponentCacheAdapterAdapterInterface
SymfonyComponentCacheCacheItem
SymfonyComponentCacheAdapter*Adapter
Symfony Cache Component
symfony_cache:
class:
SymfonyComponentCacheAdapter
FilesystemAdapter
arguments:
- ''
- 0
- '/tmp/cache/'
public: true
$cache = $this->get('symfony_cache');
$cacheItem = $cache->getItem('feed');
if($cacheItem->isHit()) {
return $cacheItem->get();
}
$feed = $feedReader->load();
$cacheItem->set($feed);
$cacheItem->expiresAfter(60);
$cache->save($cacheItem);
return $feed;
Symfony Cache Component
symfony_cache:
class:
SymfonyComponentCacheAdapter
FilesystemAdapter
arguments:
- ''
- 0
- '/tmp/symfony_cache/'
public: true
$cache = $this->get('symfony_cache');
$cacheItem = $cache->getItem('feed');
if($cacheItem->isHit()) {
return $cacheItem->get();
}
$feed = $feedReader->load();
$cacheItem->set($feed);
$cacheItem->expiresAfter(60);
$cache->save($cacheItem);
return $feed;
1
Symfony Cache Component
symfony_cache:
class:
SymfonyComponentCacheAdapter
FilesystemAdapter
arguments:
- ''
- 0
- '/tmp/symfony_cache/'
public: true
$cache = $this->get('symfony_cache');
$cacheItem = $cache->getItem('feed');
if($cacheItem->isHit()) {
return $cacheItem->get();
}
$feed = $feedReader->load();
$cacheItem->set($feed);
$cacheItem->expiresAfter(60);
$cache->save($cacheItem);
return $feed;
2
Symfony Cache Component
symfony_cache:
class:
SymfonyComponentCacheAdapter
FilesystemAdapter
arguments:
- ''
- 0
- '/tmp/symfony_cache/'
public: true
$cache = $this->get('symfony_cache');
$cacheItem = $cache->getItem('feed');
if($cacheItem->isHit()) {
return $cacheItem->get();
}
$feed = $feedReader->load();
$cacheItem->set($feed);
$cacheItem->expiresAfter(60);
$cache->save($cacheItem);
return $feed;
3
Symfony Cache Component
symfony_cache:
class:
SymfonyComponentCacheAdapter
FilesystemAdapter
arguments:
- ''
- 0
- '/tmp/symfony_cache/'
public: true
$cache = $this->get('symfony_cache');
$cacheItem = $cache->getItem('feed');
if($cacheItem->isHit()) {
return $cacheItem->get();
}
$feed = $feedReader->load();
$cacheItem->set($feed);
$cacheItem->expiresAfter(60);
$cache->save($cacheItem);
return $feed;
4
Symfony Cache Component
symfony_cache:
class:
SymfonyComponentCacheAdapter
FilesystemAdapter
arguments:
- ''
- 0
- '/tmp/symfony_cache/'
public: true
$cache = $this->get('symfony_cache');
$cacheItem = $cache->getItem('feed');
if($cacheItem->isHit()) {
return $cacheItem->get();
}
$feed = $feedReader->load();
$cacheItem->set($feed);
$cacheItem->expiresAfter(60);
$cache->save($cacheItem);
return $feed;
5
Symfony Cache Component
symfony_cache:
class:
SymfonyComponentCacheAdapter
FilesystemAdapter
arguments:
- ''
- 0
- '/tmp/symfony_cache/'
public: true
$cache = $this->get('symfony_cache');
$cacheItem = $cache->getItem('feed');
if($cacheItem->isHit()) {
return $cacheItem->get();
}
$feed = $feedReader->load();
$cacheItem->set($feed);
$cacheItem->expiresAfter(60);
$cache->save($cacheItem);
return $feed;
6
OK. Cool. But…
…can we do it in a simpler way?
PSR-16: Common Interface for Caching Libraries
PSR16: Caching Interface
➔ Goal
Simplify PSR-6 Interface for easier use cases
➔ Data
Implementing libraries MUST support all serializable PHP
data types, including: strings, integers, floats, boolean,
null, arrays, object
PSR-16: CacheInterface
public function get($key, $default = null);
public function set($key, $value, $ttl = null);
public function delete($key);
public function clear();
public function getMultiple($keys, $default = null);
public function setMultiple($values, $ttl = null);
public function deleteMultiple($keys);
public function has($key);
Symfony Simple Cache
Symfony Simple Cache
➔ Available from Symfony 3.3
➔ Implementation of PSR-16
Symfony Simple Cache
simple_cache:
class:
SymfonyComponentCacheSimpleF
ilesystemCache
arguments:
- ''
- 0
- '/tmp/cache/'
public: true
$cache = $this->get('simple_cache');
if ($cache->has('feed')) {
return $cache->get('feed');
}
$feed = $feedReader->load();
$cache->set('feed', $feed, 60);
return $feed;
Symfony Simple Cache
simple_cache:
class:
SymfonyComponentCacheSimpleF
ilesystemCache
arguments:
- ''
- 0
- '/tmp/cache/'
public: true
$cache = $this->get('simple_cache');
if ($cache->has('feed')) {
return $cache->get('feed');
}
$feed = $feedReader->load();
$cache->set('feed', $feed, 60);
return $feed;
1
Symfony Simple Cache
simple_cache:
class:
SymfonyComponentCacheSimpleF
ilesystemCache
arguments:
- ''
- 0
- '/tmp/cache/'
public: true
$cache = $this->get('simple_cache');
if ($cache->has('feed')) {
return $cache->get('feed');
}
$feed = $feedReader->load();
$cache->set('feed', $feed, 60);
return $feed;
2
Symfony Simple Cache
simple_cache:
class:
SymfonyComponentCacheSimpleF
ilesystemCache
arguments:
- ''
- 0
- '/tmp/cache/'
public: true
$cache = $this->get('simple_cache');
if ($cache->has('feed')) {
return $cache->get('feed');
}
$feed = $feedReader->load();
$cache->set('feed', $feed, 60);
return $feed;
3
Symfony Simple Cache
$cache = $this->get('simple_cache');
if ($cache->has('feed')) {
return $cache->get('feed');
}
$feed = $feedReader->load();
$cache->set('feed', $feed, 60);
return $feed;
$cache = $this->get('symfony_cache');
$cacheItem = $cache->getItem('feed');
if($cacheItem->isHit()) {
return $cacheItem->get();
}
$feed = $feedReader->load();
$cacheItem->set($feed);
$cacheItem->expiresAfter(60);
$cache->save($cacheItem);
return $feed;
Cache Component Simple Cache
Simple Cache looks simpler than Cache Component.
Why should we use the component?
Symfony Cache Component
Symfony Cache Component
➔ Tags
➔ ChainCache Adapter
➔ ProxyCache Adapter
➔ Symfony FrameworkBundle Integration
Symfony Cache Component
Symfony Cache Component
➔ Tags
➔ ChainCache Adapter
➔ ProxyCache Adapter
➔ Symfony FrameworkBundle Integration
Real use case scenario
Domain
Real Use Case Scenario
➔ Thousands of unique visitors per day
➔ Millions of page requested per day
➔ Distributed system architecture
➔ Different layers of caching
➔ Performance matters
Domain
Real Use Case Scenario
➔ Different caching strategies:
- Slow queries with low refresh rate
- Loading data from external APIs with
low rate limit
- CPU intensive tasks
- …
Real use case scenario
cache:
default_redis_provider: "%app_cache_redis_provider%"
default_memcached_provider: "%app_cache_memcached_provider%"
pools:
app.cache.rating:
adapter: cache.adapter.redis
default_lifetime: 21600
app.cache.api:
adapter: cache.adapter.memcached
default_lifetime: 3600
app.cache.customer:
adapter: cache.app #defaults to cache.adapter.filesystem
default_lifetime: 86400
public: true
config.yml
Real use case scenario
$cache = $this->get('app.cache.customer');
$cacheItem = $cache->getItem('customer14');
if ($cacheItem->isHit()) {
return $cacheItem->get();
}
//...
Use the service in a Controller, UseCase, whatever…
Invalidate cache by tags
$cacheItem = $cache->getItem('customer15');
$product = $this->repository->find('customer15');
$cacheItem->set($product);
$cacheItem->tag(['reviews', 'customers', 'customer15']);
$cacheItem->expiresAt(new DateTimeImmutable('tomorrow'));
$cache->save($cacheItem);
//...
$cache->invalidateTags(['customer15']);
$cache->invalidateTags(['customers']);
Tag the cached content in order to invalidate easily
Clear cache pools
$ ./bin/console cache:pool:clear <cache pool or clearer 1> [...<cache
pool or clearer N>]
$ ./bin/console cache:pool:clear app.cache.customer
$ ./bin/console cache:pool:clear app.cache.custom_service
Symfony FrameworkBundle provides specific command for clear cache pools.
Questa è una diapositiva per l’inserimento di 1 immagine orizzontale con didascalia
Profiler
Profiler
Bonus
Domain
Real Use Case Scenario
➔ Different caching strategies:
- Slow queries with low refresh rate
- Loading data from external APIs with
low rate limit
- CPU intensive tasks
- …
Questa è una diapositiva per l’inserimento di 1 immagine orizzontale con didascalia
InstragramFeed
Real Use Case Scenario
(https://github.com/ideatosrl/instagram-feed)
➔ Load recent media from user
➔ PSR-6 cache decorator
➔ Easily integration with Symfony
InstragramFeed
InstragramFeed
Questa è una diapositiva per l’inserimento di 1 immagine orizzontale con didascalia
InstagramCachedFeed
public function getMedia(int $count = self::MEDIA_COUNT, $maxId = null, $minId = null): array
{
$key = sprintf('instagram_feed_%s_%s_%s', $count, $maxId, $minId);
$cacheItem = $this->cache->getItem($key);
if ($cacheItem->isHit()) {
return $cacheItem->get();
}
$feed = $this->instagramFeed->getMedia($count, $maxId, $minId);
$cacheItem->set($feed);
$cacheItem->expiresAfter($this->ttl);
$this->cache->save($cacheItem);
return $feed;
}
Thanks!
http://joind.in/talk/2c738
Simone D’Amico
sd@ideato.it
@dymissy
github.com/dymissy
?
We are hiring!
Catch me around here or drop us a line at recruitment@ideato.it
Bonus 2
Questa è una diapositiva per l’inserimento di 1 immagine orizzontale con didascalia
References
https://www.slideshare.net/andreromcke/symfony-meetup-psr6-symfony-31-cache-component
http://www.php-fig.org/psr/psr-6/
http://www.php-fig.org/psr/psr-16/
https://symfony.com/doc/current/components/cache.html
https://github.com/ideatosrl/instagram-feed
Image Credits
http://cybersport.gg/45530/refreshed-cache-in-new-csgo-patch/
https://www.brainvire.com/symfony2-framework-development-services

More Related Content

PDF
JAVA THREADS.pdf
PPTX
operator overloading
PDF
OPcacheの新機能ファイルベースキャッシュの内部実装を読んでみた
PDF
An Introduction to Celery
PPTX
File management in C++
PDF
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and more
PPTX
Java GC
PDF
Serialization & De-serialization in Java
JAVA THREADS.pdf
operator overloading
OPcacheの新機能ファイルベースキャッシュの内部実装を読んでみた
An Introduction to Celery
File management in C++
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and more
Java GC
Serialization & De-serialization in Java

What's hot (20)

PPTX
This pointer
PPTX
Java interface
PDF
27 applet programming
PPT
File Input & Output
PDF
Chapter 7 - Input Output Statements in C++
PDF
Laravel Design Patterns
PDF
Nginx Internals
PPT
File Handling In C++(OOPs))
PPTX
polymorphism
PDF
JPA - Java Persistence API
PPT
PDF
Using API Platform to build ticketing system #symfonycon
PPTX
Dot net assembly
PDF
File Access Permission
PDF
Chat Room System using Java Swing
PPTX
Interfaces in java
PPT
Inheritance
PPS
Jsp element
PDF
Lets make a better react form
PPTX
Build RESTful API Using Express JS
This pointer
Java interface
27 applet programming
File Input & Output
Chapter 7 - Input Output Statements in C++
Laravel Design Patterns
Nginx Internals
File Handling In C++(OOPs))
polymorphism
JPA - Java Persistence API
Using API Platform to build ticketing system #symfonycon
Dot net assembly
File Access Permission
Chat Room System using Java Swing
Interfaces in java
Inheritance
Jsp element
Lets make a better react form
Build RESTful API Using Express JS
Ad

Similar to Symfony Cache Component: speed up your application with a new layer of cache (20)

PDF
Why we choose Symfony2
PDF
APC & Memcache the High Performance Duo
PDF
Barcelona apc mem2010
PDF
Data cache management in php
PDF
Zend_Cache: how to improve the performance of PHP applications
PDF
PhpTour Lyon 2014 - Transparent caching & context aware http cache
PDF
Rationally boost your symfony2 application with caching tips and monitoring
PDF
Dutch php conference_apc_mem2010
PDF
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
ODP
Caching and tuning fun for high scalability
PDF
Caching with Memcached and APC
KEY
Introduction to memcached
PDF
SfDay 2019: Head first into Symfony Cache, Redis & Redis Cluster
PDF
Zend Server Data Caching
PDF
Caching
PPT
Nice performance using Sf2 cache wrapping Sf1 application - Paris
PPT
Sf sf v5
PPTX
Caching
ODP
phptek13 - Caching and tuning fun tutorial
PPT
Nice performance using Sf2 cache wrapping Sf1 application
Why we choose Symfony2
APC & Memcache the High Performance Duo
Barcelona apc mem2010
Data cache management in php
Zend_Cache: how to improve the performance of PHP applications
PhpTour Lyon 2014 - Transparent caching & context aware http cache
Rationally boost your symfony2 application with caching tips and monitoring
Dutch php conference_apc_mem2010
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Caching and tuning fun for high scalability
Caching with Memcached and APC
Introduction to memcached
SfDay 2019: Head first into Symfony Cache, Redis & Redis Cluster
Zend Server Data Caching
Caching
Nice performance using Sf2 cache wrapping Sf1 application - Paris
Sf sf v5
Caching
phptek13 - Caching and tuning fun tutorial
Nice performance using Sf2 cache wrapping Sf1 application
Ad

More from Simone D'Amico (10)

PDF
Rethinking Server-Side Rendering in Modern PHP Applications
PDF
Monitoring and Observability: Building Products That Don't Break in Silence
PDF
From code to leadership: Navigating the journey from Individual Contributor t...
PDF
Symfony UX: rivoluziona il tuo frontend con Symfony
PDF
Breaking free from monoliths: revolutionizing development with Livewire and S...
PDF
Panther loves Symfony apps
PDF
Rory’s Story Cubes Retrospective
PDF
E-commerce con SF: dal case study alla realtà
PDF
Introduction to WordPress REST API
PPTX
Manage custom options pages in Wordpress
Rethinking Server-Side Rendering in Modern PHP Applications
Monitoring and Observability: Building Products That Don't Break in Silence
From code to leadership: Navigating the journey from Individual Contributor t...
Symfony UX: rivoluziona il tuo frontend con Symfony
Breaking free from monoliths: revolutionizing development with Livewire and S...
Panther loves Symfony apps
Rory’s Story Cubes Retrospective
E-commerce con SF: dal case study alla realtà
Introduction to WordPress REST API
Manage custom options pages in Wordpress

Recently uploaded (20)

PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
presentation_pfe-universite-molay-seltan.pptx
PPT
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
PDF
The Internet -By the Numbers, Sri Lanka Edition
PPTX
innovation process that make everything different.pptx
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PDF
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PDF
WebRTC in SignalWire - troubleshooting media negotiation
PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PPTX
international classification of diseases ICD-10 review PPT.pptx
PDF
Paper PDF World Game (s) Great Redesign.pdf
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
Cloud-Scale Log Monitoring _ Datadog.pdf
Slides PDF The World Game (s) Eco Economic Epochs.pdf
presentation_pfe-universite-molay-seltan.pptx
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
The Internet -By the Numbers, Sri Lanka Edition
innovation process that make everything different.pptx
Unit-1 introduction to cyber security discuss about how to secure a system
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
Slides PPTX World Game (s) Eco Economic Epochs.pptx
Introuction about ICD -10 and ICD-11 PPT.pptx
introduction about ICD -10 & ICD-11 ppt.pptx
SASE Traffic Flow - ZTNA Connector-1.pdf
WebRTC in SignalWire - troubleshooting media negotiation
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
Design_with_Watersergyerge45hrbgre4top (1).ppt
international classification of diseases ICD-10 review PPT.pptx
Paper PDF World Game (s) Great Redesign.pdf
Job_Card_System_Styled_lorem_ipsum_.pptx
The New Creative Director: How AI Tools for Social Media Content Creation Are...

Symfony Cache Component: speed up your application with a new layer of cache