1 | <?php |
||
45 | class FileCache extends SimpleCache |
||
46 | { |
||
47 | /** |
||
48 | * @var string the directory to store cache files. You may use [path alias](guide:concept-aliases) here. |
||
49 | * If not set, it will use the "cache" subdirectory under the application runtime path. |
||
50 | */ |
||
51 | public $cachePath = '@runtime/cache'; |
||
52 | /** |
||
53 | * @var string cache file suffix. Defaults to '.bin'. |
||
54 | */ |
||
55 | public $cacheFileSuffix = '.bin'; |
||
56 | /** |
||
57 | * @var int the level of sub-directories to store cache files. Defaults to 1. |
||
58 | * If the system has huge number of cache files (e.g. one million), you may use a bigger value |
||
59 | * (usually no bigger than 3). Using sub-directories is mainly to ensure the file system |
||
60 | * is not over burdened with a single directory having too many files. |
||
61 | */ |
||
62 | public $directoryLevel = 1; |
||
63 | /** |
||
64 | * @var int the probability (parts per million) that garbage collection (GC) should be performed |
||
65 | * when storing a piece of data in the cache. Defaults to 10, meaning 0.001% chance. |
||
66 | * This number should be between 0 and 1000000. A value 0 means no GC will be performed at all. |
||
67 | */ |
||
68 | public $gcProbability = 10; |
||
69 | /** |
||
70 | * @var int the permission to be set for newly created cache files. |
||
71 | * This value will be used by PHP chmod() function. No umask will be applied. |
||
72 | * If not set, the permission will be determined by the current environment. |
||
73 | */ |
||
74 | public $fileMode; |
||
75 | /** |
||
76 | * @var int the permission to be set for newly created directories. |
||
77 | * This value will be used by PHP chmod() function. No umask will be applied. |
||
78 | * Defaults to 0775, meaning the directory is read-writable by owner and group, |
||
79 | * but read-only for other users. |
||
80 | */ |
||
81 | public $dirMode = 0775; |
||
82 | |||
83 | |||
84 | /** |
||
85 | * Initializes this component by ensuring the existence of the cache path. |
||
86 | */ |
||
87 | 129 | public function init() |
|
88 | { |
||
89 | 129 | parent::init(); |
|
90 | 129 | $this->cachePath = Yii::getAlias($this->cachePath); |
|
|
|||
91 | 129 | if (!is_dir($this->cachePath)) { |
|
92 | 1 | FileHelper::createDirectory($this->cachePath, $this->dirMode, true); |
|
93 | } |
||
94 | 129 | } |
|
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | 3 | public function has($key) |
|
100 | { |
||
101 | 3 | $cacheFile = $this->getCacheFile($this->normalizeKey($key)); |
|
102 | |||
103 | 3 | return @filemtime($cacheFile) > time(); |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | 29 | protected function getValue($key) |
|
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 28 | protected function setValue($key, $value, $ttl) |
|
131 | { |
||
132 | 28 | $this->gc(); |
|
133 | 28 | $cacheFile = $this->getCacheFile($key); |
|
134 | 28 | if ($this->directoryLevel > 0) { |
|
135 | 28 | @FileHelper::createDirectory(dirname($cacheFile), $this->dirMode, true); |
|
136 | } |
||
137 | // If ownership differs the touch call will fail, so we try to |
||
138 | // rebuild the file from scratch by deleting it first |
||
139 | // https://github.com/yiisoft/yii2/pull/16120 |
||
140 | 28 | if (is_file($cacheFile) && function_exists('posix_geteuid') && fileowner($cacheFile) !== posix_geteuid()) { |
|
141 | 1 | @unlink($cacheFile); |
|
142 | } |
||
143 | 28 | if (@file_put_contents($cacheFile, $value, LOCK_EX) !== false) { |
|
144 | 28 | if ($this->fileMode !== null) { |
|
145 | @chmod($cacheFile, $this->fileMode); |
||
146 | } |
||
147 | 28 | if ($ttl <= 0) { |
|
148 | 24 | $ttl = 31536000; // 1 year |
|
149 | } |
||
150 | |||
151 | 28 | return @touch($cacheFile, $ttl + time()); |
|
152 | } |
||
153 | |||
154 | $error = error_get_last(); |
||
155 | Yii::warning("Unable to write cache file '{$cacheFile}': {$error['message']}", __METHOD__); |
||
156 | return false; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | 99 | protected function deleteValue($key) |
|
167 | |||
168 | /** |
||
169 | * Returns the cache file path given the cache key. |
||
170 | * @param string $key cache key |
||
171 | * @return string the cache file path |
||
172 | */ |
||
173 | 118 | protected function getCacheFile($key) |
|
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | 11 | public function clear() |
|
197 | |||
198 | /** |
||
199 | * Removes expired cache files. |
||
200 | * @param bool $force whether to enforce the garbage collection regardless of [[gcProbability]]. |
||
201 | * Defaults to false, meaning the actual deletion happens with the probability as specified by [[gcProbability]]. |
||
202 | * @param bool $expiredOnly whether to removed expired cache files only. |
||
203 | * If false, all cache files under [[cachePath]] will be removed. |
||
204 | */ |
||
205 | 28 | public function gc($force = false, $expiredOnly = true) |
|
211 | |||
212 | /** |
||
213 | * Recursively removing expired cache files under a directory. |
||
214 | * This method is mainly used by [[gc()]]. |
||
215 | * @param string $path the directory under which expired cache files are removed. |
||
216 | * @param bool $expiredOnly whether to only remove expired cache files. If false, all files |
||
217 | * under `$path` will be removed. |
||
218 | */ |
||
219 | 11 | protected function gcRecursive($path, $expiredOnly) |
|
245 | } |
||
246 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.