Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Caching with PSRs at Dutch PHP Conference

Caching with PSRs at Dutch PHP Conference

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.

Hannes Van De Vreken

June 30, 2017
Tweet

More Decks by Hannes Van De Vreken

Other Decks in Technology

Transcript

  1. Caching with PSRs
    Dutch PHP Conference 2017
    #dpc17 @hannesvdvreken

    View full-size slide

  2. Hi, my name is Hannes.

    View full-size slide

  3. madewithlove.be

    View full-size slide

  4. Caching PSRs
    Caching in PHP anno 2017

    View full-size slide

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

    View full-size slide

  6. 1. Intro
    what is caching?

    View full-size slide

  7. WHAT IS CACHING
    Speeding up your app

    View full-size slide

  8. WHAT IS CACHING
    1. Query stored value
    2.Execute slow task
    3.Remember result

    View full-size slide

  9. WHAT IS CACHING
    Should not cause
    app failure

    View full-size slide

  10. WHAT IS CACHING - EXAMPLES
    • HTTP Request
    • Slow DB call
    • Process image/zip/…

    View full-size slide

  11. WHAT IS CACHING
    Should not cause
    app failure

    View full-size slide

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

    View full-size slide

  13. WHAT IS CACHING
    Should not cause
    app failure

    View full-size slide

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

    View full-size slide

  15. WHAT IS CACHING
    Should not cause
    app failure

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  18. APPLICATION CACHING IN 2015
    Frameworks & cache libs have their own:
    - set of supported caching back-ends
    - interface to allow custom
    implementations

    View full-size slide

  19. APPLICATION CACHING IN 2015
    Adapters everywhere!

    View full-size slide

  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

    View full-size slide

  21. APPLICATION CACHING IN 2015
    If no adapter available
    - Write your own
    - Store in different cache stores

    View full-size slide

  22. 3. Hello PSR-6

    View full-size slide

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

    View full-size slide

  24. INTRODUCING PSR-6
    Repository - Entity

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  27. INTRODUCING PSR-6
    Don’t instantiate your own Items.
    $item = $pool->getItem(‘key’)
    ->set($value)
    ->expiresAfter(3600);
    $pool->save($item);

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  30. INTRODUCING PSR-6
    Cache features:
    - Stampede protection:
    Parallel incoming requests
    executing long process to
    update cache value

    View full-size slide

  31. STAMPEDE PROTECTION

    View full-size slide

  32. STAMPEDE PROTECTION

    View full-size slide

  33. STAMPEDE PROTECTION

    View full-size slide

  34. STAMPEDE PROTECTION

    View full-size slide

  35. STAMPEDE PROTECTION

    View full-size slide

  36. STAMPEDE PROTECTION

    View full-size slide

  37. STAMPEDE PROTECTION

    View full-size slide

  38. STAMPEDE PROTECTION

    View full-size slide

  39. STAMPEDE PROTECTION

    View full-size slide

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

    View full-size slide

  41. INTRODUCING PSR-6
    Cache features:
    - Stampede protection
    - Taggable cache
    - Hierarchical cache
    $pool->delete('tree/*');
    // {}()/\@:

    View full-size slide

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

    View full-size slide

  43. START USING IT
    Integration tests for
    implementations:
    cache/integration-tests

    View full-size slide

  44. START USING IT
    class PoolIntegrationTest extends CachePoolTest
    {
    public function createCachePool()
    {
    return new CustomCachePool();
    }
    }

    View full-size slide

  45. START USING IT
    cache/taggable-cache
    cache/namespaced-cache
    cache/hierarchical-cache
    cache/array-adapter
    cache/void-adapter
    cache/chain-adapter

    View full-size slide

  46. START USING IT - UPGRADE
    Upgrade paths

    View full-size slide

  47. START USING IT - UPGRADE
    Libraries

    View full-size slide

  48. START USING IT - UPGRADE
    Libraries (next major versions)
    have a PSR-6 caching decorator

    View full-size slide

  49. START USING IT - UPGRADE
    Frameworks

    View full-size slide

  50. START USING IT - UPGRADE
    Start using PSR-6 enabled libraries
    with adapters for current FW’s
    implementation.

    View full-size slide

  51. START USING IT - UPGRADE
    Example:
    symfony/cache
    Laravel PSR-6 bridge

    View full-size slide

  52. cache/illuminate-adapter

    View full-size slide

  53. START USING IT
    Adoption?

    View full-size slide

  54. 4. Hello PSR-16

    View full-size slide

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

    View full-size slide

  56. One simple interface
    INTRODUCING PSR-16

    View full-size slide

  57. Psr\SimpleCache\CacheInterface
    INTRODUCING PSR-16

    View full-size slide

  58. // Returns null when not available.
    // Returns null when you set it.
    $cache->set($key, null);
    $value = $cache->get($key);
    INTRODUCING PSR-16

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  61. Differences from PSR-6
    - No `null` values
    - No deferring
    INTRODUCING PSR-16

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  64. START USING IT
    Upgrade path?
    Same as PSR-6

    View full-size slide

  65. START USING IT
    Adoption?

    View full-size slide

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

    View full-size slide

  67. Thank you!
    #dpc17 @hannesvdvreken

    View full-size slide

  68. Time for questions!
    #dpc17 @hannesvdvreken

    View full-size slide