Final Class Yiisoft\Cache\File\FileCache
| Inheritance | Yiisoft\Cache\File\FileCache |
|---|---|
| Implements | Psr\SimpleCache\CacheInterface |
FileCache implements a cache handler using files.
For each data value being cached, FileCache will store it in a separate file. The cache files are placed
under {@see \Yiisoft\Cache\File\FileCache::$cachePath}. FileCache will perform garbage collection automatically to remove expired
cache files.
Please refer to {@see \Psr\SimpleCache\CacheInterface} for common cache operations that are supported by FileCache.
Public Methods
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| EXPIRATION_EXPIRED | -1 | Yiisoft\Cache\File\FileCache | |
| TTL_INFINITY | 31536000 | Yiisoft\Cache\File\FileCache |
Method Details
See also \Yiisoft\Cache\File\FileCache::$cachePath.
| public mixed __construct ( string $cachePath, integer $directoryMode = 0775 ) | ||
| $cachePath | string |
The directory to store cache files. |
| $directoryMode | integer |
The permission to be set for newly created directories. This value will be used
by PHP |
| throws | Yiisoft\Cache\File\CacheException |
If failed to create cache directory. |
|---|---|---|
public function __construct(
private readonly string $cachePath,
private int $directoryMode = 0775,
) {
}
| public boolean clear ( ) |
public function clear(): bool
{
$this->removeCacheFiles($this->cachePath, false);
return true;
}
| public boolean delete ( string $key ) | ||
| $key | string | |
public function delete(string $key): bool
{
$this->validateKey($key);
$file = $this->getCacheFile($key);
if (!is_file($file)) {
return true;
}
$result = @unlink($file);
return $this->isLastErrorSafe($result);
}
| public boolean deleteMultiple ( iterable $keys ) | ||
| $keys | iterable | |
public function deleteMultiple(iterable $keys): bool
{
$keys = $this->iterableToArray($keys);
$this->validateKeys($keys);
foreach ($keys as $key) {
$this->delete($key);
}
return true;
}
| public mixed get ( string $key, mixed $default = null ) | ||
| $key | string | |
| $default | mixed | |
public function get(string $key, mixed $default = null): mixed
{
$this->validateKey($key);
$file = $this->getCacheFile($key);
if (!$this->existsAndNotExpired($file) || ($filePointer = @fopen($file, 'rb')) === false) {
return $default;
}
flock($filePointer, LOCK_SH);
/**
* @var string $value We assume that we always can read content from `$filePointer` resource.
*/
$value = stream_get_contents($filePointer);
flock($filePointer, LOCK_UN);
fclose($filePointer);
return unserialize($value);
}
| public iterable getMultiple ( iterable $keys, mixed $default = null ) | ||
| $keys | iterable | |
| $default | mixed | |
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
$keys = $this->iterableToArray($keys);
$this->validateKeys($keys);
$results = [];
foreach ($keys as $key) {
$results[$key] = $this->get($key, $default);
}
return $results;
}
| public boolean has ( string $key ) | ||
| $key | string | |
public function has(string $key): bool
{
$this->validateKey($key);
return $this->existsAndNotExpired($this->getCacheFile($key));
}
| public boolean set ( string $key, mixed $value, null|integer|DateInterval $ttl = null ) | ||
| $key | string | |
| $value | mixed | |
| $ttl | null|integer|DateInterval | |
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
{
$this->validateKey($key);
$this->gc();
$expiration = $this->ttlToExpiration($ttl);
if ($expiration <= self::EXPIRATION_EXPIRED) {
return $this->delete($key);
}
$file = $this->getCacheFile($key, ensureDirectory: 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 (function_exists('posix_geteuid') && is_file($file) && fileowner($file) !== posix_geteuid()) {
@unlink($file);
}
if (file_put_contents($file, serialize($value), LOCK_EX) === false) {
return false;
}
if ($this->fileMode !== null) {
$result = @chmod($file, $this->fileMode);
if (!$this->isLastErrorSafe($result)) {
return false;
}
}
$result = false;
if (@touch($file, $expiration)) {
clearstatcache();
$result = true;
}
return $this->isLastErrorSafe($result);
}
| public boolean setMultiple ( iterable $values, null|integer|DateInterval $ttl = null ) | ||
| $values | iterable | |
| $ttl | null|integer|DateInterval | |
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
{
$values = $this->iterableToArray($values);
$this->validateKeys(array_map(\strval(...), array_keys($values)));
foreach ($values as $key => $value) {
$this->set((string) $key, $value, $ttl);
}
return true;
}
| public self withDirectoryLevel ( integer $directoryLevel ) | ||
| $directoryLevel | integer |
The level of sub-directories to store cache files. Defaults to 1. If the system has huge number of cache files (e.g. one million), you may use a bigger value (usually no bigger than 3). Using sub-directories is mainly to ensure the file system is not over burdened with a single directory having too many files. |
public function withDirectoryLevel(int $directoryLevel): self
{
$new = clone $this;
$new->directoryLevel = $directoryLevel;
return $new;
}
$directoryMode in the constructor instead
| public self withDirectoryMode ( integer $directoryMode ) | ||
| $directoryMode | integer |
The permission to be set for newly created directories. This value will be used
by PHP |
public function withDirectoryMode(int $directoryMode): self
{
$new = clone $this;
$new->directoryMode = $directoryMode;
return $new;
}
| public self withFileMode ( integer $fileMode ) | ||
| $fileMode | integer |
The permission to be set for newly created cache files. This value will be used
by PHP |
public function withFileMode(int $fileMode): self
{
$new = clone $this;
$new->fileMode = $fileMode;
return $new;
}
| public self withFileSuffix ( string $fileSuffix ) | ||
| $fileSuffix | string |
The cache file suffix. Defaults to '.bin'. |
public function withFileSuffix(string $fileSuffix): self
{
$new = clone $this;
$new->fileSuffix = $fileSuffix;
return $new;
}
| public self withGcProbability ( integer $gcProbability ) | ||
| $gcProbability | integer |
The probability (parts per million) that garbage collection (GC) should be performed when storing a piece of data in the cache. Defaults to 10, meaning 0.001% chance. This number should be between 0 and 1000000. A value 0 means no GC will be performed at all. |
public function withGcProbability(int $gcProbability): self
{
$new = clone $this;
$new->gcProbability = $gcProbability;
return $new;
}
Signup or Login in order to comment.