Caching with PSRs at PHPBenelux

Caching with PSRs at PHPBenelux

In this talk you will learn what PSR-6 and PSR-16 are, why they are designed like they are, what the differences are, and how to use them. From the provided interfaces, the virtual package on, to the implementations available. Everything will be explained for both beginners and more experienced developers. We will dive into implementations, cache stampede protection, all using libraries we can find on We will also show how to start using it today in our current applications by using adapters. The entire talk will be accompanied with code samples and live demos, showing both working and failing caching systems.


Hannes Van De Vreken

January 28, 2017


  1. Caching with PSRs PHPBenelux @hannesvdvreken

  2. Hi, my name is Hannes.

  3. !


  5. Caching PSRs Caching in PHP anno 2017

  6. 1. Intro 2. Caching in 2015 3. Hello PSR-6 4.

    Hello PSR-16
  7. 1. Intro what is caching?

  8. WHAT IS CACHING Speeding up your app

  9. WHAT IS CACHING 1. Execute slow task 2.Remember result 3.Use

    stored value
  10. WHAT IS CACHING Should not cause app failure

  11. WHAT IS CACHING - EXAMPLES • HTTP Request • Slow

    DB call • Process image/zip/…
  12. WHAT IS CACHING Should not cause app failure

  13. WHAT IS CACHING geocode IP check cache store result check

  14. WHAT IS CACHING Should not cause app failure

  15. WHAT IS CACHING At different layers: User-Agent Webserver (Varnish/Nginx) Application

  16. WHAT IS CACHING Should not cause app failure

  17. 2. Caching in 2015 the state of caching (pre PSR-FIG)

  18. APPLICATION CACHING IN 2015 1. Libraries do it 2.Frameworks do

    it 3.Cache libs do it
  19. APPLICATION CACHING IN 2015 Frameworks & cache libs have their

    own: - set of supported caching back-ends - interface to allow custom implementations
  20. APPLICATION CACHING IN 2015 Adapters everywhere!

  21. APPLICATION CACHING IN 2015 Framework X cache Framework Y cache

    Package W cache Cache package Z Adapter W->X Adapter W->Y Adapter W->Z Adapter Y->Z
  22. APPLICATION CACHING IN 2015 If no adapter available - Write

    your own - Store in different cache stores
  23. 3. Hello PSR-6

  24. INTRODUCING PSR-6 Finalised & accepted in December 2015

  25. INTRODUCING PSR-6 Repository - Entity

  26. INTRODUCING PSR-6 use Psr\Cache\CacheItemPoolInterface; use Psr\Cache\CacheItemInterface; $item = $pool->getItem($key); $item->getKey();

  27. INTRODUCING PSR-6 Item is an entity, it’s not immutable, but

    the Key is
  28. INTRODUCING PSR-6 Don’t instantiate your own Items. $item = $pool->getItem(‘key’)

    ->set($value) ->expiresAfter(3600); $pool->save($item);
  29. INTRODUCING PSR-6 CacheItemInterface has no getExpiresAt

  30. INTRODUCING PSR-6 Pool has support for multi-actions $pool->getItems($keys); $pool->saveDeferred($item); $pool->commit();

  31. INTRODUCING PSR-6 Repository - Entity model allows extensions

  32. INTRODUCING PSR-6 Cache features: - Stampede protection: Parallel incoming requests

    executing long process to update cache value









  42. INTRODUCING PSR-6 Cache features: - Stampede protection - Taggable cache

    $item->addTags(['cat-1']); $pool->clearTags(['cat-1']);
  43. INTRODUCING PSR-6 Cache features: - Stampede protection - Taggable cache

    - Hierarchical cache $pool->delete('tree/*'); // {}()/\@:
  44. Practical who uses it, and how can I use it?

  45. None
  46. None
  47. None
  48. START USING IT Integration tests for implementations: cache/integration-tests

  49. None
  50. START USING IT class PoolIntegrationTest extends CachePoolTest { public function

    createCachePool() { return new CustomCachePool(); } }
  51. START USING IT - UPGRADE Upgrade paths

  52. START USING IT - UPGRADE Libraries

  53. START USING IT - UPGRADE Libraries (next major versions) have

    a PSR-6 caching decorator
  54. START USING IT - UPGRADE Frameworks

  55. START USING IT - UPGRADE Start using PSR-6 enabled libraries

    with adapters for current FW’s implementation.
  56. START USING IT - UPGRADE Example: symfony/cache Laravel PSR-6 bridge

  57. None
  58. START USING IT Adoption?

  59. None
  60. 4. Hello PSR-16

  61. INTRODUCING PSR-16 Finalised & accepted in December 2016

  62. One simple interface INTRODUCING PSR-16

  63. Psr\SimpleCache\CacheInterface INTRODUCING PSR-16

  64. // Returns null when not available. $value = $cache->get($key); INTRODUCING

  65. $cache->set($key, null); // No way to see if value is

    null // or value not set. INTRODUCING PSR-16
  66. $cache->set($key, new ValueObject()); INTRODUCING PSR-16

  67. CacheInterface::getMultiple CacheInterface::setMultiple CacheInterface::deleteMultiple INTRODUCING PSR-16

  68. Differences from PSR-6 - No `null` values - No deferring

  69. Also allows extension thanks to - {}()/\@: INTRODUCING PSR-16

  70. Practical who uses it, and how can I use it?

  71. None
  72. START USING IT Upgrade path? Same as PSR-6

  73. START USING IT Integration tests?

  74. START USING IT Adoption?

  75. None
  76. None
  77. 1. Intro 2. Caching in 2015 3. Hello PSR-6 4.

    Hello PSR-16 RECAP
  78. Thank you! https:/ / @hannesvdvreken

  79. Time for questions! @hannesvdvreken