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()

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

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图