开发者

loading the cached file takes longer than not caching - and reading the cache file isn't the problem, its outputting it

开发者 https://www.devze.com 2023-01-17 03:17 出处:网络
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.

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 la开发者_开发知识库rge cache file is required because it has a lot of data on the page, so i cant get it down from 40kb.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消