3

I have the following action:

public function viewImageAction()
{
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender();

    $filename = sanitize_filename($this->_request->getParam('file'), 'jpg');
    $data = file_get_contents(APPLICATION_PATH . '/../private-files/fans-pictures/' . $filename);
    $this->getResponse()
         ->setHeader('Content-type', 'image/jpeg')
         ->setBody($data);
}

And in my index.php before the application start I have:

/** Zend Cache to avoid unecessary application load **/
require_once 'Zend/Cache.php';

$frontendOptions = array(
'lifetime' => 3600,
'default_options' => array(
    'cache' => $cache_flag,
    'cache_with_cookie_variables' => true,
    'make_id_with_cookie_variables' => false),
'regexps' => array(
    '^(/.+)?/admin/?' => array('cache' => false),
    '^(/.+)?/pictures/view-image/?' => array('cache' => true),
    '^(/.+)?/authentication/?' => array('cache' => false),
    '^(/.+)?/fan-profile/?' => array('cache' => false),
    '^(/.+)?/fan-registration/?' => array('cache' => false))
);

$backendOptions = array(
'cache_dir' => APPLICATION_PATH . '/cache/pages/');

$cache = Zend_Cache::factory(
  'Page', 'File', $frontendOptions, $backendOptions
);

$cache->start();

The cache works fine, except that if I try to access the url, like public/admin/pictures/view-image/file/63.jpg the headers come with text/html not image/jpeg.

Am I doing something wrong?

EDITED

I've tried:

'memorize_headers' => array('Content-type')

But nothing...

Also, I've notice that this type of caching (before the application start) can't be done on admin areas because the application need to run and check the session. So I need to put the chache as soon as possible to avoid the load of all components involved.

Any tips?

Keyne Viana
  • 6,194
  • 2
  • 24
  • 55
  • can you check one of the saved cache files and see if it's actually storing the headers? You should be able to find a serialized item called 'headers' `"...;s:7:"headers";a:0:{}}...` that is an example of one without headers saved. If it's not saving them, there's another issue. If it is saving them then it's a matter of figuring out why they're not being replayed. – Darryl E. Clarke Nov 25 '10 at 21:43
  • @darryl-e-clarke In the meta cache file I have the following: `a:4:{s:4:"hash";i:1472989423;s:5:"mtime";i:1290795482;s:6:"expire";i:1290799082;s:4:"tags";a:0:{}}` – Keyne Viana Nov 26 '10 at 18:21

1 Answers1

1

SOLUTION

The problem is with the location of the memorize_headers parameter.

I was trying this:

$frontendOptions = array(
'lifetime' => 3600,
'default_options' => array(
    'cache' => $cache_flag,
    'cache_with_cookie_variables' => true,
    'memorize_headers' => array('Content-Type', 'Content-Encoding'),
    'make_id_with_cookie_variables' => false),
'regexps' => array(
    '^(/.+)?/admin/?' => array('cache' => false),
    '^(/.+)?/admin/pictures/view-image/?' => array('cache' => true),
    '^(/.+)?/authentication/?' => array('cache' => false),
    '^(/.+)?/fan-profile/?' => array('cache' => false),
    '^(/.+)?/fan-registration/?' => array('cache' => false))
);

The right location of this is out of the default_options key:

$frontendOptions = array(
'lifetime' => 3600,
'memorize_headers' => array('Content-Type', 'Content-Encoding'),
'default_options' => array(
    'cache' => $cache_flag,
    'cache_with_cookie_variables' => true,
    //'cache_with_session_variables' => true,
    'make_id_with_cookie_variables' => false),
'regexps' => array(
    '^(/.+)?/admin/?' => array('cache' => false),
    '^(/.+)?/admin/pictures/view-image/?' => array('cache' => true),
    '^(/.+)?/authentication/?' => array('cache' => false),
    '^(/.+)?/fan-profile/?' => array('cache' => false),
    '^(/.+)?/fan-registration/?' => array('cache' => false))
);

Now it works.

Keyne Viana
  • 6,194
  • 2
  • 24
  • 55