1 | <?php |
||
48 | class DbCache extends SimpleCache |
||
49 | { |
||
50 | /** |
||
51 | * @var Connection|array|string the DB connection object or the application component ID of the DB connection. |
||
52 | * After the DbCache object is created, if you want to change this property, you should only assign it |
||
53 | * with a DB connection object. |
||
54 | * Starting from version 2.0.2, this can also be a configuration array for creating the object. |
||
55 | */ |
||
56 | public $db = 'db'; |
||
57 | /** |
||
58 | * @var string name of the DB table to store cache content. |
||
59 | * The table should be pre-created as follows: |
||
60 | * |
||
61 | * ```php |
||
62 | * CREATE TABLE cache ( |
||
63 | * id char(128) NOT NULL PRIMARY KEY, |
||
64 | * expire int(11), |
||
65 | * data BLOB |
||
66 | * ); |
||
67 | * ``` |
||
68 | * |
||
69 | * where 'BLOB' refers to the BLOB-type of your preferred DBMS. Below are the BLOB type |
||
70 | * that can be used for some popular DBMS: |
||
71 | * |
||
72 | * - MySQL: LONGBLOB |
||
73 | * - PostgreSQL: BYTEA |
||
74 | * - MSSQL: BLOB |
||
75 | * |
||
76 | * When using DbCache in a production server, we recommend you create a DB index for the 'expire' |
||
77 | * column in the cache table to improve the performance. |
||
78 | */ |
||
79 | public $cacheTable = '{{%cache}}'; |
||
80 | /** |
||
81 | * @var int the probability (parts per million) that garbage collection (GC) should be performed |
||
82 | * when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance. |
||
83 | * This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all. |
||
84 | */ |
||
85 | public $gcProbability = 100; |
||
86 | |||
87 | |||
88 | /** |
||
89 | * Initializes the DbCache component. |
||
90 | * This method will initialize the [[db]] property to make sure it refers to a valid DB connection. |
||
91 | * @throws InvalidConfigException if [[db]] is invalid. |
||
92 | */ |
||
93 | 19 | public function init() |
|
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | 3 | public function has($key) |
|
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | 15 | protected function getValue($key) |
|
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | 3 | protected function getValues($keys) |
|
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | protected function setValue($key, $value, $ttl) |
||
193 | |||
194 | /** |
||
195 | * Stores a value identified by a key into cache if the cache does not contain this key. |
||
196 | * This is the implementation of the method declared in the parent class. |
||
197 | * |
||
198 | * @param string $key the key identifying the value to be cached |
||
199 | * @param string $value the value to be cached. Other types (if you have disabled [[serializer]]) cannot be saved. |
||
200 | * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire. |
||
201 | * @return bool true if the value is successfully stored into cache, false otherwise |
||
202 | */ |
||
203 | 16 | protected function addValue($key, $value, $duration) |
|
204 | { |
||
205 | 16 | $this->gc(); |
|
206 | |||
207 | try { |
||
208 | 16 | $this->db->noCache(function (Connection $db) use ($key, $value, $duration) { |
|
209 | 16 | $db->createCommand() |
|
210 | 16 | ->insert($this->cacheTable, [ |
|
211 | 16 | 'id' => $key, |
|
212 | 16 | 'expire' => $duration > 0 ? $duration + time() : 0, |
|
213 | 16 | 'data' => [$value, \PDO::PARAM_LOB], |
|
214 | 16 | ])->execute(); |
|
215 | 16 | }); |
|
216 | |||
217 | 16 | return true; |
|
218 | } catch (\Exception $e) { |
||
219 | return false; |
||
220 | } |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | */ |
||
226 | protected function deleteValue($key) |
||
236 | |||
237 | /** |
||
238 | * Removes the expired data values. |
||
239 | * @param bool $force whether to enforce the garbage collection regardless of [[gcProbability]]. |
||
240 | * Defaults to false, meaning the actual deletion happens with the probability as specified by [[gcProbability]]. |
||
241 | */ |
||
242 | 16 | public function gc($force = false) |
|
250 | |||
251 | /** |
||
252 | * {@inheritdoc} |
||
253 | */ |
||
254 | 11 | public function clear() |
|
262 | } |
||
263 |