Slide 1

Slide 1 text

DALLAS PHP MAY 2014

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Get Recent Posts Get User By ID

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Potentially hundreds of static HTML files on the web server.  But, static serving is fast!  Yes… yes it is… sometimes…  Pain to invalidate in some cases. Have to wrap every single app output with cache collecting and storage. May even have to refactor the app depending on how output is sent out. (lots of echos everywhere vs a template, etc, whatever)  But WordPress has a plugin for that!  Good for you. Dynamic site has become more of a self updating site rather than an application.

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

if cache exists return cached data else query database fetch data result cache data return data

Slide 15

Slide 15 text

PRO Typically smaller memory footprint. Abstracts the cache at a very high level that is easy to intercept. Interception being key. You could integrity test it against the database, if you wanted. CON Still requires PHP to parse data and create your objects for each requested object.

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

if cache exists return cached object else query database fetch result build object from data cache object return object

Slide 18

Slide 18 text

PRO Objects are brought back just as you left them. Objects don‘t need to construct again. Just inflate. CON Requires deeper integration with the code platform depending on application structure. May require architecture changes – this style works much nicer with Factory style building. If an object is changed, such as a property renamed, all the cached versions will be broke. A forced flush!

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Local to the application instance/hit/request. Great for storing objects which tend to be small and reused multiple times an application, such as users objects. Installation: None. It’s an application design feature. Overly Simple Example: Cache::$Data = array( ‘user-1’ => User Object { ‘ID’=>1, ‘Name’=‘Bob’ }, ‘user-117’ => User Object { ‘ID’=>117, ‘Name’=‘John’ } );

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Local to the application cluster/network. We use it the same as the Appcache, only it magically persists across page hits or requests. Memcache is a server for key=>value storage. It allows you to build a pool of servers working together to create one giant cache. Servers can be added and dropped as needed. http://memcached.org/ It can be accessed with PHP via the Memcache extension. http://www.php.net/memcache (note, PHP has memcache and memcacheD… without the D is the preferred, updated, and up to date version. However, the preferred server daemon does have the D.)

Slide 24

Slide 24 text

DB Master PHP App Slave Slave Slave Load Balancer

Slide 25

Slide 25 text

PHP App (running libmemcached) Server Server Server

Slide 26

Slide 26 text

Server Installation: apt-get install memcached service memcached start Client Installation apt-get install php5-memcache

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

- Data / Connection Management. Single point of entry for multiple end points. - Store Data. - Retrieve Data. - Invalidate / Delete Data. - Bonus: Invalidate / Delete ALL THE THINGS.

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

If using a Cache layer that does not automatically flatten data for you on store like Memcache does… $obj = (object)[‘Hello’ => ‘World’]; var_dump(serialize($obj)); > O:13:“stdClass":1:{s:5:"Hello";s:5:"World";} var_dump(unserialize(serialize($obj)); > Object stdClass { > ["Hello"] => string(5) "World“ > }

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

get_user(1); checking cache… i no has. i no has. checking database… lol, fools. i has it. ok now I has it. ok now i has it. here is user with id 1

Slide 40

Slide 40 text

get_user(1); checking cache… i has i has! here it is. here is user with id 1

Slide 41

Slide 41 text

get_user(1); checking cache… i no has. oh oh i has! here it is. now i has it. here is user with id 1

Slide 42

Slide 42 text

get_user(1); checking cache… i has i has! here it is. here is user with id 1

Slide 43

Slide 43 text

No content

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

This constructor is fetching data, not constructing data. This constructor is limited to searching by User ID. User instances do not need access to fetch methods, as this one will have.

Slide 48

Slide 48 text

Constructor only contains logic to validate and construct the object given the building materials it was given. The finished Object is cached. GetByID knows that the cache contains complete objects. Static functions means objects don’t have access to instance methods they don’t need. A user doesn’t need to know how to find itself by its Email when you already have found it.

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Doctrine Cache (App, APC, Disk, Memcache, MongoDB, Redis, …)  https://github.com/doctrine/cache Symfony 2 Cache (App, APC, Disk, Memcache, Redis, …)  https://github.com/illuminate/cache Nether Cache (App, Memcache, Disk)  https://github.com/netherphp/cache

Slide 51

Slide 51 text

This example uses the Nether Cache module to handle checking, populating, and invalidating of cache. All the third parties more or less work the same, just with different function names and whatnot. Pick the one that fits best in your framework, (e.g. already using Symfony2, use illuminate/cache)

Slide 52

Slide 52 text

Shared hosting and on disk cache?  Encrypt cache before store.  Only store to a private directory that only the required applications have access to read.  Don’t cache in the public web directory. Memcached, MongoDB, Redis, or any other network accessed cache?  Cache servers should only be accessible by the private network only.  Firewalling, etc.  Encrypt if you want, technically not necessary if protected isolated network.  Obviously, u/p for servers that support auth.

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Bare Bones DB Slave Pretty Decent Memcache Node

Slide 56

Slide 56 text

Bob Majdak Jr • @bobmajdakjr • [email protected] • These Slides: • https://speakerdeck.com/bobm ajdakjr/dynamic-data-cache-v2 • Complete and commented version of the API we built in these slides. • https://github.com/catch404/d allasphp-201405 • (see cache/lib/cache.php)