2. Presentation title here
About me
https://www.facebook.com/yatsenco
https://github.com/anyt
about me…
Andrey Yatsenco
● PHP Developer at Oro Inc.
● 3 years with Symfony
● 6 years with PHP
3. Presentation title here
Data Cache Management in PHP
In this presentation
● Everything about cache
● Full-page cache
● Browser cache. HTTP cache
● Basics about cache
● About data cache
● Common pitfalls
4. Presentation title here
Data Cache Management in PHP
Cache is the temporary storage where
frequently asked or expensive computed data
can be stored for faster access
5. Presentation title here
Data Cache Management in PHP
Caching concepts
● Unique cache key
● Lifetime
● Clearing cache data by key
6. Presentation title here
Data Cache Management in PHP
Cache key
● All data identified by the key
● Key should be unique systemwide
● It’s good idea to use key prefixes
(namespaces)
○ Application namespace
○ Specific data namespace
7. Presentation title here
Data Cache Management in PHP
Lifetime (TTL)
● Expiration DateTime
● In seconds from save
○ internally DateTime used too
8. Presentation title here
Data Cache Management in PHP
Clearing cache data by key:
● Key should be unique
● Cache systems should provide ability to
delete data by key
9. Presentation title here
Data Cache Management in PHP
Why to cache?
● Reduce number of requests to database
● Reduce number of requests to external
resources (API, etc)
● Reduce number of requests to slow storage
(like file system)
● Reduce expensive computing data
recalculation
● You suppose to have high load
12. Presentation title here
Data Cache Management in PHP
Frontend Cache strategies:
● Page cache in browser
● Offline website cache
● Data cache
○ Cookie
○ Local Storage
13. Presentation title here
Data Cache Management in PHP
Backend Cache strategies:
● Entire page cache
● Parts of the page
● Opcode-cache
● Store sql-queries result
● Store complex php computing result
● You custom
14. Presentation title here
Data Cache Management in PHP
In Symfony before using data cache, it’s good
idea to enable default cache that works out of
the box
15. Presentation title here
Data Cache Management in PHP
Quick speed up Symfony application:
● Production mode for AppKernel
● use AppCache in front controller
● Doctrine Metadata cache
● Doctrine Query cache (not query result)
● composer dump-autoload --optimize
● Enable OpCode cache
16. Presentation title here
Data Cache Management in PHP
Next: Symfony HTTP cache for shared
pages:
Based on HTTP request headers
● Reverse proxy cache
● ESI cache
● Browser cache
http://symfony.com/doc/current/book/http_cache.html
20. Presentation title here
Data Cache Management in PHP
Custom Data Cache
● Expensive data computing
● External resources results
● Your custom data
21. Presentation title here
Data Cache Management in PHP
Custom Data Cache
● Doctrine Cache component
● Symfony 3.1 Cache component (PSR-6)
● PHP-Cache (PSR-6 + steroids)
22. Presentation title here
Data Cache Management in PHP
Doctrine Cache component
● Use DoctrineCacheBundle to connect
with Symfony '< 3.1'
● Used internally in Doctrine ORM
● Can be used without Doctrine ORM at all
23. Presentation title here
Data Cache Management in PHP
Doctrine Cache component
Supported providers:
● APC
● CouchBase
● Filesystem
● MongoDB
● Memcache
● Memcached
● Redis
● Riak
● SQLite3
● WinCache
● xCache
● Zend Data Cache
24. Presentation title here
Data Cache Management in PHP
Doctrine Cache component
Supported providers:
● Array
○ In memory cache that resets every request
● Chain
○ Chain of several caches, from fast and expensive to slow and cheap
26. Presentation title here
PSR-6 standard
The goal of this PSR is to allow developers to
create cache-aware libraries that can be
integrated into existing frameworks and
systems without the need for custom
development.
Data Cache Management in PHP
30. Presentation title here
Data Cache Management in PHP
Symfony cache component (from SF3.1)
https://github.com/symfony/cache
● Strict PSR-6 implementation
● Very simple and fast
● Has doctrine/cache proxy adapter for
advanced features
31. Presentation title here
Extra cache features
● Tags
○ Tags is used to control the invalidation of items.
● Hierarchy
○ Think of a hierarchy like a file system. If you remove a folder "Foo",
all items and folders in "Foo" will also be removed
Data Cache Management in PHP
32. Presentation title here
Extra cache features
● Tags
● Hierarchy
Not supported by doctrine or symfony cache
components
● Perhaps will be implemented in
doctrine/cache 2.0
Data Cache Management in PHP
33. Presentation title here
Extra cache features
● Tags
● Hierarchy
PHP-Cache library (PSR-6)
php-cache.com
Data Cache Management in PHP
36. Presentation title here
Data Cache Management in PHP
Pitfalls
● Premature optimization
● Caching only optimization
● Inability to easy invalidate cache item
● Cache slam
● Relying on cache data
● Inconsistency
● Cache works slower then cache target
37. Presentation title here
Data Cache Management in PHP
Links:
Psr-6
● www.php-fig.org/psr/psr-6/
Doctrine cache
● http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/caching.html
● http://symfony.com/doc/current/bundles/DoctrineCacheBundle/index.html
Symfony cache component
● https://github.com/symfony/cache
PHP-Cache project
● http://www.php-cache.com/en/latest/
Sergey Zhuravel presentation about scalability:
● http://www.slideshare.net/sergeyz/scalability-58564573
Vitaly Berdylo presentation about Symfony Performance
● http://www.slideshare.net/vitaliyberdylo/symfony2-performance-issues-and-improvements