我在文件中的查询缓存方面遇到问题。我有一个代码正在尝试获取具有序列化查询结果的文件,如果找不到文件,或更旧的一天,那么我正尝试连接到DB并远程获取它。代码如下:
// Getting cache file function
$cur_time = @time();
$time_modified = @filemtime($file_path);
if ($cur_time > ($time_modified + $cache_period_hours))
return NULL;
else
return unserialize(file_get_contents($file_path));
//If NULL is returned get result remotely;
if($result === NULL){
if (!db::connect("base", "192.168.1.111", "root", "password"))
die("Database error. Can not connect to database.");
//Execute query, write to file serialized result
}
当我的流量很大时,会出现以下错误:
Lost connection to MySQL server at 'reading authorization packet', system error: 0.
但是我不知道为什么当我有了新的高速缓存文件时,脚本甚至试图连接数据库。可以同时读取同一文件的用户数量是否有限制?还有什么办法可以防止这个MySQL错误?
最佳答案
改成:
if ($cur_time > ($time_modified + $cache_period_hours*3600))
这会将
$cache_period_hours
转换为秒,以匹配其他变量的单位。