1 | <?php |
||
39 | class DbCache extends Cache |
||
40 | { |
||
41 | /** |
||
42 | * @var Connection|array|string the DB connection object or the application component ID of the DB connection. |
||
43 | * After the DbCache object is created, if you want to change this property, you should only assign it |
||
44 | * with a DB connection object. |
||
45 | * Starting from version 2.0.2, this can also be a configuration array for creating the object. |
||
46 | */ |
||
47 | public $db = 'db'; |
||
48 | /** |
||
49 | * @var string name of the DB table to store cache content. |
||
50 | * The table should be pre-created as follows: |
||
51 | * |
||
52 | * ```php |
||
53 | * CREATE TABLE cache ( |
||
54 | * id char(128) NOT NULL PRIMARY KEY, |
||
55 | * expire int(11), |
||
56 | * data BLOB |
||
57 | * ); |
||
58 | * ``` |
||
59 | * |
||
60 | * where 'BLOB' refers to the BLOB-type of your preferred DBMS. Below are the BLOB type |
||
61 | * that can be used for some popular DBMS: |
||
62 | * |
||
63 | * - MySQL: LONGBLOB |
||
64 | * - PostgreSQL: BYTEA |
||
65 | * - MSSQL: BLOB |
||
66 | * |
||
67 | * When using DbCache in a production server, we recommend you create a DB index for the 'expire' |
||
68 | * column in the cache table to improve the performance. |
||
69 | */ |
||
70 | public $cacheTable = '{{%cache}}'; |
||
71 | /** |
||
72 | * @var int the probability (parts per million) that garbage collection (GC) should be performed |
||
73 | * when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance. |
||
74 | * This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all. |
||
75 | */ |
||
76 | public $gcProbability = 100; |
||
77 | |||
78 | |||
79 | /** |
||
80 | * Initializes the DbCache component. |
||
81 | * This method will initialize the [[db]] property to make sure it refers to a valid DB connection. |
||
82 | * @throws InvalidConfigException if [[db]] is invalid. |
||
83 | */ |
||
84 | 37 | public function init() |
|
89 | |||
90 | /** |
||
91 | * Checks whether a specified key exists in the cache. |
||
92 | * This can be faster than getting the value from the cache if the data is big. |
||
93 | * Note that this method does not check whether the dependency associated |
||
94 | * with the cached data, if there is any, has changed. So a call to [[get]] |
||
95 | * may return false while exists returns true. |
||
96 | * @param mixed $key a key identifying the cached value. This can be a simple string or |
||
97 | * a complex data structure consisting of factors representing the key. |
||
98 | * @return bool true if a value exists in cache, false if the value is not in the cache or expired. |
||
99 | */ |
||
100 | 2 | public function exists($key) |
|
119 | |||
120 | /** |
||
121 | * Retrieves a value from cache with a specified key. |
||
122 | * This is the implementation of the method declared in the parent class. |
||
123 | * @param string $key a unique key identifying the cached value |
||
124 | * @return string|false the value stored in cache, false if the value is not in the cache or expired. |
||
125 | */ |
||
126 | 30 | protected function getValue($key) |
|
143 | |||
144 | /** |
||
145 | * Retrieves multiple values from cache with the specified keys. |
||
146 | * @param array $keys a list of keys identifying the cached values |
||
147 | * @return array a list of cached values indexed by the keys |
||
148 | */ |
||
149 | 4 | protected function getValues($keys) |
|
150 | { |
||
151 | 4 | if (empty($keys)) { |
|
152 | return []; |
||
153 | } |
||
154 | 4 | $query = new Query(); |
|
155 | 4 | $query->select(['id', 'data']) |
|
156 | 4 | ->from($this->cacheTable) |
|
157 | 4 | ->where(['id' => $keys]) |
|
158 | 4 | ->andWhere('([[expire]] = 0 OR [[expire]] > ' . time() . ')'); |
|
159 | |||
160 | 4 | if ($this->db->enableQueryCache) { |
|
161 | 4 | $this->db->enableQueryCache = false; |
|
162 | 4 | $rows = $query->createCommand($this->db)->queryAll(); |
|
163 | 4 | $this->db->enableQueryCache = true; |
|
164 | } else { |
||
165 | $rows = $query->createCommand($this->db)->queryAll(); |
||
166 | } |
||
167 | |||
168 | 4 | $results = []; |
|
169 | 4 | foreach ($keys as $key) { |
|
170 | 4 | $results[$key] = false; |
|
171 | } |
||
172 | 4 | foreach ($rows as $row) { |
|
173 | 4 | if (is_resource($row['data']) && get_resource_type($row['data']) === 'stream') { |
|
174 | 2 | $results[$row['id']] = stream_get_contents($row['data']); |
|
175 | } else { |
||
176 | 4 | $results[$row['id']] = $row['data']; |
|
177 | } |
||
178 | } |
||
179 | |||
180 | 4 | return $results; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Stores a value identified by a key in cache. |
||
185 | * This is the implementation of the method declared in the parent class. |
||
186 | * |
||
187 | * @param string $key the key identifying the value to be cached |
||
188 | * @param string $value the value to be cached. Other types (if you have disabled [[serializer]]) cannot be saved. |
||
189 | * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire. |
||
190 | * @return bool true if the value is successfully stored into cache, false otherwise |
||
191 | */ |
||
192 | 30 | protected function setValue($key, $value, $duration) |
|
211 | |||
212 | /** |
||
213 | * Stores a value identified by a key into cache if the cache does not contain this key. |
||
214 | * This is the implementation of the method declared in the parent class. |
||
215 | * |
||
216 | * @param string $key the key identifying the value to be cached |
||
217 | * @param string $value the value to be cached. Other types (if you have disabled [[serializer]]) cannot be saved. |
||
218 | * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire. |
||
219 | * @return bool true if the value is successfully stored into cache, false otherwise |
||
220 | */ |
||
221 | 31 | protected function addValue($key, $value, $duration) |
|
240 | |||
241 | /** |
||
242 | * Deletes a value with the specified key from cache |
||
243 | * This is the implementation of the method declared in the parent class. |
||
244 | * @param string $key the key of the value to be deleted |
||
245 | * @return bool if no error happens during deletion |
||
246 | */ |
||
247 | protected function deleteValue($key) |
||
257 | |||
258 | /** |
||
259 | * Removes the expired data values. |
||
260 | * @param bool $force whether to enforce the garbage collection regardless of [[gcProbability]]. |
||
261 | * Defaults to false, meaning the actual deletion happens with the probability as specified by [[gcProbability]]. |
||
262 | */ |
||
263 | 32 | public function gc($force = false) |
|
271 | |||
272 | /** |
||
273 | * Deletes all values from cache. |
||
274 | * This is the implementation of the method declared in the parent class. |
||
275 | * @return bool whether the flush operation was successful. |
||
276 | */ |
||
277 | 22 | protected function flushValues() |
|
285 | } |
||
286 |