An interesting approach for caching models

recently I got a task to implement caching in your models. In discussions with colleagues were born is pretty interesting, in my opinion, the idea that I would like to put on your court.

the implementation of the idea on Zend Framework:
1. All models inherit from our class My_Db_Table_Abstract:
class My_Model_ModuleSettings extends My_Db_Table_Abstract

2. Which in turn inherited from Zend_Db_Table_Abstract:
class My_Db_Table_Abstract extends Zend_Db_Table_Abstract

3. In the base for all models of class My_Db_Table_Abstract described magic method __call():
public function __call($name, $arguments)
{
/** If call a cached method */
if (preg_match('/^cached_(.+)$/', $name, $methodName)&&method_exists($this,$methodName[1])) {
/** Get the cache instance */
$cache = My_Cache::getInstance();
/** Get arguments hash */
$argHash = md5(print_r($arguments, true));
/** Get model class name */
$className = get_class($this);
/** If the method result don't cached */
if (!$result = $cache->load('model_'.$className.'_'.$methodName[1].'_'.$argHash)) {
$result = call_user_method_array($methodName[1], $this, $arguments);
$cache->save($result,
'model_'.$className.'_'.$methodName[1].'_'.$argHash
array('model',$className,$methodName[1]));
}
return $result;
} else {
/** Generate exception */
throw new Exception('Call to undefined method '.$name);
}
}


Now we have the ability to use the methods of the models in two ways:
1. Not caching, just turning to the method:
$result = $this->_life->getAll('Now!!');

2. Caching and write it to a method name prefix "cached_":
$result = $this->_life->cached_getAll('Now!!');


In the second case, referring to a nonexistent method, the method fires the __call(), which checks for a cached result. If the result of the execution of the method is cached — cache is used if no method is called and the result cached.

Some details:
1. To cache result of method execution was different when different parameters of the method I made a hash of parameters:
$argHash = md5(print_r($arguments));
This is perhaps the most ambiguous moment, because I can't really say how this may affect the performance (in our testing, the load increase was observed). You can use a different hashing function(md5(),sha1()..) and different ways of bringing the array of variables to string type (print_r(), var_dump(), implode()).

2. The name of the cache file is model_ИмяКласса_ИмяМетода_ХэшПараметров, excluding the possibility of coincidence.

3. The cache is marked with the tags 'model', 'className' and 'methodName' that allow you to easily manipulate the cleaning. Here's an example of clearing the cache of all methods My_Model_ModuleSettings:
$cache->clean(
Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG
array('My_Model_ModuleSettings')
);


I'd love to hear your comments... What do you see as the disadvantages of this method? Does he have the right to life?
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Monitoring PostgreSQL + php-fpm + nginx + disk using Zabbix

Templates ESKD and GOST 7.32 for Lyx 1.6.x

Customize your Google