dongque1646 2017-12-28 19:56
浏览 27

在PHP中写缓存

I need to store the content array in cache, using the provided key. Add current unix timestamp as array element ‘created’. Can someone check my code if it is correct or not!

public function writeToCache($key, $content) {
    $this->cacheExtension = new DateTime;

    $file = fopen($this->cacheLocation . $key . $this->cacheExtension->format("y:m:d h:i:s"), "w") or die("Unable to open file!");
    fwrite($file, $content); 
    fclose($file);
}
  • 写回答

1条回答 默认 最新

  • dqwn64004 2017-12-30 18:08
    关注

    I solve the problem. I was dumb!

    public function writeToCache($key, $content) {
    $date = new DateTime();
    $date = $date->format("yy-mm-dd");
    
    $file = fopen($this->cacheLocation . $key . "_created_" . $date, "w") or die("Unable to open file!");
    fwrite($file, json_encode($content)); 
    fclose($file);}
    
    评论

报告相同问题?