Caching whole html output with zend - zend-framework

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.

Related

Zend_Cache_Core showing when I am trying to create cache file in view file

I am trying to cache a view file content and I am using the following method inside a view file,
$frontendOptions = array(
'lifetime' => 3600,
'automatic_serialization' => false
);
$backendOptions = array(
'cache_dir' => '../cache'
);
$cache = Zend_Cache::factory(
'Core',
'File',
$frontendOptions,
$backendOptions
);
$cache->save('somebody', 'fileName', array('tag1', tag2));
it is not creating cache file and it is showing the error, the same is working fine in controller.
Any help will be appreciated.
Thanks,
Suresh
Replace
$cache->save('somebody', 'fileName', array('tag1', tag2));
by
$cache->save('somebody', 'fileName', array('tag1', 'tag2'));
and
'cache_dir' => '../cache'
by
'cache_dir' => APPLICATION_PATH . '/../caches'
with the cache directory at the same level as the application directory
K. V. Suresh you have to send the the Cache Object created by $cache->load(key) to your view's controller in order to access it there.
From your controller:
$data = $cache->load('somebody');
$this->view->cached_data = $data;
From view:
Zend_Debug::dump($this->cache_data);

drupal form api file upload

I have a form with file attachment. Once i load the file and submit the form, if validation errors occur the form is loaded again, but the file i had uploaded is not rendered and I have to load it again.
I tried using file_save_upload but it doesnot seem to work.
$file_attach_set= file_save_upload('file_attachment1', array());
//$file_attach_setII = $form_state['values']['attc'];
$contextid = 150;
if(empty($file_attach_setII)){
$form['file_attachment' . $i] = array(
'#type' => 'file',
"#title" =>'kik'
'#default_value'=> $file_attach_set->fid,
//'#title_display' => $file_attach_set->uri,
''
);
}
The file element doesn't have a #default_value property. Try using the managed_file type which has this property.
https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#file
$form['file_attachment' . $i] = array(
'#type' => 'managed_file',
'#title' =>'kik',
'#default_value'=> array(
'fid' => $file_attach_set->fid,
),
);

Zend_Cache Clean Method

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.

Zend Framework Cache creates new file and doesnt load it

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.

Zend_Cache_Frontend_Page does not recognize output without echo

I am trying to fiddle around with Zend_Cache, so I added following code to my action (will be moved to bootstrap later, I guess):
$frontendOptions = array(
'lifetime' => 7200,
'debug_header' => true, // für das Debuggen
'default_options' => array(
'cache' => true,
'cache_with_get_variables' => true,
'cache_with_session_variables' => true,
'cache_with_cookie_variables' => true,
'cache_with_post_variables' => true,
)
);
$backendOptions = array(
'cache_dir' => '/tmp/'
);
$cache = Zend_Cache::factory('Page', 'File',
$frontendOptions, $backendOptions
);
echo "hej";
var_dump($cache->start('someid'));
Zend generates a cache file containing hejbool(false) now, but apart from that it does not cache my page. According to a German book about zend framework, false is correct when there is no cache available. true is only returned when a cache was found.
When I debugged within Zend_Cache_Frontend_Page.php directly, it went down to the bottom of the start()-method, meaning that nothing went wrong (id given) and no cache was found, so one had to be generated. This was done (I can see it in /tmp/), but without the needed content.
So why does not not cache the output from Zend_View, but only direct output via echo?
I do not call any explicit function to render the view, but this did not seem necessary anymore (my views are always rendered automatically according to controller and action). I tried it for both a standard XHTML template (index.phtml) and an RSS template (index.rss.phtml).
Any ideas? Do you need any other code fragments?
When using the Zend_Cache_Frontend_Page you have to enable the disableOutputBuffering option. The reason is that Zend_Cache_Frontend_Page uses ob_start with a callback and it has to be the first call to ob_start otherwise it leads to that strange behaviour you've encountered.
To enable it you can either set it in your Bootstrap with
Zend_Controller_Front::getInstance()->setParam('disableOutputBuffering', true);
or using the configuration file after your frontController-setup (here in the INI-style configuration):
resources.frontController.params.disableOutputBuffering = true