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.
Related
i'm having a little problem with Zend Framework Full Page Cache.
My current Bootstrap configuration looks like this:
$dir = PUBLIC_PATH . "/tmp/";
$frontendOptions = array(
'lifetime' => 6000000000,
'content_type_memorization' => true,
'default_options' => array(
'cache_with_get_variables' => true,
'cache_with_post_variables' => true,
'cache_with_session_variables' => true,
'cache_with_cookie_variables' => true,
),
'regexps' => array(
'^/.*' => array('cache' => true),
)
);
$backendOptions = array(
'cache_dir' => $dir
);
// getting a Zend_Cache_Frontend_Page object
$cache = Zend_Cache::factory('Page',
'File',
$frontendOptions,
$backendOptions);
$cache->start();
which worked perfectly before changing our development system to the live one.
Now if we enable the caching system it creates the correct cached file in the correct path but doens't load it.
So for every request another cache file is created but the old one NEVER gets loaded.
Maybe anyone has had this problems before and can give me a hint?
Thanks in advance!
there might be issue with permission to move from development environment to live.
The tmp directory is writable by myself and other users of the same group and also apparently Zend will access the files as another user. The solution was to chmod 777 on the folder, making it writable.
let me know if i can help you more.
I am trying to cache everything that is output by layout script and controller's action script using Zend_Cache but it is not working correctly. All I get is
DEBUG HEADER : This is a cached page !
I got layout.phtml script and index.phtml script. Both produce html code.
In my IndexController i put
$frontendOptions = array(
'lifetime' => 7,
'debug_header' => true,
'regexps' => array(
'^/$' => array('cache' => true),
'^/index/' => array('cache' => true)
)
);
$backendOptions = array('cache_dir' => '../application/cache/');
$cache = Zend_Cache::factory('Page', 'File', $frontendOptions, $backendOptions);
if(!$cache->start('mypage')) {
}
How to get it working? I expect that html code should be saved in cache folder.
check this tutorial out Brandon Savage on Zend Cache, it's short and I think it's more current then the ZF docs.
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.
Ok, here's the problem:
$frontendOptions = array(
'lifetime' => 7200,
'debug_header' => true, // for debugging, but it doesn't work...
'regexps' => array(
// Cache the static pages
'^/pages/' => array('cache' => true),
)
);
$backendOptions = $config->cache->backOptions->toArray();
// getting a Zend_Cache_Frontend_Page object
require_once 'Zend/Cache.php';
$cache = Zend_Cache::factory('Page',
$config->cache->backend,
$frontendOptions,
$backendOptions);
$cache->start();
This doesn't do anything at all. Page loading times are exactly the same and the folder indicated in $backendOptions is empty. What am I doing wrong?
By the way: $config->cache->backend reads "file".
Well, following my tradition of answering my own questions, here comes the answer, and a subquestion, if anyone knows what's going on:
Basically, this thing doesn't work out of the box if you happen to run something more advanced than Hello World. I had a cookie set and since it found a cookie, it refused to do anything about it, so one hour digging in the caching code I discovered that the magic needed was simply to set
'cache_with_cookie_variables' => true,
And well, since all cookies are more or less unique and I don't really want to care about them, I set
'make_id_with_cookie_variables' => false
So now it works flawlessly.
Thanks to Chris and smoove for taking the time out and now in hindsight your comments made a lot of sense. Naturally though, I didn't have any errors or warnings and "File" was indeed spelled with uppercase.
What I wonder now is if I can send a spike to delete the proper cache file upon certain circumstances. I can hammer it (copy the ID generator in the cache and unset() the proper target), but there might be a fancier solution. If you have any idea, let me know.
Please go to config/application.ini and set:
resources.frontController.params.disableOutputBuffering = true
If you are done with config/application.ini , just copy and past the code below and have fun.
Please do remember the temporary file ; I have used here servercache you can use temp or tmp or whatsoever.
$frontendOptions = array(
'lifetime' => 900,
'automatic_serialization' => true,
'default_options' => array(
'cache_with_get_variables' => true,
'cache_with_post_variables' => true,
'cache_with_session_variables' => true,
'cache_with_files_variables' => true,
'cache_with_cookie_variables' => true,
'make_id_with_get_variables' => true,
'make_id_with_post_variables' => true,
'make_id_with_session_variables' => true,
'make_id_with_files_variables' => true,
'make_id_with_cookie_variables' => true,
'cache'=>true
),
);
$backendOptions = array(
'cache_dir' => APPLICATION_PATH . '/servercache/'
);
$cache = Zend_Cache::factory('Page', 'File', $frontendOptions, $backendOptions);
$cache->start();