Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dynamic Data Cache (v2)

Dynamic Data Cache (v2)

This is a highly revised version of the presentation I gave at Lone Star PHP 2014. The API we create is much nicer to use in the end. Also dropped Diskcache to provide more time to talk about stackable caches instead.

Bob Majdak Jr

May 12, 2014
Tweet

More Decks by Bob Majdak Jr

Other Decks in Programming

Transcript

  1. 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.
  2. 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.
  3. if cache exists return cached object else query database fetch

    result build object from data cache object return object
  4. 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!
  5. 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’ } );
  6. 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.)
  7. - Data / Connection Management. Single point of entry for

    multiple end points. - Store Data. - Retrieve Data. - Invalidate / Delete Data. - Bonus: Invalidate / Delete ALL THE THINGS.
  8. 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“ > }
  9. <application> get_user(1); <get_user($id)> checking cache… <appcache> i no has. <memcache>

    i no has. <get_user($id)> checking database… <database> lol, fools. i has it. <appcache> ok now I has it. <memcache> ok now i has it. <get_user($id)> here is user with id 1
  10. <application> get_user(1); <get_user($id)> checking cache… <appcache> i no has. <memcache>

    oh oh i has! here it is. <appcache> now i has it. <get_user($id)> here is user with id 1
  11. 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.
  12. 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.
  13. 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
  14. 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)
  15. 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.
  16. 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)