Slide 1

Slide 1 text

Demystifying Luís Cobucci
 @lcobucci https://goo.gl/oZPuRc cache in Doctrine ORM

Slide 2

Slide 2 text

Reliability https://goo.gl/1shwsY

Slide 3

Slide 3 text

Maintainability https://goo.gl/D4DvKC

Slide 4

Slide 4 text

Scalability https://goo.gl/M67mS8

Slide 5

Slide 5 text

Trade-offs https://goo.gl/L7YdSp

Slide 6

Slide 6 text

Minions of users! https://goo.gl/7MAkHZ

Slide 7

Slide 7 text

“ There are two hard things in Computer Science: cache invalidation, naming things, and off-by-one errors Common sense

Slide 8

Slide 8 text

Luís Cobucci
 @lcobucci

Slide 9

Slide 9 text

“ a hardware or software component that stores data so future requests for that data can be served faster; the data stored in a cache might be the result of an earlier computation, or the duplicate of data stored elsewhere. Cache (computing) - Wikipedia

Slide 10

Slide 10 text

Our software Cache External API

Slide 11

Slide 11 text

Our software Cache External API 1. Give me “external.result”

Slide 12

Slide 12 text

Our software Cache External API 2. Cache miss 1. Give me “external.result”

Slide 13

Slide 13 text

Our software Cache External API 2. Cache miss 3. Give me data 1. Give me “external.result”

Slide 14

Slide 14 text

Our software Cache External API 2. Cache miss 3. Give me data 4. There you go 1. Give me “external.result”

Slide 15

Slide 15 text

Our software Cache External API 2. Cache miss 1. Give me “external.result” 3. Give me data 4. There you go 5. Store “external.result”

Slide 16

Slide 16 text

Our software Cache External API 2. Cache miss 1. Give me “external.result” 3. Give me data 4. There you go 5. Store “external.result” 6. Stored!

Slide 17

Slide 17 text

Our software Cache External API 1. Give me “external.result”

Slide 18

Slide 18 text

Our software Cache External API 2. Cache hit! 1. Give me “external.result”

Slide 19

Slide 19 text

Doctrine ORM? https://goo.gl/xCBz84

Slide 20

Slide 20 text

Template Message Consumer Campaign 1 1 1 * * *

Slide 21

Slide 21 text

Template Message Consumer Campaign 1 1 1 * * *

Slide 22

Slide 22 text

Template Message Consumer Campaign 1 1 1 * * * Order matters

Slide 23

Slide 23 text

Template Message Consumer Campaign 1 1 1 * * * INSERT INTO consumer (name, email)
 VALUES (“Luís Cobucci”, “[email protected]”);
 SET @consumer_id = LAST_INSERT_ID();

Slide 24

Slide 24 text

Template Message Consumer Campaign 1 1 1 * * * INSERT INTO consumer (name, email)
 VALUES (“Luís Cobucci”, “[email protected]”);
 SET @consumer_id = LAST_INSERT_ID(); INSERT INTO template (name, body)
 VALUES (“Template 1”, “blah… blah… blah…”);

Slide 25

Slide 25 text

Template Message Consumer Campaign 1 1 1 * * * INSERT INTO consumer (name, email)
 VALUES (“Luís Cobucci”, “[email protected]”);
 SET @consumer_id = LAST_INSERT_ID(); INSERT INTO template (name, body)
 VALUES (“Template 1”, “blah… blah… blah…”); INSERT INTO campaign (title, template_id, message)
 VALUES (“Test”, LAST_INSERT_ID(), “blah… blah… blah…”);

Slide 26

Slide 26 text

Template Message Consumer Campaign 1 1 1 * * * INSERT INTO consumer (name, email)
 VALUES (“Luís Cobucci”, “[email protected]”);
 SET @consumer_id = LAST_INSERT_ID(); INSERT INTO template (name, body)
 VALUES (“Template 1”, “blah… blah… blah…”); INSERT INTO campaign (title, template_id, message)
 VALUES (“Test”, LAST_INSERT_ID(), “blah… blah… blah…”); INSERT INTO message (campaign_id, consumer_id, sent)
 VALUES (LAST_INSERT_ID(), @consumer_id, FALSE);

Slide 27

Slide 27 text

Template Message Consumer Campaign 1 1 1 * * * INSERT INTO consumer (name, email)
 VALUES (“Luís Cobucci”, “[email protected]”);
 SET @consumer_id = LAST_INSERT_ID(); INSERT INTO template (name, body)
 VALUES (“Template 1”, “blah… blah… blah…”); INSERT INTO campaign (title, template_id, message)
 VALUES (“Test”, LAST_INSERT_ID(), “blah… blah… blah…”); INSERT INTO message (campaign_id, consumer_id, sent)
 VALUES (LAST_INSERT_ID(), @consumer_id, FALSE); Don’t forget to use a transaction!

