Data cache management in php

38
Presentation title here Data Cache Management in PHP

Transcript of Data cache management in php

Page 1: Data cache management in php

Presentation title here

Data Cache Managementin PHP

Page 2: Data cache management in 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

Page 3: Data cache management in 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

Page 4: Data cache management in php

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

Page 5: Data cache management in php

Presentation title here

Data Cache Management in PHP

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

Page 6: Data cache management in php

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

Page 7: Data cache management in php

Presentation title here

Data Cache Management in PHP

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

○ internally DateTime used too

Page 8: Data cache management in php

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

Page 9: Data cache management in php

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

Page 10: Data cache management in php

Presentation title here

Data Cache Management in PHP

Cache alternatives?● Code refactoring

Page 11: Data cache management in php

Presentation title here

Data Cache Management in PHP

Cache strategies:● Frontend cache● Backend cache

Page 12: Data cache management in php

Presentation title here

Data Cache Management in PHP

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

○ Cookie ○ Local Storage

Page 13: Data cache management in php

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

Page 14: Data cache management in php

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

Page 15: Data cache management in php

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

Page 16: Data cache management in php

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

Page 17: Data cache management in php

Presentation title here

Data Cache Management in PHP

When to use Data cache?

● Displays different

Page 18: Data cache management in php

Presentation title here

Data Cache Management in PHP

Doctrine Query Result cache

● You need to configure it per query

Page 19: Data cache management in php

Presentation title here

Data Cache Management in PHP

Doctrine Query Result cache● Clearing cache by ID

Page 20: Data cache management in php

Presentation title here

Data Cache Management in PHP

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

Page 21: Data cache management in php

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)

Page 22: Data cache management in php

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

Page 23: Data cache management in php

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

Page 24: Data cache management in php

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

Page 25: Data cache management in php

Presentation title here

Data Cache Management in PHPDoctrine Cache component usage example:

Page 26: Data cache management in php

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

Page 27: Data cache management in php

Presentation title here

PSR-6 standardKey concepts:● Items● Pool

Data Cache Management in PHP

Page 28: Data cache management in php

Presentation title here

Data Cache Management in PHPPSR-6 standard

Page 29: Data cache management in php

Presentation title here

Data Cache Management in PHPPSR-6 standard

Page 30: Data cache management in php

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

Page 31: Data cache management in php

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

Page 32: 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

Page 33: 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

Page 34: Data cache management in php

Presentation title here

PHP-Cache Tags usage example:

Data Cache Management in PHP

Page 35: Data cache management in php

Presentation title here

PHP-Cache Hierarchy usage example:

Data Cache Management in PHP

Page 36: 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

Page 37: Data cache management in php

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

Page 38: Data cache management in php

Presentation title here

?