Slide 1

Slide 1 text

Caching with PSRs drukwerkdeal.nl @hannesvdvreken

Slide 2

Slide 2 text

Hi, my name is Hannes.

Slide 3

Slide 3 text

!

Slide 4

Slide 4 text

madewithlove.be

Slide 5

Slide 5 text

Caching PSRs Caching in PHP anno 2017

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

1. Intro what is caching?

Slide 8

Slide 8 text

WHAT IS CACHING Speeding up your app

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

WHAT IS CACHING Should not cause app failure

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

WHAT IS CACHING Should not cause app failure

Slide 13

Slide 13 text

WHAT IS CACHING geocode IP check cache store result check cache

Slide 14

Slide 14 text

WHAT IS CACHING Should not cause app failure

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

WHAT IS CACHING Should not cause app failure

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

APPLICATION CACHING IN 2015 Adapters everywhere!

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

3. Hello PSR-6

Slide 24

Slide 24 text

INTRODUCING PSR-6 Finalised & accepted in December 2015

Slide 25

Slide 25 text

INTRODUCING PSR-6 Repository - Entity

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

INTRODUCING PSR-6 Repository - Entity model allows extensions

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

STAMPEDE PROTECTION

Slide 33

Slide 33 text

STAMPEDE PROTECTION

Slide 34

Slide 34 text

STAMPEDE PROTECTION

Slide 35

Slide 35 text

STAMPEDE PROTECTION

Slide 36

Slide 36 text

STAMPEDE PROTECTION

Slide 37

Slide 37 text

STAMPEDE PROTECTION

Slide 38

Slide 38 text

STAMPEDE PROTECTION

Slide 39

Slide 39 text

STAMPEDE PROTECTION

Slide 40

Slide 40 text

STAMPEDE PROTECTION

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

START USING IT - UPGRADE Upgrade paths

Slide 52

Slide 52 text

START USING IT - UPGRADE Libraries

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

START USING IT - UPGRADE Frameworks

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

cache/illuminate-adapter

Slide 59

Slide 59 text

START USING IT Adoption?

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

4. Hello PSR-16

Slide 62

Slide 62 text

INTRODUCING PSR-16 Finalised & accepted in December 2016

Slide 63

Slide 63 text

One simple interface INTRODUCING PSR-16

Slide 64

Slide 64 text

Psr\SimpleCache\CacheInterface INTRODUCING PSR-16

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

START USING IT Upgrade path? Same as PSR-6

Slide 74

Slide 74 text

START USING IT Adoption?

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

Thank you! @hannesvdvreken

Slide 79

Slide 79 text

Time for questions! @hannesvdvreken