Slide 28

Slide 28 text

EntityManager persist() flush() find() remove()

Slide 29

Slide 29 text

declare(strict_types=1); $consumer = new Consumer('Luís Cobucci', '[email protected]');
 $template = new Template('Template 1', 'blah… blah… blah…');
 $campaign = new Campaign($template, 'Test', 'blah… blah… blah…');
 $message = new Message($campaign, $consumer, false);
 
 $entityManager->persist($consumer); $entityManager->persist($template); $entityManager->persist($campaign); $entityManager->persist($message); $entityManager->flush();

Slide 30

Slide 30 text

declare(strict_types=1); $consumer = $entityManager->find('Consumer', 2); $campaign = $entityManager->find('Campaign', 1);
 
 $message = new Message($campaign, $consumer, false);
 
 $entityManager->persist($message); $entityManager->flush();

Slide 31

Slide 31 text

http://goo.gl/gH0hsx Amazing, right?

Slide 32

Slide 32 text

Make it fast! https://goo.gl/6Y2kQd

Slide 33

Slide 33 text

Metadata https://goo.gl/AhjBFJ

Slide 34

Slide 34 text

/** @ORM\Entity */ class Consumer
 {
 /** 
 * @ORM\Id
 * @ORM\GeneratedValue 
 * @ORM\Column(type="integer")
 */ private $id;
 
 /** @ORM\Column(type="string") */
 private $name;
 
 /** @ORM\Column(type="string") */
 private $email; }

Slide 35

Slide 35 text

/** @ORM\Entity */ class Consumer
 {
 /** 
 * @ORM\Id
 * @ORM\GeneratedValue 
 * @ORM\Column(type="integer")
 */ private $id;
 
 /** @ORM\Column(type="string") */
 private $name;
 
 /** @ORM\Column(type="string") */
 private $email; } ClassMetadata

Slide 36

Slide 36 text

Query https://goo.gl/nuFRba

Slide 37

Slide 37 text

SELECT consumer FROM Consumer consumer;

Slide 38

Slide 38 text

SELECT consumer FROM Consumer consumer; SELECT
 c0.id AS id_0,
 c0.name AS name_1, c0.email AS email_2,
 FROM consumer c0;

Slide 39

Slide 39 text

Result set https://goo.gl/7yah7S

Slide 40

Slide 40 text

$query = 'SELECT COUNT(m) FROM Message m WHERE m.user = :user'; 
 $count = $entityManager->createQuery($query)
 ->setParameter('user', 1)
 ->getSingleScalarResult();

Slide 41

Slide 41 text

$query = 'SELECT COUNT(m) FROM Message m WHERE m.user = :user'; 
 $count = $entityManager->createQuery($query)
 ->setParameter('user', 1)
 ->useResultCache(true)
 ->useResultCacheLifeTime(3600)
 ->getSingleScalarResult();

Slide 42

Slide 42 text

L2C https://goo.gl/8pbG3y

Slide 43

Slide 43 text

https://goo.gl/cuLYT9 Bugs…

Slide 44

Slide 44 text

https://goo.gl/1y3fvm Bugs…

Slide 45

Slide 45 text

https://goo.gl/D3Gw7e Bugs…

Slide 46

Slide 46 text

EntityManager UnitOfWork DB

Slide 47

Slide 47 text

EntityManager UnitOfWork L2C DB

Slide 48

Slide 48 text

/**
 * @ORM\Entity
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
 */ class Consumer
 {
 // …
 }

Slide 49

Slide 49 text

/**
 * @ORM\Entity
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
 */ class Consumer
 {
 // …
 } READ_ONLY
 NONSTRICT_READ_WRITE
 READ_WRITE

Slide 50

Slide 50 text

/**
 * @ORM\Entity
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
 */ class Message
 {
 // …
 
 /**
 * @ORM\ManyToOne(targetEntity="Consumer") * @ORM\Cache(usage="READ_ONLY") */
 private $consumer; 
 // …
 }

Slide 51

Slide 51 text

$query = ‘SELECT m FROM Message m WHERE m.user = :user'; 
 $messages = $entityManager->createQuery($query)
 ->setParameter('user', 1)
 ->setCacheable(true)
 ->getResult();

Slide 52

Slide 52 text

https://goo.gl/bGP8u8 Intense traffic

Slide 53

Slide 53 text

Demystifying Luís Cobucci
 @lcobucci https://goo.gl/oZPuRc cache in Doctrine ORM

Slide 54

Slide 54 text

Thanks! @lcobucci