Slide 1

Slide 1 text

t.me/aperitech bit.ly/devcalendar bit.ly/CodemotionTC #AperiTech Un’iniziativa di powered by

Slide 2

Slide 2 text

IN-MEMORY CACHING OBJECTS TECHNIQUES Mauro Cassani, PUG ROMA 30th May, 2017

Slide 3

Slide 3 text

ABOUT ME Mauro Cassani github.com/mauretto78

Slide 4

Slide 4 text

WHAT IS CACHE? In computing, a cache, is 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.[1] “ “ [1] https://en.wikipedia.org/wiki/Cache_(computing)

Slide 5

Slide 5 text

CACHING TECHNIQUES There are different caching methods that we can use with PHP: ➤ Caching content
 We can cache content in PHP by saving the final output of a specific script into the filesystem then simply serving it like a static file for a specific period of time instead of executing the original script.

Slide 6

Slide 6 text

CACHING TECHNIQUES There are different caching methods that we can use with PHP: ➤ Caching content
 We can cache content in PHP by saving the final output of a specific script into the filesystem then simply serving it like a static file for a specific period of time instead of executing the original script. ➤ Database Cache
 Caching the caching is done by the database server itself. This works by caching the results of the query so the query is only parsed the first time it runs and in the succeeding requests it won’t be parsed since the results from the specific query is already cached by the database server.

Slide 7

Slide 7 text

CACHING TECHNIQUES There are different caching methods that we can use with PHP: ➤ Caching content
 We can cache content in PHP by saving the final output of a specific script into the filesystem then simply serving it like a static file for a specific period of time instead of executing the original script. ➤ Database Cache
 Caching the caching is done by the database server itself. This works by caching the results of the query so the query is only parsed the first time it runs and in the succeeding requests it won’t be parsed since the results from the specific query is already cached by the database server. ➤ Memory Cache
 Memory caching techniques are widely used; they use the volatile memory data and are a lot more faster than caching in the file system.

Slide 8

Slide 8 text

MEMORY CACHING Memory caching techniques are widely used; they use the volatile memory data and are a lot more faster than caching in the file system. ➤ APCU [2]
 Alternative PHP Cache User Cache (APCU) provides a full backwards compatible API to the shared memory userland cache provided by APC. [2] http://php.net/manual/en/intro.apcu.php

Slide 9

Slide 9 text

MEMORY CACHING Memory caching techniques are widely used; they use the volatile memory data and are a lot more faster than caching in the file system. ➤ APCU [2]
 Alternative PHP Cache User Cache (APCU) provides a full backwards compatible API to the shared memory userland cache provided by APC. ➤ MEMCACHED [3]
 Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. [2] http://php.net/manual/en/intro.apcu.php
 [3] https://memcached.org/

Slide 10

Slide 10 text

MEMORY CACHING Memory caching techniques are widely used; they use the volatile memory data and are a lot more faster than caching in the file system. ➤ APCU [2]
 Alternative PHP Cache User Cache (APCU) provides a full backwards compatible API to the shared memory userland cache provided by APC. ➤ MEMCACHED [3]
 Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. ➤ REDIS [4]
 Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. [2] http://php.net/manual/en/intro.apcu.php
 [3] https://memcached.org/
 [4] https://redis.io/

Slide 11

Slide 11 text

APCU APCu (APC User Cache) is APC stripped of opcode caching. The first APCu codebase was versioned 4.0.0, it was forked from the head of the APC master branch at the time. PHP 7 support is available as of APCu 5.0.0. APCu can provide a compatibility mode, such that it can provide a drop in replacement for the applicable parts of APC.

Slide 12

Slide 12 text

APCU IN PHP

Slide 13

Slide 13 text

APCU IN PHP(2) prepare($sql); $stmt->bindValue(':id', $someId, PDO::PARAM_INT); $row = $stmt->fetch(PDO::FETCH_OBJ); apcu_store($myCacheKey, serialize($row)); } // Now I have access to $row, where I can do what I need to // And for subsequent calls, the data will be pulled from cache and skip // the query altogether var_dump(unserialize($row));

Slide 14

Slide 14 text

