Data cache management in php

Post on 14-Jan-2017

403 views 1 download

Transcript of Data cache management in php

Presentation title here

Data Cache Managementin PHP

Presentation title here

About me

https://www.facebook.com/yatsencohttps://github.com/anyt

about me…

Andrey Yatsenco

● PHP Developer at Oro Inc.● 3 years with Symfony ● 6 years with PHP

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

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

Presentation title here

Data Cache Management in PHP

Caching concepts● Unique cache key● Lifetime● Clearing cache data by key

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

Presentation title here

Data Cache Management in PHP

Lifetime (TTL)● Expiration DateTime● In seconds from save

○ internally DateTime used too

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

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

Presentation title here

Data Cache Management in PHP

Cache alternatives?● Code refactoring

Presentation title here

Data Cache Management in PHP

Cache strategies:● Frontend cache● Backend cache

Presentation title here

Data Cache Management in PHP

Frontend Cache strategies:● Page cache in browser● Offline website cache● Data cache

○ Cookie ○ Local Storage

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

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

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

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

Presentation title here

Data Cache Management in PHP

When to use Data cache?

● Displays different

Presentation title here

Data Cache Management in PHP

Doctrine Query Result cache

● You need to configure it per query

Presentation title here

Data Cache Management in PHP

Doctrine Query Result cache● Clearing cache by ID

Presentation title here

Data Cache Management in PHP

Custom Data Cache● Expensive data computing● External resources results● Your custom data

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)

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

Presentation title here

Data Cache Management in PHP

Doctrine Cache componentSupported providers:

● APC● CouchBase● Filesystem● MongoDB● Memcache● Memcached

● Redis● Riak● SQLite3● WinCache● xCache● Zend Data Cache

Presentation title here

Data Cache Management in PHP

Doctrine Cache componentSupported providers:

● Array○ In memory cache that resets every request

● Chain○ Chain of several caches, from fast and expensive to slow and cheap

Presentation title here

Data Cache Management in PHPDoctrine Cache component usage example:

Presentation title here

PSR-6 standardThe 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

Presentation title here

PSR-6 standardKey concepts:● Items● Pool

Data Cache Management in PHP

Presentation title here

Data Cache Management in PHPPSR-6 standard

Presentation title here

Data Cache Management in PHPPSR-6 standard

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

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

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

Presentation title here

Extra cache features● Tags● Hierarchy

PHP-Cache library (PSR-6)php-cache.com

Data Cache Management in PHP

Presentation title here

PHP-Cache Tags usage example:

Data Cache Management in PHP

Presentation title here

PHP-Cache Hierarchy usage example:

Data Cache Management in PHP

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

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

Presentation title here

?