/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached. Other types (If you have disabled [[serializer]]) unable to get is
* correct in [[getValue()]].
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $duration)
{
$this->gc();
$cacheFile = $this->getCacheFile($key);
if ($this->directoryLevel > 0) {
@FileHelper::createDirectory(dirname($cacheFile), $this->dirMode, true);
}
// If ownership differs the touch call will fail, so we try to
// rebuild the file from scratch by deleting it first
// https://github.com/yiisoft/yii2/pull/16120
if (is_file($cacheFile) function_exists('posix_geteuid') fileowner($cacheFile) !== posix_geteuid()) {
@unlink($cacheFile);
}
if (@file_put_contents($cacheFile, $value, LOCK_EX) !== false) {
if ($this->fileMode !== null) {
@chmod($cacheFile, $this->fileMode);
}
if ($duration = 0) {
$duration = 31536000; // 1 year
}
return @touch($cacheFile, $duration + time());
}
$error = error_get_last();
Yii::warning("Unable to write cache file '{$cacheFile}': {$error['message']}", __METHOD__);
return false;
}
在設置緩存之前會主動調(diào)用清理緩存的方法gc()
/**
* Removes expired cache files.
* @param bool $force whether to enforce the garbage collection regardless of [[gcProbability]].
* Defaults to false, meaning the actual deletion happens with the probability as specified by [[gcProbability]].
* @param bool $expiredOnly whether to removed expired cache files only.
* If false, all cache files under [[cachePath]] will be removed.
*/
public function gc($force = false, $expiredOnly = true)
{
if ($force || mt_rand(0, 1000000) $this->gcProbability) {
$this->gcRecursive($this->cachePath, $expiredOnly);
}
}