【问题标题】:Caching options for php script that pulls data from mysql tables从 mysql 表中提取数据的 php 脚本的缓存选项
【发布时间】:2012-11-19 11:34:19
【问题描述】:

我想知道使用什么函数来缓存拉取的数据并在 15-30 分钟后刷新。

这是将数据从 mysql 拉到网站的 php 脚本。

<?php
include('mysql_connection.php');

$c = mysqlConnect();

$locale = $_GET['locale'];
$last_bulletins_id = $_GET['bulletins_id'];

sendQuery ("set character_set_results='utf8'"); 
sendQuery ("set collation_connection='utf8_general_ci'"); 

if(strcmp($locale,"en") != 0)
$locale = "en";
$result = sendQuery("
SELECT bulletins.id, posted_date, message, name
FROM bulletins 
LEFT JOIN players ON (bulletins.player_id = players.user_id)
WHERE bulletins.id > ".$last_bulletins_id." and locale = '".$locale."' 
ORDER BY bulletins.id DESC 
LIMIT 10");
echo '<table width=\"100%\">';
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
    echo '<tr><td width=\"100%\"><b>Date: </b>'.$row[1].'</td></tr>';
    echo '<tr><td width=\"100%\"><b>Author: </b>'.$row[3].'</td></tr>';
    echo '<tr><td width=\"100%\">'.nl2br($row[2]).'</td></tr>';
    echo '<tr><td width=\"100%\"><hr style="height: 2px; border: none; background: #515151;"></td></tr>';
}
echo '</table>';    

mysqlClose($c);

?>

【问题讨论】:

    标签: php html mysql caching


    【解决方案1】:

    您应该将输出的数据写入缓存文件,并检查从上次修改文件和加载缓存文件已经过去了多少时间,或者如果时间大于您设置的限制,则查询数据库并输出新结果。这些功能会派上用场:

    file-put-contents

    filemtime

    touch

    file-exists

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多