views:

30

answers:

1

hi, i have a site with a few db queries, and lots of visitors, so i thought id cache it.

this is in php, so i use ob_start() etc to get the contents and save the file. this is fast.

it takes 0.05 secs. (tbh i don't even need to cache).

the problem is with loading the file.

if i do this:

 readfile($cache_file)

it takes 0.43 secs.

if i do

$c= fread(fopen($cache_file,'r',filesize($cache_file)) 

(ie read the file, dont output it) its faster than 0.05.

if i then do

echo $c

it takes 0.4 secs again.

any ideas how to speed this up? it seems that basically echoing (or however readfile does it) the full cache in one go takes longer than just generating the page on the fly.

ps the file size of the cache file is approx 41 kilobytes. i've fully tested to make sure it is the loading tof the large file that is the problem. doing a readfile($smallfile); is fast. its only slow when outputing the cache file when the cache file is large

edit - also i am using this script on another site, with a much smaller cache file (pretty basic page) and it deff speeds it up. the large cache file is required because it has a lot of data on the page, so i cant get it down from 40kb.

+1  A: 

Why don't you just serve up the cached page directly instead of passing it through PHP? Save your cached file as [request_name]_cache.html then use mod_rewrite to serve the cached HTML directly if it exists:

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI}_cache.html -f
RewriteRule ^(.*)$ $1_cache.html

If you're already leveraging mod_rewrite directly you might need to adjust this.

Also if you change the data in the db, don't forget to delete the cached page.

rojoca