douchu5131 2014-02-05 11:23
浏览 55
已采纳

PHP缓存不向文本文件写入任何内容

I try to make my PHP cache with this code:

//Make cache files
$cache = 'tweets-cache.txt';
$date = 'tweets-date.txt';

$currentTime = time(); // Current time


 // Get cache time
$datefile = fopen($date, 'r');
$cacheDate = fgets($datefile);
fclose($datefile);


 //check if cache has expired
if (floor(abs(($currentTime-$cacheDate) / 10800)) <= $_GET['expiry'] && $cacheDate) {
$cachefile = fopen($cache, 'r');
$data = fgets($cachefile);
fclose($cachefile);
} else { 

//Make the REST call
$data = (array) $cb->$api($params);

    // update cache file
    $cachefile = fopen($cache, 'wb');  
    fwrite($cachefile, utf8_encode($data));  
    fclose($cachefile); 
    // update date file
    $datefile = fopen($date, 'wb');  
    fwrite($datefile, utf8_encode(time()));  
    fclose($datefile);      
}

//Output result in JSON, getting it ready for jQuery to process
echo json_encode($data);

Now he writes nothing in tweets-cache.txt. i think it is because of he cannot write an array with utf8_encode.

  • 写回答

1条回答 默认 最新

  • dongshang1979 2014-02-05 20:38
    关注

    Yes, you are right about utf8_encode() function. Try to write serialized data to the file:

    fwrite($cachefile, json_encode($data));
    

    and when you read your data back from the file, do de-serialization:

    $data = json_decode(fgets($cachefile));
    

    Also, you can make things simpler if you use functions like file_get/put_contents, so your code:

    $cachefile = fopen($cache, 'r');
    $data = fgets($cachefile);
    fclose($cachefile);
    

    will be:

    $data = json_decode(file_get_contents($cache));
    

    and:

    // update cache file
    $cachefile = fopen($cache, 'wb');  
    fwrite($cachefile, utf8_encode($data));  
    fclose($cachefile); 
    
    // update date file
    $datefile = fopen($date, 'wb');  
    fwrite($datefile, utf8_encode(time()));  
    fclose($datefile);    
    

    translates to:

    // update cache file
    file_put_contents($cache, json_encode($data));
    
    // update date file  
    file_put_contents($date, time());
    

    Be aware of the synchronization problems you can face when you are using filesystem in multithreaded/multiprocess environment. Two and more php instances can make attempt to write data to the same file in the same time, and you will get corrupted file.

    You can use filesystem locking or special cache software like Memcached, which is much more preferable solution for such cases.

    Memcached

    PHP Reference: file_put_contents()

    PHP Reference: file_get_contents()

    PHP Reference: json_encode()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源