I'm developing a web-site with Zend Framework and I wish to use ZFDebug.
I followed the installation instructions, described here, but I don't initialize Zend_Cache in the bootstrap (define cache manager settings in section of configuration file).
So, my bootstrap section for ZFDebug looks similar to this:
if ($this->hasPluginResource("cachemanager")) {
$this->bootstrap("cachemanager");
$cache = $this->getPluginResource("cachemanager")->getCacheManager()->getCache("default");
$options["plugins"]["Cache"] = array("backend" => $cache->getBackend());
}
$debug = new ZFDebug_Controller_Plugin_Debug($options);
With this code ZFDebug shows 'Cache' item in menu, but it is not clickable. What should I do to make ZFDebug show cache info? I use Xcache as Zend_Cache backend.
I only started playing with ZFDebug this morning but my Boostrap.php init's the cache and registers it first. In the _initZFDebug i call call the registry to get the cache.
protected function _initCache()
{
$frontendOptions = array(
'lifetime' => 3600*24*5, // cache lifetime of 5 days
'automatic_serialization' => true,
'logging' => false,
'caching' => true
);
$backendOptions = array(
'cache_dir' => './../data/cache/', // Directory where to put the cache files
'hashed_directory_level' => 2
);
$flickrFrontendOptions = array(
'lifetime' => 3600*24*5, // cache lifetime of 5 days
'automatic_serialization' => true,
'logging' => false,
'caching' => true
);
$flickrBackendOptions = array(
'cache_dir' => './../data/flickr/', // Directory where to put the cache files
'hashed_directory_level' => 2
);
// getting a Zend_Cache_Core object
$cache = Zend_Cache::factory(
'Core',
'File',
$frontendOptions,
$backendOptions);
Zend_Registry::set('cache', $cache);
$flickrcache = Zend_Cache::factory(
'Core',
'File',
$flickrFrontendOptions,
$flickrBackendOptions);
Zend_Registry::set('flickrcache', $flickrcache);
}
protected function _initZFDebug()
{
if ($this->hasOption('zfdebug'))
{
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('ZFDebug');
$options = array(
'plugins' => array('Variables',
'Database' => array('adapter' => $db),
'File' => array('basePath' => $this->hasOption('zfdebug.basePath')),
'Cache' => array('backend' => Zend_Registry::get('cache')->getBackend()),
'Exception')
);
$debug = new ZFDebug_Controller_Plugin_Debug($options);
$this->bootstrap('frontController');
$frontController = $this->getResource('frontController');
$frontController->registerPlugin($debug);
}
}
Related
I have this code:
'translator' => array(
...
'cache' => array(
'adapter' => array(
'name' => 'Filesystem',
'options' => array(
'cache_dir' => __DIR__ . '/../../../data/cache',
'ttl' => '3600'
)
),
'plugins' => array(
array(
'name' => 'serializer',
'options' => array()
),
'exception_handler' => array(
'throw_exceptions' => true
)
)
)
The question is, how do I invalidate it not by TTL?
For example, I KNOW when the translation was changed so I want to invalidate in on demand but I have not found a way to do it.
The translator component does not utilize the TaggableInterface so you have to know the cacheId which the translator generates to clear the item from you storage adapter. You can use the following code to simply generate the same id and remove the item. Call this from your service or some event listener.
$translator = $sm->get('McvTranslator');
$textDomain = 'default';
$locale = 'en';
$cacheId = 'Zend_I18n_Translator_Messages_' . md5($textDomain . $locale);
$translator->getCache()->removeItem($cacheId);
I think you could set Ttl = 0 (always), and when the cache (file) is not valid anymore -- delete it.
Another way to do it:
Find a point in your code where you call addTranslation.
For example:
$translate = Zend_Registry::get('Zend_Translate');
$translate->addTranslation(array(
'content' => "$dir/$locale.mo",
'locale' => $locale
));
Change the addTranslation function to add reload => true , like this:
$translate->addTranslation(array(
'content' => "$dir/$locale.mo",
'locale' => $locale,
'reload' => true
));
Refresh your page.
Voila.
Remeber to remove reload after that, otherwise you will have no cache.
I just tried to use Zend_Cache in my application, and it worked. The problem now is I am not sure where to put Zend_Cache clean() method in my code.
Here is my code:
// application/Bootstrap.php
protected function _initCache()
{
$dir = "./cache";
$frontendOptions = array(
'lifetime' => 10,
'content_type_memorization' => true,
'default_options' => array(
'cache' => true,
'cache_with_get_variables' => true,
'cache_with_post_variables' => true,
'cache_with_session_variables' => true,
'cache_with_cookie_variables' => true,
),
'regexps' => array(
// cache the whole IndexController
'^/.*' => array('cache' => true),
'^/index/' => array('cache' => true),
// place more controller links here to cache them
)
);
$backendOptions = array(
'cache_dir' =>$dir
);
// getting a Zend_Cache_Frontend_Page object
$cache = Zend_Cache::factory('Page',
'File',
$frontendOptions,
$backendOptions);
$cache->start();
}
Where should I put the $cache->clean(/* something */);?
As you have configured our $frontend array to cache data for 10 seconds, so the data cached will automatically be remove after 10 second.
i would suggest that you can increase your time like
one hour : 3600 or 2 hours like 7200
However if later in your application you want to clean the entire cached data manually, simply write
$cache->clean();
This will clean the entire cached data. However if you want to remove specific data being cached, write
$cache->remove(‘mydata’);
for more use of cache see link
i am not associate with above link it is just for your knowledge
hope this will sure help you.
Im my module.config.php file got following excerpt:
return array(
'service_manager' => array(
'aliases' => array(
'Util\Dao\Factory' => 'modelFactory',
),
'factories' => array(
'modelFactory' => function($sm) {
$dbAdapter = $sm->get('Doctrine\ORM\EntityManager');
return new \Util\Dao\Factory($dbAdapter);
},
)
),
'doctrine' => array(
'driver' => array(
'application_entities' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
__DIR__ . '/../src/Application/Model'
),
),
'orm_default' => array(
'drivers' => array(
'Application\Model' => 'application_entities'
)
),
),
),
How do i put the block "doctrine" in module class?
Well, this is actually fairly simple. Probably your Module class has the method getConfig(). That method usually loads moduleName/config/module.config.php.
So in whatever function you are, simply call the getConfig()-method. This is pure and basic php ;)
//Module class
public function doSomethingAwesome() {
$moduleConfig = $this->getConfig();
$doctrineConfig = isset($moduleConfig['doctrine'])
? $moduleConfig['doctrine']
: array('doctrine-config-not-initialized');
}
However you need to notice that this only includes your Modules config. If you need to gain access to the merged configuration, you'll need to do that in the onBootstrap()-method. Which would be done like this:
//Module class
public function onBootstrap(MvcEvent $mvcEvent)
{
$application = $mvcEvent->getApplication();
$serviceLocator = $application->getServiceLocator();
$mergedConfig = $serviceLocator->get('config');
$doctrineConfig = isset($mergedConfig['doctrine'])
? $mergedConfig['doctrine']
: array('doctrine-config-not-initialized');
}
This works similar if you're attaching to some events...
I am using Zend Cache with page caching but it seems to miss the cache after a period of time. For a while it is OK, but I come back tomorrow and hit the page, it doesn't fetch the contents from the cache. why?
$frontendOptions = array(
'content_type_memorization' => true, // This remembers the headers, needed for images
'lifetime' => NULL, // cache lifetime forever
'automatic_serialization' => true,
'automatic_cleaning_factor' => 0
);
$myPageCache = new Zend_Cache_Frontend_Page(array(
'debug_header' => false,
'automatic_cleaning_factor'=>0,
'content_type_memorization' => true,
'default_options' => array(
'cache' => true,
'cache_with_get_variables' => true,
'cache_with_post_variables' => true,
'cache_with_session_variables' => true,
'cache_with_cookie_variables' => true
)));
$backendOptions = array('cache_dir' => '.' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR);
$cache = Zend_Cache::factory($myPageCache,
'File',
$frontendOptions,
$backendOptions);
$cacheKey = hash('md5', "cache_" . $cachePath); // cachePath is the key I use for the cache
if(!$cache->start($cacheKey)) {
I output html here
$cache->end();
}
Indeed. My read of the static method
Zend_Cache::factory($frontend, $back, $frontendOptions, $backendOptions, ...)
is that the $frontendOptions are used only when you pass a string for the $frontend parameter. When you pass a concrete instance as you are doing, the $frontendOptions are ignored.
If you still want to pass a concrete instance $myPageCache into the factory, then it seems like you need to pass the lifetime parameter (and the others) into the call that creates the instance. Otherwise, you could load up a single $frontendOptions array and use:
$cache = Zend_Cache::factory('Page', 'File', $frontendOptions, $backendOptions);
ahh... opps.
Is it because my 'lifetime' => NULL, is being ignored?
I there a way to make zend_cache treat front end view similar to smarty? I would like to reduce load times and page caching seems the best way todo this.
Also it would need something similar to {nocache}.
Okay so I now have: Bootstrap.php
protected function _initCache() {
$this->bootstrap('locale');
$locale = $this->getResource('locale');
$front = array ( 'lifetime' => 1800,
'automatic_serialization' => false,
'caching' => true,
'cache_id_prefix' => 'ec_',
'debug_header' => true,
'default_options'
=> array ('cache_with_get_variables' => true,
'cache_with_post_variables' => false,
'cache_with_session_variables' => false,
'cache_with_cookie_variables' => false ),
);
$back = array('cache_dir' => '../data/Cache/'.$locale);
$cache = Zend_Cache::factory('Page', 'File', $front, $back);
$cache->start();
Zend_Registry::set('cache', $cache);
return $cache;
}
However, the only time my cache is hit is with code like:
$cache = Zend_Registry::get('cache');
if (!$data = $cache->load('sidebar_'.$module.'_'.$controller)) {
$data['Studio'] = Eurocreme_Studio::load_by_type(array('type' => 'sidebar', 'from' => 0, 'to' => COUNT_HIGH));
$data['Movie'] = Eurocreme_Movie::load_by_type(array('type' => 'sidebar', 'from' => 0, 'to' => 5));
$data['Gallery'] = Eurocreme_Gallery::load_by_type(array('type' => 'sidebar', 'from' => 0, 'to' => 5));
$data['Category'] = Eurocreme_Category::load_tree(0);
$cache->save($data, 'my_view_helper_sidebar_'.$module.'_'.$controller);
}
I was hoping to capture the entire views.
Does anyone have any working examples of how to implement it fully? The docs don't really go in-depth.
You may want to use Zend_Cache_Frontend_Output or Zend_Cache_Frontend_Page. From Zend Framework manual:
Zend_Cache_Frontend_Output is an output-capturing frontend. It utilizes output buffering in PHP to capture everything between its start() and end() methods.
Zend_Cache_Frontend_Page is like Zend_Cache_Frontend_Output but designed for a complete page.
You are probably looking for Zend_Cache_Frontend_Page. Please refer to Zend Cache docs for details.