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

PSR-6 & Symfony Cache: fast by standards

PSR-6 & Symfony Cache: fast by standards

After several months of discussion, the PHP-FIG (PHP Framework Interop Group) published the PSR-6. Its goal is to enable interoperability between caching systems implementations - a mandatory aspect to improve performance in your applications. At this conference, we will see the benefits and limitations of PSR-6, we will discuss the various existing cache solutions (Doctrine, Stash, php-cache), and I will present the Symfony 3.1 Cache component and its latest improvements in version 3.2.

Nicolas Grekas

December 01, 2016
Tweet

More Decks by Nicolas Grekas

Other Decks in Technology

Transcript

  1. PSR-6 & Symfony Cache fast by standards #SymfonyCon @nicolasgrekas joind.in

  2. @nicolasgrekas # SymfonyCon

  3. #SymfonyCon @nicolasgrekas

  4. #SymfonyCon @nicolasgrekas

  5. #SymfonyCon @nicolasgrekas

  6. PHP-FIG Strength Standards! And wish the best to FIG 3.0

  7. PSR-6 Caching Interface Draft: Sept. 2013 Final: Dec. 2015

  8. #SymfonyCon @nicolasgrekas • A common interface for basic and intermediate-level

    caching needs. • Allow for multiple independent extensions without collision. PSR-6 Goals
  9. #SymfonyCon @nicolasgrekas • Compatibility with all existing cache implementations. •

    Advanced caching features such as namespacing or tagging. PSR-6 Non-Goals
  10. #SymfonyCon @nicolasgrekas • doctrine/cache 26.3M • sonata-project/cache 2.0M • zendframework/zend-cache

    1.9M • illuminate/cache 1.0M • tedivm/stash 0.6M PSR-6 Prior-art on Packagist
  11. #SymfonyCon @nicolasgrekas PSR-6 CacheItemInterface

  12. #SymfonyCon @nicolasgrekas PSR-6 CacheItemPoolInterface

  13. #SymfonyCon @nicolasgrekas PSR-6 Example #1

  14. #SymfonyCon @nicolasgrekas PSR-6 Example #2

  15. #SymfonyCon @nicolasgrekas • getItem is the only standardized item factory

    • Keys cannot use reserved chars • false is returned on backend-errors • Atomicity/transactions/laziness is unspecified • Cache items are not interoperable PSR-6 Edge-cases
  16. #SymfonyCon @nicolasgrekas • See on Packagist https://packagist.org/providers/psr/cache-implementation • Stash PHP

    – stashphp.com • PHP Cache – php-cache.com • and … PSR-6 Implementations
  17. Symfony Cache 3.1 What else?

  18. #SymfonyCon @nicolasgrekas • Be the most powerful PHP caching library

    • A strict PSR-6 implementation • Following Symfony’s quality processes* • Just the bare minimum to be fast by default * thank you Tobias for "cache/integration-tests“! Symfony Cache Goals in 3.1
  19. #SymfonyCon @nicolasgrekas • ApcuAdapter • ArrayAdapter • FilesystemAdapter • RedisAdapter

    • …it’s up to you to add more Base Symfony Cache Adapters
  20. #SymfonyCon @nicolasgrekas • AbstractAdapter • ChainAdapter • DoctrineAdapter • ProxyAdapter

    • + DoctrineProvider Special Symfony Cache Adapters
  21. #SymfonyCon @nicolasgrekas Doctrine 834.863 items/ms PHP Cache 313.334 items/ms Stash

    PHP 55.346 items/ms Symfony 533.366 items/ms Bench Symfony Cache - APCu
  22. #SymfonyCon @nicolasgrekas Doctrine 705.001 items/ms PHP Cache 362.477 items/ms Stash

    PHP 153.803 items/ms Symfony 845.910 items/ms Bench Symfony Cache - APCu
  23. #SymfonyCon @nicolasgrekas Doctrine 34.061 items/ms Symfony 478.910 items/ms Bench Symfony

    Cache - Redis
  24. Symfony Framework Wiring

  25. $container->get('cache.app') or @cache.app in yml

  26. #SymfonyCon @nicolasgrekas framework: cache: pools: my_app.cache.geodata => @my_app.cache.geodata in services.yml

    app/config/config.yml
  27. #SymfonyCon @nicolasgrekas framework: cache: pools: my_app.cache.geodata public: true => $container->get('my_app.cache.geodata‘)

    app/config/config.yml
  28. #SymfonyCon @nicolasgrekas framework: cache: pools: my_app.cache.geodata adapter: cache.adapter.redis app/config/config_prod.yml

  29. #SymfonyCon @nicolasgrekas framework: cache: app: cache.adapter.redis app/config/config_prod.yml

  30. #SymfonyCon @nicolasgrekas • « cache.app » for model-sourced items o

    Defaults to filesystem, change it to Redis • « cache.system » for code-sourced items o Defaults to APCu+filesystem chain Existing Cache Pool Services
  31. #SymfonyCon @nicolasgrekas services: geocoder.doctrine_cache: class: Symfony\Component\Cache\DoctrineProvider arguments: - '@my_app.cache.geodata' app/config/services.yml

  32. Symfony Cache 3.2 PSR-6 and beyond

  33. #SymfonyCon @nicolasgrekas ./bin/console cache:pool:clear my_app.geocoder.cache New in Symfony 3.2

  34. #SymfonyCon @nicolasgrekas • PdoAdapter works for Doctrine DBAL • PhpArrayAdapter

    one big PHP 7 static array in shm • PhpFileAdapter many small PHP 7 static array in shm • NullAdapter pro-tip: item factory for any Symfony adapters New in Symfony 3.2
  35. #SymfonyCon @nicolasgrekas • cache.system = PHP7 array > APCu >

    files • Offline cache warm-ups for derived cache.validator/serializer/annotations Note: APCu warmup & reset handled properly New in Symfony 3.2
  36. #SymfonyCon @nicolasgrekas • Tags based invalidation New in Symfony 3.2

  37. #SymfonyCon @nicolasgrekas Tags based invalidation

  38. #SymfonyCon @nicolasgrekas Tags based invalidation

  39. #SymfonyCon @nicolasgrekas • Serve stale items while refreshing? • Desynchronized

    Cache Misses? • Add a profiler panel with cache stats? • PSR-16 SimpleCache? • …add your ideas here Symfony 3.3?
  40. joind.in #SymfonyCon @nicolasgrekas Merci