Extensible PHP Caching Library
by Justin Leider on April 23, 2010
Everyone has probably already seen every caching class there ever was and ever will be. However, when I was searching for a class that could easily be switched from one data store to the next I couldn't find a thing. Every caching class I seemed to come by was written specifically for a single back end and with their predefined static keys/column names. Well, those silly restrictions have come to an end!
I present to you the Extensible PHP Caching Library (Hosted at GitHub) - This collection of classes makes it easy to customize key or column names to your needs as well as switch from one data store to another. This extensibility is baked into the core of the library via an abstract class. This abstract Cache class takes an associative array of keys/columns and determines how to use those keys based on the type of cache back end you are using. For example: Suppose you are using a RDBMS such as MySQL. In this case the associative array will be parsed and the query built such that the key is the column name and the value is what you want to query on. However, if you chose to use a NoSQL key/value store such as Memcache then the associative array is sorted and imploded to create a single string as the key.
Since we always specify the key as an associative array, switching between different data stores is as simple as changing the Class name from say MCache to SQLCache in your code. Nothing else is required to change data stores. Keys, expiration dates, and data processing all stay the same and all functions are called with the same arguments. This functionality is attributed to the abstract base Cache class which guides and regulates the inheriting classes.
Lets get to some examples:
In the following example we create a new SQLCache object and pass it our associative array of keys and values. Check if there is cached data, if there isnt then do something to generate the data and then cache it. Notice how you dont have to pass the key in again when setting the cache. The key is stored within the object so you can get and set as many times as you need without having to set the key every time. Lastly we delete the cache.
// Get Cache $cache = new SQLCache(array('column1' => 123, 'column2' => 'blah', … )); $output = $cache->getCache(); if(!$output) { // Do something to generate data $output = 'some datas'; // Set Cache if($cacheNow) { // Force a write to cache now $cache->setCache($output, '+1 day', true); } else { // Setting no time defaults to time() + 86400, one day from now. $cache->setCache($output); } } print $output; // Delete the cache $cache->deleteCache();
If ever you needed to update this caching strategy to include Memcached the only change would be to change SQLCache to Mcache:
$cache = new SQLCache(array('column1' => 123, 'column2' => 'blah', … )); to $cache = new MCache(array('key1' => 123, 'key2' => 'blah', … ));
The column and key names are arbitrary and may be set to anything you want to name it. For SQL caches make sure you create a cache table that has the corresponding column names and that they are indexed optimally.
While I have only created classes for SQL (MySQL - since there is a LIMIT 1), Memcached and File based caching, the base class can be extended to include any key/value store or any database with columns (MongoDB, CouchDB, Tokyo, Postgress, Oracle, etc). Just update the back end calls and you are good to go.
For anyone who updates or adds functionality please let me know so I can give credit where credit is due. This library is available under the LGPLv3.
10 comments
Hi Justin,
I ran across your blog, and I really enjoyed reading it! There was some notable content that we would love to re-post on DZone.com. Would you be interested in joining our MVB program (most valuable bloggers)? Our MVBs are a group of high-quality bloggers whose content is re-posted on DZone.com. Links lead traffic back to their blogs, and most experience a substantial increase in overall traffic thanks to joining our program. Essentially, it allows developers like you to gain more visibility among our large, diverse community.
Contact me if this is something that interests you.
-Katie McKinsey-
DZone Content Curator
katie@dzone.com
by Katie McKinsey on February 24, 2011 at 7:05 pm. #
[...] want to subscribe to the RSS feed for updates on this topic.Powered by WP Greet Box WordPress PluginJustin Leider created the Extensible PHP Caching Library with the intent of enabling easy customizing of static keys/column names to suit your requirements [...]
by An Extensible PHP Caching Library | php Snake Portfolio on April 25, 2010 at 6:34 pm. #
Social comments and analytics for this post…
This post was mentioned on Twitter by cvanschalkwijk: Extensible PHP Caching Library (http://cli.gs/Ursev) #php #rtw…
by uberVU - social comments on April 25, 2010 at 11:55 am. #
[...] This post was mentioned on Twitter by Derivante. Derivante said: Extensible PHP Caching Library (http://cli.gs/Ursev) #php #rtw [...]
by Tweets that mention Derivante - Extensible PHP Caching Library -- Topsy.com on April 25, 2010 at 6:56 am. #
[...] Przeczytaj artykuł: Derivante – Extensible PHP Caching Library [...]
by Derivante – Extensible PHP Caching Library « cache on April 23, 2010 at 10:50 pm. #
I was hoping for something like Zend_Cache but better.
I want to be able to cache to Memcache and to disk. If memcache doesn’t have it, pull from file, if not, re-populate both.
by EllisGL on April 23, 2010 at 7:37 pm. #
Unless the data you are caching is extremely expensive to generate then I would imagine 2 potential cache misses would be slower than just generating the data in the first place.
Of course you could always create 2 cache objects and check one after the other.
$fc = new FileCache(array(params..), basedir);
$data = $fc->getCache();
if(!$data) {
$mc = new MCache(array(params..));
$data = $mc->getCache();
if(!$data) {
$data = generate_data();
$fc->setCache($data);
$mc->setCache($data);
}
}
That code snippet would check both your caches and then generate the data and cache it if it wasn’t found or had expired.
by Justin Leider on April 26, 2010 at 5:22 pm. #
[...] posted here: Derivante – Extensible PHP Caching Library [...]
by Webby Scripts Derivante – Extensible PHP Caching Library on April 23, 2010 at 6:05 pm. #
[...] Adres URL: Derivante – Extensible PHP Caching Library [...]
by Derivante – Extensible PHP Caching Library « php on April 23, 2010 at 5:50 pm. #
this caching library changed my life.
by red on April 23, 2010 at 4:48 pm. #