The document discusses the Symfony Cache Component and how it can be used to speed up applications with a new caching layer. It introduces key concepts like PSR-6 and PSR-16 caching interfaces, the Symfony Cache Component which implements these interfaces, and Symfony Simple Cache which provides a simpler interface. Real use cases are presented where caching can improve performance for applications with high traffic by caching queries, API responses, and computationally intensive tasks.
10. 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
12. 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
13. 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
14. 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();
15. PSR-6: CacheItemInterface
public function getKey();
public function get();
public function isHit();
public function set($value);
public function expiresAt($expiration);
public function expiresAfter($time);
26. 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
27. 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);
38. 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
39. 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
- …
40. 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
41. 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…
42. 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
43. 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.
44. Questa è una diapositiva per l’inserimento di 1 immagine orizzontale con didascalia
Profiler
47. 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
- …
48. Questa è una diapositiva per l’inserimento di 1 immagine orizzontale con didascalia
49. InstragramFeed
Real Use Case Scenario
(https://github.com/ideatosrl/instagram-feed)
➔ Load recent media from user
➔ PSR-6 cache decorator
➔ Easily integration with Symfony