PHP - 以零数据库调用加载页面的方法

标签 php mysql websocket load

我正在处理一个有超过 100 个数据库调用的页面,加载该网页需要花费太多时间。

我正在考虑加载一个数据库调用为零的页面。我有以下几种方法-

  1. 使用每 5 分钟频繁运行的 cron 作业创建我的页面的 HTML 副本,然后使用 file_get_contents 加载它。

  2. 创建一个包含数据库结果值的 txt 文件,然后将其加载到页面中。

  3. 使用 websockets - 当有新数据时,它会更新页面。

我需要你们的专家意见:)

最佳答案

如果您使用的是 CodeIgniter 之类的框架,则可以管理页面级别以及数据库查询级别的缓存。查询缓存会过期,因此无需担心更新数据。

对于普通 php,我过去常常将整个输出写入某个文件,然后检查日期时间以提供或更新相同的文件。

在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();  ?>

最后!

关于PHP - 以零数据库调用加载页面的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26750844/

相关文章:

python-3.x - 如何从 faust 应用程序向 Websocket 发送数据

mysql - 如何将行从一个表移动到另一个表中不存在的行?

php - extjs 4 : filling model data into combobox from PHP + MySql

javascript - 选择一个选项时不发送文本框值

php - 关于行数的 mysql_result 的语法问题

mysql - 使用 Node 和 MySQL/MariaDB 构建可扩展的 API

php - PHP 网页的英语到阿拉伯语翻译

javascript - websockets 和数据库更新(推送更改)

java - 实时搜索结果

Javascript 以前可以工作,但现在不行了