Reputation: 16596
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.
Upvotes: 0
Views: 236
Reputation: 53921
You need to change your caching function to return an object or array. It would always have a data
field that would contain the data and whatever other fields you need ie item_id
.
Array(
data => '<h1>whatever data you were caching before</h1>'
item_id => 32,
cache_date => '2010-03-01 12:32:01'
)
This will serialize/deserialze just fine and you would have access to additional parameters attached to your cached data.
Does this make sense?
Upvotes: 1