在PHP中写缓存

时间:2017-12-28 19:56:35

标签: php caching yii memcached

我需要使用提供的密钥将内容数组存储在缓存中。将当前的unix时间戳添加为数组元素“created”。有人可以检查我的代码是否正确!

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 个答案:

答案 0 :(得分:0)

我解决了这个问题。我笨了!

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);}