Caching page by parts; how to pass variables calculated in cached parts into never-cached parts?
Posted
by Kirzilla
on Stack Overflow
See other posts from Stack Overflow
or by Kirzilla
Published on 2010-04-01T21:23:41Z
Indexed on
2010/04/01
21:33 UTC
Read the original article
Hit count: 326
Hello,
Let's imagine that I have a code like this...
if (!$data = $cache->load("part1_cache_id")) {
$item_id = $model->getItemId();
ob_start();
echo 'Here is the cached item id: '.$item_id;
$data = ob_get_contents();
ob_end_clean();
$cache->save($data, "part1_cache_id");
}
echo $data;
echo never_cache_function($item_id);
if (!$data_2 = $cache->load("part2_cache_id")) {
ob_start();
echo 'Here is the another cached part of the page...';
$data_2 = ob_get_contents();
ob_end_clean();
$cache->save("part2_cache_id");
}
echo $data_2;
As far as you can see I need to pass $item_id variable into never_cache_function, but if fist part is cached (part1_cache_id) then I have no way to get $item_id value. I see the only solution - serialize all data from fist part (including $item_id value); then cache serialized string and unserialize it everytime when script is executed...
Something like this...
if (!$data = $cache->load("part1_cache_id")) {
$item_id = $model->getItemId();
$data['item_id'] = $item_id;
ob_start();
echo 'Here is the cached item id: '.$item_id;
$data['html'] = ob_get_contents();
ob_end_clean();
$cache->save( serialize($data), "part1_cache_id" );
}
$data = unserialize($data);
echo $data['html']
echo never_cache_function($data['item_id']);
Is there any other ways for doing such trick? I'm looking for the most high performance solution.
Thank you
UPDATED And another question is - how to implement such caching into controller without separating page into two templates? Is it possible?
PS: Please, do not suggest Smarty, I'm really interested in implementing custom caching.
© Stack Overflow or respective owner