PHP Fopen有旧版本的txt文件,是缓存吗?



我使用下面的解析器从生产数据监视器(将其数据存储在每个作业的文本文件中)获取数据,并输出到另一个php程序以在网格中显示数据,它大约每5秒在显示刷新时调用一次。

数据需要是最新的,否则技术人员得到的是垃圾数据。每隔几天,其中一个作业就会卡住并停止更新几个小时,即使文本文件每隔几秒钟更新一次。通常唯一的解决办法是完全重启web服务器。

web服务器(运行IIS10和PHPv7.2的Windows server 2019)和生产监视器(Windows 10 pro)位于同一域中,生产监视器的数据文件夹共享给web服务器用户。

文本文件可以缓存吗?当数据卡住时,txt文件比程序输出的内容早几个小时,所以它一定不是从data文件夹中读取的。

<?php
function getData(){
$scan = scan([Network Share Address For Data File]);
$data = array();
foreach ($scan as $x){  //loops through each snapshot file's path
$parse = parse($x);
$data[$parse['Machine']] = $parse;
}
return $data;
}
function parse($snapshot){   //Will read given text file from path and return array of data
$file = fopen($snapshot, "r") or die("Unable to open!"); //open file in read mode
$data = array();
while(($line = fgets($file))!== false){
if(strchr($line,"[")){  //If line starts with [ then it contains the job name
$line = str_replace(array('[',']',"n"),'',$line);
$data += ['Job'=>$line];
}elseif(strchr($line,"machine = ")){
$line = (int)str_replace(array("machine = Machine ","n"),"",$line);
$data += ['Machine'=>$line];
}elseif(strchr($line,"status = ")){
$line = trim(str_replace(array("status = ","n"),"",$line));
$data += ['Status'=>$line];
}
}
fclose($file);
return $data;
}

function scan($scanPath){ //Searches host folder for snapshot files and adds thier path to the return array
$parseList = array();
foreach (ScanDir($scanPath) as $jobDir){
if (is_Dir($scanPath ."\". $jobDir) && in_array("Snapshot.txt", scandir($scanPath ."\". $jobDir))){
array_push($parseList,$scanPath."\".$jobDir."\"."Snapshot.txt");
}
}
return $parseList;
}
?>

我从来没有听说过fopen中的缓存机制-但我从自己的经验中知道,窗口在同时打开文件时可能非常棘手,可能写入过程阻塞了读取过程。您可以通过将写入的文件标记为已关闭(可能通过一些时间戳)来解决这个问题。

最新更新