PHP Class yii\caching\FileCache

For each data value being cached, FileCache will store it in a separate file. The cache files are placed under [[cachePath]]. FileCache will perform garbage collection automatically to remove expired cache files. Please refer to Cache for common cache operations that are supported by FileCache.
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends Cache
Show file Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$cacheFileSuffix cache file suffix. Defaults to '.bin'.
$cachePath the directory to store cache files. You may use path alias here. If not set, it will use the "cache" subdirectory under the application runtime path.
$dirMode the permission to be set for newly created directories. This value will be used by PHP chmod() function. No umask will be applied. Defaults to 0775, meaning the directory is read-writable by owner and group, but read-only for other users.
$directoryLevel 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.
$fileMode the permission to be set for newly created cache files. This value will be used by PHP chmod() function. No umask will be applied. If not set, the permission will be determined by the current environment.
$gcProbability 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.
$keyPrefix a string prefixed to every cache key. This is needed when you store cache data under the same [[cachePath]] for different applications to avoid conflict. To ensure interoperability, only alphanumeric characters should be used.

Public Methods

Method Description
exists ( mixed $key ) : boolean Checks whether a specified key exists in the cache.
gc ( boolean $force = false, boolean $expiredOnly = true ) Removes expired cache files.
init ( ) Initializes this component by ensuring the existence of the cache path.

Protected Methods

Method Description
addValue ( string $key, string $value, integer $duration ) : boolean Stores a value identified by a key into cache if the cache does not contain this key.
deleteValue ( string $key ) : boolean Deletes a value with the specified key from cache This is the implementation of the method declared in the parent class.
flushValues ( ) : boolean Deletes all values from cache.
gcRecursive ( string $path, boolean $expiredOnly ) Recursively removing expired cache files under a directory.
getCacheFile ( string $key ) : string Returns the cache file path given the cache key.
getValue ( string $key ) : string | false Retrieves a value from cache with a specified key.
setValue ( string $key, string $value, integer $duration ) : boolean Stores a value identified by a key in cache.

Method Details

addValue() protected method

This is the implementation of the method declared in the parent class.
protected addValue ( string $key, string $value, integer $duration ) : boolean
$key string the key identifying the value to be cached
$value string the value to be cached. Other types (if you have disabled [[serializer]]) unable to get is correct in [[getValue()]].
$duration integer the number of seconds in which the cached value will expire. 0 means never expire.
return boolean true if the value is successfully stored into cache, false otherwise

deleteValue() protected method

Deletes a value with the specified key from cache This is the implementation of the method declared in the parent class.
protected deleteValue ( string $key ) : boolean
$key string the key of the value to be deleted
return boolean if no error happens during deletion

exists() public method

This can be faster than getting the value from the cache if the data is big. Note that this method does not check whether the dependency associated with the cached data, if there is any, has changed. So a call to [[get]] may return false while exists returns true.
public exists ( mixed $key ) : boolean
$key mixed a key identifying the cached value. This can be a simple string or a complex data structure consisting of factors representing the key.
return boolean true if a value exists in cache, false if the value is not in the cache or expired.

flushValues() protected method

This is the implementation of the method declared in the parent class.
protected flushValues ( ) : boolean
return boolean whether the flush operation was successful.

gc() public method

Removes expired cache files.
public gc ( boolean $force = false, boolean $expiredOnly = true )
$force boolean whether to enforce the garbage collection regardless of [[gcProbability]]. Defaults to false, meaning the actual deletion happens with the probability as specified by [[gcProbability]].
$expiredOnly boolean whether to removed expired cache files only. If false, all cache files under [[cachePath]] will be removed.

gcRecursive() protected method

This method is mainly used by FileCache::gc.
protected gcRecursive ( string $path, boolean $expiredOnly )
$path string the directory under which expired cache files are removed.
$expiredOnly boolean whether to only remove expired cache files. If false, all files under `$path` will be removed.

getCacheFile() protected method

Returns the cache file path given the cache key.
protected getCacheFile ( string $key ) : string
$key string cache key
return string the cache file path

getValue() protected method

This is the implementation of the method declared in the parent class.
protected getValue ( string $key ) : string | false
$key string a unique key identifying the cached value
return string | false the value stored in cache, false if the value is not in the cache or expired.

init() public method

Initializes this component by ensuring the existence of the cache path.
public init ( )

setValue() protected method

This is the implementation of the method declared in the parent class.
protected setValue ( string $key, string $value, integer $duration ) : boolean
$key string the key identifying the value to be cached
$value string the value to be cached. Other types (If you have disabled [[serializer]]) unable to get is correct in [[getValue()]].
$duration integer the number of seconds in which the cached value will expire. 0 means never expire.
return boolean true if the value is successfully stored into cache, false otherwise

Property Details

$cacheFileSuffix public property

cache file suffix. Defaults to '.bin'.
public $cacheFileSuffix

$cachePath public property

the directory to store cache files. You may use path alias here. If not set, it will use the "cache" subdirectory under the application runtime path.
public $cachePath

$dirMode public property

the permission to be set for newly created directories. This value will be used by PHP chmod() function. No umask will be applied. Defaults to 0775, meaning the directory is read-writable by owner and group, but read-only for other users.
public $dirMode

$directoryLevel public property

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 $directoryLevel

$fileMode public property

the permission to be set for newly created cache files. This value will be used by PHP chmod() function. No umask will be applied. If not set, the permission will be determined by the current environment.
public $fileMode

$gcProbability public property

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 $gcProbability

$keyPrefix public property

a string prefixed to every cache key. This is needed when you store cache data under the same [[cachePath]] for different applications to avoid conflict. To ensure interoperability, only alphanumeric characters should be used.
public $keyPrefix