Clearing cache using Zend Framework event manager -
i try implement cache invalidation in zf1. idea add listener cache class, trigger invalidation in case of 'invalidate' event.
from learn in manual correct way add event manager every class can possibly trigger cache invalidation , attach cache class listener it. seems awful lot of work such simple task.
it possible write works sth. like: * class can trigger 'invalidate' event * whenever invalidate event triggered specified callback cachemanager class invoked , clears cache
there did, hope help:
class application_service_client extends application_service_abstractservice { protected $cacheformid = 'clientlist'; protected $index = 'client'; public function __construct() { parent::__construct(new application_model_dbtable_client()); $this->events->attach('insert', function ($e) { $event = $e->getname(); $target = get_class($e->gettarget()); $this->cleancache('clientlist'); }); } public function ajouterclient($data) { unset($data['csrfhash']); $id = $this->model->ajouter($data); $client = $this->findclient($id); $this->events()->trigger('insert', $this, $client); return $client; } public function formlist() { mb_internal_encoding("utf-8"); $cache = $this->getcache(); $cacheid = $this->cacheformid; if (!($tocache = $cache->load($cacheid))) { $itemall = $this->findall(); foreach ($itemall $item) { $key[] = $item['idclient']; $value[] = $item['nom'] . ' ' . $item['prenom']; } $tocache = array_combine($key, $value); $cache->save($tocache, $cacheid); } return $tocache; } } and abstratct class:
abstract class application_service_abstractservice { protected $model; protected $cache; protected $events; public function __construct($model = null) { if (!is_null($model)) { $this->setmodel($model); } $this->events(); $this->setcache(); } public function events(zend_eventmanager_eventcollection $events = null) { if (null !== $events) { $this->events = $events; } elseif (null === $this->events) { $this->events = new zend_eventmanager_eventmanager(__class__); } return $this->events; } public function setcache($cachename = 'form') { $bootstrap = \zend_controller_front::getinstance()->getparam('bootstrap'); $cachemanager = $bootstrap->getresource('cachemanager'); $this->cache = $cachemanager->getcache($cachename); } /** * cache object cache manager * * @return zend_cache */ protected function getcache() { if (!$this->cache) { $this->setcache(); } return $this->cache; } protected function cleancache($cacheid) { $this->cache->remove($cacheid); } }
Comments
Post a Comment