MEMCACHED Memcached is a distributed caching system in RAM for objects, and is widely used in dynamic web applications to relieve the load on the database, making it possible to serve some of the data to which access is most frequent, directly from RAM memory .

Slide 15

Slide 15 text

MEMCACHED Memcached is a distributed caching system in RAM for objects, and is widely used in dynamic web applications to relieve the load on the database, making it possible to serve some of the data to which access is most frequent, directly from RAM memory . Memcached was created in 2003 by the company operating LiveJournal, Danga Interactive, which had to handle more than 20 million exponential dynamic daily requests. The adoption of this innovative caching system has made it possible to drastically reduce the load on database servers and significantly improve site performance.

Slide 16

Slide 16 text

MEMCACHED Memcached is a distributed caching system in RAM for objects, and is widely used in dynamic web applications to relieve the load on the database, making it possible to serve some of the data to which access is most frequent, directly from RAM memory . Memcached was created in 2003 by the company operating LiveJournal, Danga Interactive, which had to handle more than 20 million exponential dynamic daily requests. The adoption of this innovative caching system has made it possible to drastically reduce the load on database servers and significantly improve site performance. The software was then publicly released under the BSD license that allowed it to be implemented by other web giants and the inevitable addition of many features and constant performance enhancement. Among the most famous memcached users we find informatics giants such as WordPress, Wikipedia, Flickr, Twitter, YouTube, Digg and Facebook.

Slide 17

Slide 17 text

MEMCACHED IN PHP addServer('localhost', 11211); // Set a value $memcached->set('foo', 'bar', 7200); $memcached->set('integer', 12345678, 7200); $memcached->set('boolean', true, 7200); $memcached->set('array', ['apple', 'banana', 'pear'], 7200); $memcached->set('object', serialize(new MyAwesomeClass()), 7200); // Get a value $memcached->get(‘foo'); // Update a value $memcached->replace('foo', 'barbar', 10200); // Change TTL $memcached->touch('foo', 29000); // Delete a value $memcached->delete('foo'); // Flush the cache $memcached->flush();

Slide 18

Slide 18 text

