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.
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.
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.
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!
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’ } );
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.)
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“ > }
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
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.
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)
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.
• 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)