This article will show you how to write / use your own Zend Cache Backend Adapter along side provide you with the Class that will allow you to store your cached data, objects and even full html pages inside a MySql database. You can read about how the class can be used, how it works (theory of operation) or you can just download it from the end of the article.
Why Zend_Cache & Mysql
If you haven’t already started using Zend_Cache, you should definitely try it out. There is no need to explain the benefits of using it but these include an increased page load speed since the requests are balanced for all page views. If you wish to find out more about Zend_Cache, you can go to the manual and read more there since the Zend team provides excellent documentation.
However, sometimes, the backends (File, SQLite, memcache, apc) provided by Zend are not enough to satisfy our needs.
In my case, I wanted to use Zend_Cache, but did not have at my disposal any type of backend that the Cache object supported. So I decided to write my own.
Thus, I have created the adapter MyApp_Cache_Backend_PDOMysql which is a backend for Zend_Cache. It does exactly what SQLite does and has some improvements over it: it is highly customizable via the constructor. You are not required to have a certain table called somehow you do not like and your table structure can be however you wanted it to be, keeping in mind you need the following in order for it to work:
- A text column where the serialized cache will be stored
- A 32 char column where the signature of the cache will be stored
- An integer column, unsigned, where the expiry time stamp will be held
Theory of Operation
The Cache object, when initialized and started, checks to see if the current request signature (page load, method load, function load or any other) exists in the backend, in our case if it exists in the cache table. If it does exist and it is not expired, it fetches & deserializeses the data column of that row and the requested method / page now holds this data, instead of querying / fetching the data from the server again.
If the signature does not exist in the Database or it has expired, it simply deletes it, waits for the method / page to load, serializes the response and saves it in the cache for later use.
The PDOMysql adapter only sets up the option it needs to have in order to load and save cached data from the Mysql database.
Using the PDOMysql adapter
Once you have the database table setup, you can start using it as following:
a) Put the class file inside your Application/library/path/MyApp/Cache/Backend/PDOMysql.php file (or change the class name to reflect your directory structure)
b) Put the following code in your initCache(){ … } method from your bootstrap file:
//...
//Front End options go here, see documentation for more information about this
//Example FrontEnd Option:
$frontend= array(
'lifetime' => 3600, //nb seconds to hold the cache
'automatic_serialization' => true
);
$backend = new MyApp_Cache_Backend_PDOMysql(
array(
'table_name' => 'My_Cache_Table_Name',
'signature_column' => 'My_Signature_Column',
'data_column' => 'My_Data_Column',
'expiry_column' => 'My_Expiry_Column'
)
);
$cache = Zend_Cache::factory(
'core',
$backend,
$frontend,
array()
);
Zend_Registry::set('cache',$cache);
//Other code you may have
//...
Note: i have added all the options to the constructor even though you may not pass them at all since you can just use the defaults (cache/signature/expiry/data)
Once you have these things done, you can just get the cache object from the registry wherever you want, before running a method which you want to get it from cache, (ex: $cache = Zend_Registry::get(‘cache’); ) and then just run
- $cache->load( md5($method.serialize($params)) ); – to test & load a cached data object
- $cache->save($data, md5($method.serialize($params)) ); – to save a cached data object
Download & Conclusion
You can download the class file here .
This Class does only what it was meant to do (what I need it to do) so if you have any ideas of how to improve it or perhaps just thoughts/suggestions of what you might want it to do in order to help you, please comment below and I will do my best to update the package so that everybody can benefit out of it.
