Caching with PSRs at PHP Srbija

Caching with PSRs at PHP Srbija

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 Packagist.org, 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 Packagist.org. 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.

39eb3f3d313b13f05534e496285040b8?s=128

Hannes Van De Vreken

May 27, 2018
Tweet

Transcript

  1. Caching with PSRs PHP Srbija #phpsrb @hannesvdvreken

  2. Hi, my name is Hannes.

  3. Caching PSRs Caching in PHP anno 2018

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

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

  6. WHAT IS CACHING Speeding up your app

  7. WHAT IS CACHING 1. Query stored value 2.Execute slow task

    3.Remember result
  8. WHAT IS CACHING Should not cause app failure

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

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

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

    cache
  12. WHAT IS CACHING Should not cause app failure

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

    Opcache
  14. WHAT IS CACHING Should not cause app failure

  15. WHAT IS CACHING Strategies Warmup Slow tasks in background Long

    TTL + busting Stampede
  16. 2. Caching in 2015 the state of caching (pre PSR-FIG)

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

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

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

  20. 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
  21. APPLICATION CACHING IN 2015 If no adapter available - Write

    your own - Store in different cache stores
 (ops will not love you for that)
  22. 3. Hello PSR-6

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

  24. INTRODUCING PSR-6 Repository - Entity

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

    $item->get();
  26. INTRODUCING PSR-6 Item is an entity, it’s not immutable, but

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

    ->set($value) ->expiresAfter(3600); $pool->save($item);
  28. INTRODUCING PSR-6 Pool has support for multi-actions $pool->getItems($keys); $pool->saveDeferred($item); $pool->commit();

  29. INTRODUCING PSR-6 https://github.com/php-fig/cache

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

  31. INTRODUCING PSR-6 Allows speccing more features in the future by

    reserving characters - {}()/\@:
  32. INTRODUCING PSR-6 Cache features: - Stampede protection: Parallel incoming requests

    executing long process to update cache value
  33. STAMPEDE PROTECTION

  34. STAMPEDE PROTECTION

  35. STAMPEDE PROTECTION

  36. STAMPEDE PROTECTION

  37. STAMPEDE PROTECTION

  38. STAMPEDE PROTECTION

  39. STAMPEDE PROTECTION

  40. STAMPEDE PROTECTION

  41. STAMPEDE PROTECTION

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

    cache/taggable-cache
  43. INTRODUCING PSR-6 Cache features: - Stampede protection - Taggable cache

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

    - Hierarchical cache cache/hierarchical-cache
  45. INTRODUCING PSR-6 Cache features: - Stampede protection - Taggable cache

    - Hierarchical cache $pool->hasItem('|users|4711|followers'); // true $pool->delete(‘|users|4711'); $pool->hasItem('|users|4711|followers'); // false
  46. Practical who uses it, and how can I use it?

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

  51. None
  52. START USING IT class PoolIntegrationTest extends CachePoolTest { public function

    createCachePool() { return new CustomCachePool(); } }
  53. START USING IT cache/taggable-cache cache/namespaced-cache cache/hierarchical-cache cache/prefixed-cache cache/encryption-cache cache/array-adapter cache/void-adapter

    cache/chain-adapter
  54. START USING IT - UPGRADE Upgrade paths

  55. START USING IT - UPGRADE Libraries

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

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

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

    with adapters for current FW’s implementation.
  59. START USING IT - UPGRADE Example: symfony/cache cache/illuminate-adapter

  60. None
  61. cache/illuminate-adapter

  62. START USING IT Adoption?

  63. None
  64. 4. Hello PSR-16

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

  66. One simple interface INTRODUCING PSR-16

  67. Psr\SimpleCache\CacheInterface INTRODUCING PSR-16

  68. // Returns null when not available. // Returns null when

    you set it to null. $cache->set($key, null); $value = $cache->get($key); INTRODUCING PSR-16
  69. $cache->has($key); // bool INTRODUCING PSR-16

  70. $cache->set($key, new ValueObject()); INTRODUCING PSR-16

  71. $cache->setMultiple($values); $cache->getMultiple($keys); $cache->deleteMultiple($keys); INTRODUCING PSR-16

  72. https://github.com/php-fig/simple-cache INTRODUCING PSR-16

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

    INTRODUCING PSR-16
  74. INTRODUCING PSR-16 Allows speccing more features in the future by

    reserving characters - {}()/\@:
  75. Practical who uses it, and how can I use it?

  76. None
  77. None
  78. START USING IT Upgrade path? Same as PSR-6

  79. START USING IT Adoption?

  80. None
  81. None
  82. None
  83. START USING IT $laravelCache = App::make(‘cache’)->store(); $psr6Cache = new IlluminateCachePool($laravelCache);

    $psr16Cache = new SimpleCacheBridge($psr6Cache);
  84. START USING IT https://github.com/hannesvdvreken/caching-demo

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

    Hello PSR-16 RECAP
  86. Thank you! #phpsrb @hannesvdvreken

  87. Time for questions! #phpsrb @hannesvdvreken