MEMCACHED IN PHP(2) addServer('localhost', 11211); $myCacheKey = 'my_cache_key'; $row = $cache->get($myCacheKey); if (!$row) { // Issue painful query to mysql $sql = "SELECT * FROM table WHERE id = :id"; $dbo->prepare($sql); $stmt->bindValue(':id', $someId, PDO::PARAM_INT); $row = $stmt->fetch(PDO::FETCH_OBJ); $memcached->set($myCacheKey, serialize($row)); } // Now I have access to $row, where I can do what I need to // And for subsequent calls, the data will be pulled from cache and skip // the query altogether var_dump(unserialize($row));

Slide 19

Slide 19 text

REDIS Redis is an in-memory database open-source software project sponsored by Redis Labs. It is networked, in-memory, and stores keys with optional durability.

Slide 20

Slide 20 text

REDIS Redis is an in-memory database open-source software project sponsored by Redis Labs. It is networked, in-memory, and stores keys with optional durability. The name Redis means REmote DIctionary Server. 
 Salvatore Sanfilippo, the original developer of Redis, was hired by VMware in March, 2010. In May, 2013, Redis was sponsored by Pivotal Software (a VMware spin-off). In June 2015, development became sponsored by Redis Labs.

Slide 21

Slide 21 text

REDIS Redis is an in-memory database open-source software project sponsored by Redis Labs. It is networked, in-memory, and stores keys with optional durability. The name Redis means REmote DIctionary Server. 
 Salvatore Sanfilippo, the original developer of Redis, was hired by VMware in March, 2010. In May, 2013, Redis was sponsored by Pivotal Software (a VMware spin-off). In June 2015, development became sponsored by Redis Labs. According to monthly rankings by DB-Engines.com, Redis is often ranked the most popular key-value database. Redis has also been ranked the #4 NoSQL database in user satisfaction and market presence based on user reviews, the most popular NoSQL database in containers, and the #1 NoSQL database among Top 50 Developer Tools & Services.

Slide 22

Slide 22 text

REDIS Redis is an in-memory database open-source software project sponsored by Redis Labs. It is networked, in-memory, and stores keys with optional durability. The name Redis means REmote DIctionary Server. 
 Salvatore Sanfilippo, the original developer of Redis, was hired by VMware in March, 2010. In May, 2013, Redis was sponsored by Pivotal Software (a VMware spin-off). In June 2015, development became sponsored by Redis Labs. According to monthly rankings by DB-Engines.com, Redis is often ranked the most popular key-value database. Redis has also been ranked the #4 NoSQL database in user satisfaction and market presence based on user reviews, the most popular NoSQL database in containers, and the #1 NoSQL database among Top 50 Developer Tools & Services. Someone defines Redis as “Memcached on Steroids”.

Slide 23

Slide 23 text

REDIS IN PHP PRedis [5] is flexible and feature-complete Redis client for PHP and HHVM by Daniele Alessandri. [5] https://github.com/nrk/predis

Slide 24

Slide 24 text

REDIS DATA TYPES: STRINGS The Redis String type is the simplest type of value you can associate with a Redis key. It is the only data type in Memcached, so it is also very natural for newcomers to use it in Redis.

Slide 25

Slide 25 text

REDIS DATA TYPES: STRINGS The Redis String type is the simplest type of value you can associate with a Redis key. It is the only data type in Memcached, so it is also very natural for newcomers to use it in Redis. If you don’t provide a ttl, the element will be persisted permanenty. set('foo', 'bar', 7200); $client->set('integer', 12345678, 7200); $client->set('boolean', true, 7200); $client->set('array', ['apple', 'banana', 'pear'], 7200); $client->set('object', serialize(new MyAwesomeClass()), 7200); // delete a value $client->del(‘foo’); // Get a string value $client->get(‘foo');

Slide 26

Slide 26 text

REDIS DATA TYPES: LISTS Redis lists are collections of string elements sorted according to the order of insertion. They are basically linked lists.

Slide 27

Slide 27 text

REDIS DATA TYPES: LISTS Redis lists are collections of string elements sorted according to the order of insertion. They are basically linked lists. lpush(’buylist’, ’apple'); $client->lpush(’buylist’, ’pie’); $client->rpush(’buylist’, ’pineapple’); // Get a string value $client->lrange(‘buylist’, 0, -1); // [‘pie’, ‘apple’, ‘pineapple’] // Remove the last two occurrences of ‘apple’ in the list $client->lrem,(‘buylist’, -2, ’apple');

Slide 28

Slide 28 text

REDIS DATA TYPES: SETS Redis Sets are collections of unique, unsorted string elements.

Slide 29

Slide 29 text

REDIS DATA TYPES: SETS Redis Sets are collections of unique, unsorted string elements. sadd(’tags:user:1’, ’apple'); $client->sadd(’tags:user:1’, ’pie'); $client->sadd(’tags:user:1’, ’pineapple’); // Get a set value $client->smembers(‘tags:user:1’); // [‘apple’, ‘pie’, ’pineapple’] $client->srem(’tags:user:1’, ’pineapple’); // removes ‘pineapple’ key

Slide 30

Slide 30 text

REDIS DATA TYPES: SETS Redis Sets are collections of unique, unsorted string elements. sadd(’tags:user:1’, ’apple'); $client->sadd(’tags:user:1’, ’pie'); $client->sadd(’tags:user:1’, ’pineapple’); // Get a set value $client->smembers(‘tags:user:1’); // [‘apple’, ‘pie’, ’pineapple’] $client->srem(’tags:user:1’, ’pineapple’); // removes ‘pineapple’ key sadd(’car:make:ford’, ’fiesta'); $client->sadd(’car:make:ford’, ’focus'); $client->sadd(’car:make:maserati’, ’quattroporte’); // Retrieve all ford molds $client->smembers(‘car:make:ford’); // [‘fiesta’, ‘focus’] CREATE AN INDEX

Slide 31

Slide 31 text

REDIS DATA TYPES: SORTED SETS Similar to Sets but where every string element is associated to a floating number value, called score. The elements are always taken sorted by their score, so unlike Sets it is possible to retrieve a range of elements

Slide 32

Slide 32 text

REDIS DATA TYPES: SORTED SETS Similar to Sets but where every string element is associated to a floating number value, called score. The elements are always taken sorted by their score, so unlike Sets it is possible to retrieve a range of elements zadd(’fruits’, 1, ’apple’); $client->zadd(’fruits’, 2, ’pie’); $client->zadd(’fruits’, 2.2, ’pineapple’); // get fruits sorted set $client->zrange(‘fruits’, 0, 1, [‘withscores’ => true]); $client->zrem(’fruits’, ’pineapple’); // removes ‘pineapple’ key

Slide 33

Slide 33 text

REDIS DATA TYPES: SORTED SETS Similar to Sets but where every string element is associated to a floating number value, called score. The elements are always taken sorted by their score, so unlike Sets it is possible to retrieve a range of elements zadd(’fruits’, 1, ’apple’); $client->zadd(’fruits’, 2, ’pie’); $client->zadd(’fruits’, 2.2, ’pineapple’); // get fruits sorted set $client->zrange(‘fruits’, 0, 1, [‘withscores’ => true]); $client->zrem(’fruits’, ’pineapple’); // removes ‘pineapple’ key zadd(’fruits’, 0, ’apple’); $client->zadd(’fruits’, 0, ’pie’); $client->zadd(’fruits’, 0, ’pineapple’); // Get all fruits sorted set with scores $client->zrangebylex(‘fruits’, 0, -1, [‘limit’ => [20, 5]]); LEXICOGRAPHICAL SORTING

Slide 34

Slide 34 text

REDIS DATA TYPES: HASHES Hashes, which are maps composed of fields associated with values. Both the field and the value are strings While hashes are handy to represent objects, actually the number of fields you can put inside a hash has no practical limits (other than available memory), so you can use hashes in many different ways inside your application.

Slide 35

Slide 35 text

REDIS DATA TYPES: HASHES Hashes, which are maps composed of fields associated with values. Both the field and the value are strings While hashes are handy to represent objects, actually the number of fields you can put inside a hash has no practical limits (other than available memory), so you can use hashes in many different ways inside your application. hset(’user:1’, ’name’, ‘Mauro'); $client->hset(’user:1’, ’surname’, ‘Cassani'); $client->hset(’user:1’, ’email’, ‘[email protected]’); // Get a string value $client->hget(‘user:1’, ’name’); // Mauro // Delete hash $client->hdel(‘user:1’, ’name’); // deletes Mauro // Get a string value $client->hgetall(‘user:1’); // full array

Slide 36

Slide 36 text

REDIS KEYS AND SCANS ➤ KEYS - returns all keys matching pattern.

Slide 37

Slide 37 text

REDIS KEYS AND SCANS ➤ KEYS - returns all keys matching pattern. ➤ SCAN - iterates the set of keys in the currently selected Redis database.

Slide 38

Slide 38 text

REDIS KEYS AND SCANS ➤ KEYS - returns all keys matching pattern. ➤ SCAN - iterates the set of keys in the currently selected Redis database. ➤ SSCAN - iterates elements of Sets types.

Slide 39

Slide 39 text

REDIS KEYS AND SCANS ➤ KEYS - returns all keys matching pattern. ➤ SCAN - iterates the set of keys in the currently selected Redis database. ➤ SSCAN - iterates elements of Sets types. ➤ HSCAN - iterates fields of Hash types and their associated values.

Slide 40

Slide 40 text

REDIS KEYS AND SCANS ➤ KEYS - returns all keys matching pattern. ➤ SCAN - iterates the set of keys in the currently selected Redis database. ➤ SSCAN - iterates elements of Sets types. ➤ HSCAN - iterates fields of Hash types and their associated values. ➤ ZSCAN - iterates elements of Sorted Set types and their associated scores.

Slide 41

Slide 41 text

REDIS KEYS AND SCANS keys(’user:*’) // Search for all keys starting by user foreach (new Iterator\Keyspace($client, 'predis:*') as $key) { // SCAN } foreach (new Iterator\SetKey($client, 'predis:set') as $member) { // SSCAN } foreach (new Iterator\SortedSetKey($client, 'predis:zset') as $member => $rank) { // ZSCAN } foreach (new Iterator\HashKey($client, 'predis:hset') as $field => $value) { // HSCAN } ➤ KEYS - returns all keys matching pattern. ➤ SCAN - iterates the set of keys in the currently selected Redis database. ➤ SSCAN - iterates elements of Sets types. ➤ HSCAN - iterates fields of Hash types and their associated values. ➤ ZSCAN - iterates elements of Sorted Set types and their associated scores.

Slide 42

Slide 42 text

BRIEF COMPARISON APCU MEMCACHED REDIS documentation + ++ +++ (multi)get ✓ ✓ (multi)set ✓ ✓ incr./decr. ✓ ✓ ✓ delete ✓ ✓ ✓ expiration(ttl) ✓ ✓ ✓ range queries ✓ data types ✓ persistance ✓

Slide 43

Slide 43 text

BRIEF COMPARISON (2) APCU MEMCACHED REDIS clustering ✓ ✓ multi-threaded ✓ replication ✓ key length 256 bytes 512 Mb max value size 1 Mb 1 Mb 512 Mb data volume max size 3.5 TiB 4.7 TiB+ read/write +++ +++ ++ storage type in memory in memory disk + in memory cli ✓ ✓

Slide 44

Slide 44 text

REAL WORLD USE CASE Think on this: ➤ You have some lists coming from your database or your API;

Slide 45

Slide 45 text

REAL WORLD USE CASE Think on this: ➤ You have some lists coming from your database or your API; ➤ You want to perform some queries on your lists;

Slide 46

Slide 46 text

REAL WORLD USE CASE Think on this: ➤ You have some lists coming from your database or your API; ➤ You want to perform some queries on your lists; ➤ For example, imagine you want to display hundred of POIs in Google Maps, and allow the user to make some query via a dropdown menu, or maybe you want to allow your users to perform a simple text search on your list;

Slide 47

Slide 47 text

REAL WORLD USE CASE Think on this: ➤ You have some lists coming from your database or your API; ➤ You want to perform some queries on your lists; ➤ For example, imagine you want to display hundred of POIs in Google Maps, and allow the user to make some query via a dropdown menu, or maybe you want to allow your users to perform a simple text search on your list; ➤ You want your site is super-fast (so you won’t to make more queries on you db);

Slide 48

Slide 48 text

REAL WORLD USE CASE Think on this: ➤ You have some lists coming from your database or your API; ➤ You want to perform some queries on your lists; ➤ For example, imagine you want to display hundred of POIs in Google Maps, and allow the user to make some query via a dropdown menu, or maybe you want to allow your users to perform a simple text search on your list; ➤ You want your site is super-fast (so you won’t to make more queries on you db); ➤ Of course, you need to cache your data!

Slide 49

Slide 49 text

REAL WORLD USE CASE Think on this: ➤ You have some lists coming from your database or your API; ➤ You want to perform some queries on your lists; ➤ For example, imagine you want to display hundred of POIs in Google Maps, and allow the user to make some query via a dropdown menu, or maybe you want to allow your users to perform a simple text search on your list; ➤ You want your site is super-fast (so you won’t to make more queries on you db); ➤ Of course, you need to cache your data! HOW TO ACHIEVE IT?

Slide 50

Slide 50 text

APPROACH “A” get(‘lista')){ // apcu_fetch with APCu $m->set('lista', $arrayOfElements, $ttl); // apcu_store with APCu } foreach ($m->get('lista') as $key => $item){ // apcu_fetch with APCu $item = unserialize($item); // ... }

Slide 51

Slide 51 text

APPROACH “A” keys('lista')){ foreach ($arrayOfElements as $item){ $c->hset('lista', $item->{id}, serialize($item)); } } foreach ($c->hgetall('lista') as $key => $item){ $item = unserialize($item); // ... } get(‘lista')){ // apcu_fetch with APCu $m->set('lista', $arrayOfElements, $ttl); // apcu_store with APCu } foreach ($m->get('lista') as $key => $item){ // apcu_fetch with APCu $item = unserialize($item); // ... }

Slide 52

Slide 52 text

APPROACH “B” set('lista:'.$element->id, serialize($element), $ttl); // apcu_store $arrayOfIdElements[(string) $element->id] = 'lista:'.$element->id; } $m->set('lista', $arrayOfIdElements, $ttl); foreach ($m->get('lista') as $key){ // apcu_fetch with APCu $item = $m->get($key); // ... // apcu_fetch with APCu }

Slide 53

Slide 53 text

APPROACH “B” keys('lista:*'); if(!$list){ foreach ($arrayOfElements as $item){ $c->hset('lista:'.$item->id, 'element', serialize($item)); } } foreach ($list as $item) { $item = unserialize($c->hget($item, 'element')); } set('lista:'.$element->id, serialize($element), $ttl); // apcu_store $arrayOfIdElements[(string) $element->id] = 'lista:'.$element->id; } $m->set('lista', $arrayOfIdElements, $ttl); foreach ($m->get('lista') as $key){ // apcu_fetch with APCu $item = $m->get($key); // ... // apcu_fetch with APCu }

Slide 54

Slide 54 text

SOLUTION: CHUNK! set(‘lista:counter', count($arrayOfElements)); if(!$m->get(‘lista:chunk-1’)){ foreach (array_chunk($arrayOfElements, 1000) as $chunk_number => $item){ $arrayToPersist = []; foreach ($item as $key => $value){ $arrayToPersist[$key] = $value; } $m->set(‘lista:chunk-‘.($chunk_number + 1), $arrayToPersist); } }

Slide 55

Slide 55 text

SOLUTION: CHUNK! $number = ceil($m->get(‘lista:counter') / 1000); $list = []; for ($i=1; $i<=$number; $i++){ if(empty($list)){ $list = $m->get(‘lista:chunk-1’); } else { $list = array_merge($collection, $m->get(‘lista:chunk-'.$i)); } } foreach ($list as $key => $item){ // ... } set(‘lista:counter', count($arrayOfElements)); if(!$m->get(‘lista:chunk-1’)){ foreach (array_chunk($arrayOfElements, 1000) as $chunk_number => $item){ $arrayToPersist = []; foreach ($item as $key => $value){ $arrayToPersist[$key] = $value; } $m->set(‘lista:chunk-‘.($chunk_number + 1), $arrayToPersist); } }

Slide 56

Slide 56 text

SOLUTION: CHUNK! (2) set(‘lista:counter', count($arrayOfElements)); if(!$c->hgetall(‘lista:chunk-1’)){ foreach (array_chunk($arrayOfElements, 1000) as $chunk_number => $item){ foreach ($item as $key => $value){ $c->hset(‘lista:chunk-‘.($chunk_number+1), $key, $value); } }

Slide 57

Slide 57 text

SOLUTION: CHUNK! (2) set(‘lista:counter', count($arrayOfElements)); if(!$c->hgetall(‘lista:chunk-1’)){ foreach (array_chunk($arrayOfElements, 1000) as $chunk_number => $item){ foreach ($item as $key => $value){ $c->hset(‘lista:chunk-‘.($chunk_number+1), $key, $value); } } $number = ceil($c->get(‘lista:counter') / 1000); $list = []; for ($i=1; $i<=$number; $i++){ if(empty($list)){ $list = $c->hgetall(‘lista:chunk-1'); } else { $list = array_merge($collection, $c->hgetall(‘lista:chunk-'. $i)); } } foreach ($list as $key => $item){ // ... }

Slide 58

Slide 58 text

IN-MEMORY LIST In-memory List [6] is a PHP library that allows you to create and store in memory your lists, and then quickly retrieve and perform queries on it. [6] https://github.com/mauretto78/in-memory-list

Slide 59

Slide 59 text

ARCHITECTURE Domain Entities + Factory Client + QB

Slide 60

Slide 60 text

ARCHITECTURE Domain Infrastructure Entities + Factory Client + QB Repo + Drivers

Slide 61

Slide 61 text

ARCHITECTURE Domain Infrastructure Application Entities + Factory Client + QB Repo + Drivers

Slide 62

Slide 62 text

DOMAIN MODEL ListCollection $items $uuid $headers ListCollectionUuid $uuid Here is the UML Diagram of Domain Modal: ListElement $uuid $body ListElementUuid $uuid

Slide 63

Slide 63 text

BASIC USAGE create($array);

Slide 64

Slide 64 text

BASIC USAGE create($array); foreach ($collection as $element){ $item = $client->item($element); // ... } Now you can iterate over all elements:

Slide 65

Slide 65 text

DRIVERS

Slide 66

Slide 66 text

DRIVERS

Slide 67

Slide 67 text

DRIVERS $client = new Client('redis', $redis_params); // Redis

Slide 68

Slide 68 text

PARAMETERS create($array, $parameters);

Slide 69

Slide 69 text

PARAMETERS When use create method to generate a list, you can provide to it a parameters array. The allowed keys are: create($array, $parameters);

Slide 70

Slide 70 text

PARAMETERS When use create method to generate a list, you can provide to it a parameters array. The allowed keys are: ➤ uuid - uuid of list create($array, $parameters);

Slide 71

Slide 71 text

PARAMETERS When use create method to generate a list, you can provide to it a parameters array. The allowed keys are: ➤ uuid - uuid of list ➤ element-uuid - uuid for the list elements create($array, $parameters);

Slide 72

Slide 72 text

PARAMETERS When use create method to generate a list, you can provide to it a parameters array. The allowed keys are: ➤ uuid - uuid of list ➤ element-uuid - uuid for the list elements ➤ headers - headers array for the list create($array, $parameters);

Slide 73

Slide 73 text

PARAMETERS When use create method to generate a list, you can provide to it a parameters array. The allowed keys are: ➤ uuid - uuid of list ➤ element-uuid - uuid for the list elements ➤ headers - headers array for the list ➤ index - add the list elements to cache index or not create($array, $parameters);

Slide 74

Slide 74 text

PARAMETERS When use create method to generate a list, you can provide to it a parameters array. The allowed keys are: ➤ uuid - uuid of list ➤ element-uuid - uuid for the list elements ➤ headers - headers array for the list ➤ index - add the list elements to cache index or not ➤ chunk-size - the chunks size in which the array will be splitted (integer) create($array, $parameters);

Slide 75

Slide 75 text

PARAMETERS When use create method to generate a list, you can provide to it a parameters array. The allowed keys are: ➤ uuid - uuid of list ➤ element-uuid - uuid for the list elements ➤ headers - headers array for the list ➤ index - add the list elements to cache index or not ➤ chunk-size - the chunks size in which the array will be splitted (integer) ➤ ttl - time to live of the list (in seconds) create($array, $parameters);

Slide 76

Slide 76 text

QUERIES create($array, [‘uuid' => 'simple-array']); $qb = new QueryBuilder($collection); $qb ->addCriteria('title', 'Mario', 'CONTAINS') ->addCriteria('group_id', '45') ->addCriteria('rate', '3', '>') ->orderBy(‘title') ->limit(30); foreach ($qb->getResults() as $element){ $item = $client->item($element); // ... }

Slide 77

Slide 77 text

A SIMPLE BENCHMARK $number, 'name' => 'Name '.$number, 'email' => 'Email'.$number, ]; } $apiArray = json_encode($array); $client = new Client($driver, $params); $collection = $client->findListByUuid('range-list') ?: $client- >create(json_decode($apiArray), [‘uuid' => 'range-list', 
 ‘element-uuid’ => 'id']); foreach ($collection as $element) { $item = $client->item($element); echo 'id: '.$item->id.'
'; echo 'name: '.$item->name.'
'; echo 'email: '.$item->email.'
'; } echo ' ELAPSED TIME: '.$time_elapsed_secs = microtime(true) - $start;

Slide 78

Slide 78 text

BENCHMARK RESULTS seconds 0 0.75 1.5 2.25 3 number of items 5000 10000 20000 30000 40000 50000 var_dump (Redis) echo (Redis) var_dump (MC) echo (MC) var_dump (Apcu) echo (Apcu)

Slide 79

Slide 79 text

FINAL RECAP: WHO WINS? Small Projects No great differences High traffic websites ✓ Memcached Real-time applications, 
 secondary database persist ✓ Redis

Slide 80

Slide 80 text

THANK YOU! ANY QUESTIONS?