加载缓存文件比不缓存需要更长的时间 - 读取缓存文件不是问题,它会输出它
我有一个网站,有一些数据库查询和很多访问者,所以我认为 id 缓存它。
这是在 php 中,所以我使用 ob_start() 等来获取内容并保存文件。这很快。
需要 0.05 秒。 (说实话,我什至不需要缓存)。
问题在于加载文件。
如果我这样做:
readfile($cache_file)
需要 0.43 秒。
如果我这样做
$c= fread(fopen($cache_file,'r',filesize($cache_file))
(即读取文件,不输出它),它会比 0.05 快。
如果我这样做,
echo $c
又需要 0.4 秒。
有什么想法可以加快速度吗?看起来基本上一次性回显(或者无论如何 readfile 这样做)完整的缓存比仅动态生成页面需要更长的时间。
ps 缓存文件的文件大小约为 41 KB。我已经进行了全面测试,以确保问题出在大文件的加载上。执行 readfile($smallfile);速度很快。当缓存文件很大时输出缓存文件时它的唯一速度慢
编辑 - 我也在另一个站点上使用这个脚本,具有较小的缓存文件(非常基本的页面)并且它确实加快了速度。需要大的缓存文件,因为页面上有很多数据,所以我无法将其从 40kb 降下来。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不直接提供缓存页面而不是通过 PHP 传递它呢?将缓存文件另存为 [request_name]_cache.html,然后使用 mod_rewrite 直接提供缓存的 HTML(如果存在):
如果您已经直接利用 mod_rewrite,则可能需要对此进行调整。
另外,如果您更改数据库中的数据,请不要忘记删除缓存的页面。
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:
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.