Where to put the memcache in a ZF project?
-
26-06-2021 - |
Question
I am trying to adopt memcached in my project which is built under ZendFrame.
Firstly I create a Memcached class in the library(it seems a bit unecessary as it pretty much just encapsulates the functions what memcache offers)
Then I am wondering where exactly should I use it?
Controllers or the Mappers?
I can see both ways have its point, what's the conventional way for doing this.
Thanks, guys.
Solution
Under bootstrap.php you need to define the configuration using _initCache() function which returns void.
Define frontEnd driver, backend driver (where u will physically save data) and load the factory!
There are 2 types of caching, server and compression.
If you choose server, you need to define external extension (memcached is an extension)
A snippet which I would recommend is:
function _initCache() {
$frontendDriver = 'Core';
$frontendOptions = array(
'lifetime' => 7200, // cache lifetime of 2 hours
'automatic_serialization' => true
);
$backendDriver = extension_loaded('memcache') ? 'Memcached' : 'File';
$backendOptions = array();
// getting a Zend_Cache_Core object
$cache = Zend_Cache::factory($frontendDriver, $backendDriver, $frontendOptions, $backendOptions);
Zend_Registry::set('Zend_Cache', $cache);
}
Getting data from Cache:
$date = $cache->load($cacheKey);
Saving data into cache:
$cache->save($data, $cacheKey);
Where can you use it?
ANYWHERE in your application!
Go through this good article.
Any Questions? :)