This talk is divided into 2 blocks. The first one takes a look at Symfony's Cache component and the underlying PSR-6 and PSR-16 standards. The second one is a quick glance at HTTP-Caching & ESI in Symfony.
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
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
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
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
• 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
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
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
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.
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.
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.
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)
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/
Psr\Cache; interface CacheItemPoolInterface { public function getItem($key); public function getItems(array $keys = array()); public function hasItem($key); // … }
Psr\Cache; interface CacheItemPoolInterface { // … public function clear(); public function deleteItem($key); public function deleteItems(array $keys); // … }
Psr\Cache; interface CacheItemPoolInterface { // … public function save(CacheItemInterface $item); public function saveDeferred(CacheItemInterface $item); public function commit(); }
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
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'); } }
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'); } }
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'); } }