我正在处理一个有100多个数据库调用的页面,加载网页需要太多时间。
我正在考虑加载一个没有数据库调用的页面。我脑海中有以下几种方式-
-
使用每5分钟频繁运行一次的cron作业创建页面的HTML副本,然后使用file_get_contents加载它。
-
创建一个包含数据库结果值的txt文件,然后将其加载到页面中。
-
使用websockets-当有新数据时,它会更新页面。
我需要你们的专家意见:)
如果您使用像CodeIgniter这样的框架,您可以管理页面级别以及数据库查询级别的缓存。查询缓存带有过期,因此无需担心更新数据。
对于纯php,我曾经将整个输出写入某个文件,然后检查datetime以提供或更新相同的输出。
在php文件的开头,
<?php
$cachefile = "some-unique-name.html";
$cachetime = 864000;
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
readfile($cachefile);
exit;
}
// if no file or too old, render and capture HTML page.
ob_start();
session_start();
?>
------ output all your html code here ----
<?php $fp = fopen($cachefile, 'w+');
fwrite($fp, ob_get_contents());
fclose($fp);
// Send browser output
ob_end_flush();
ob_flush(); ?>
最后!
您是否规范了这些数据库中的表?
我也有类似的问题。一个糟糕的表设计可能会干扰你的