Reputation: 657
I have a caching scenario which I'm not sure how to solve. I am able to cache static pages without a problem but am having a little trouble with dynamic pages.
In the dynamic page the logic goes something like this:
//part1
-some processing for the beg of the page (needed for part 3)
-echo some info
//part2
echo user specific menu + some user specific info
//part3
echo rest of the page
The problem is that if I cache the whole page, and then the cache is read, it is no good ( part2 will be incorrect since it depends on the user).
to cache a page, I just use
"ob_start();" at the beginning of the file, then save ob_get_contents() to a file and then "ob_end_flush();" at the end.
If the file already exists in cache and its not expired, i do: "include(file);" and "exit;"
I tried splitting the caching into two files but parts 2 and 3 depend on some php processing of part1 I'm having a hard time...
Does anybody have any ideas on how to solve this? Please let me know if I'm not being very clear and I will reformulate the question. Thank you!
Upvotes: 1
Views: 242
Reputation: 61793
If you could you should use APC(and/or Redis/Memcached) to cache your data because it is going to be WAAAAAAAAAAAAAAAAY faster(everything stays in MEMORY). If you want your website to perform good you should have APC(precompiled byte-code in MEMORY) installed anyway.
Else you should use the excellent Cache_Lite library to cache your data to a file. The introduction explains perfectly how to use it. If it all possible you should write to /dev/shm/
because this maps to MEMORY which is going to be way faster then Disc IO.
I tried splitting the caching into two files but parts 2 and 3 depend on some php processing of part1 I'm having a hard time...
You should have unique pieces(exclusively as large as possible would be best, but sometimes hard to keep track off) and then combine these pieces you need on that page. Each piece should have an unique ID off-course(for retrieval).
<?php
require_once('Cache/Lite.php');
$options = array(
'cacheDir' => '/tmp/',
'lifeTime' => 3600
);
// Create a Cache_Lite object
$Cache_Lite = new Cache_Lite($options);
if ($data = $Cache_Lite->get('#{ID for user specific menu}')) {
echo($data);
} else {
$data = 'Data of the block 1-1';
$Cache_Lite->save($data);
}
echo('<br><br>Non cached line !<br><br>');
if ($data = $Cache_Lite->get('#{user specific info}')) {
echo($data);
} else {
$data = 'Data of the block 2';
$Cache_Lite->save($data);
}
?>
Upvotes: 1
Reputation: 3461
If the reason behind caching the "user specific menu" and the "user specific info" is to prevent querying a database, you could very well cache the resulting query instead of the resulting HTML.
You also mentioned that you are caching into a file. You might want to look into a cache server such as memcached. This will reduce the load on the filesystem.
Unless you are doing intense processing in the PHP code or if you are performing a query that is taking time, I don't see the need to cache the HTML outputted by your PHP script.
Upvotes: 0