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 | 37 | public function init() |
|
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | 6 | public function has($key) |
|
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | 30 | protected function getValue($key) |
|
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | 6 | protected function getValues($keys) |
|
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | protected function setValue($key, $value, $ttl) |
||
198 | |||
199 | /** |
||
200 | * Stores a value identified by a key into cache if the cache does not contain this key. |
||
201 | * This is the implementation of the method declared in the parent class. |
||
202 | * |
||
203 | * @param string $key the key identifying the value to be cached |
||
204 | * @param string $value the value to be cached. Other types (if you have disabled [[serializer]]) cannot be saved. |
||
205 | * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire. |
||
206 | * @return bool true if the value is successfully stored into cache, false otherwise |
||
207 | */ |
||
208 | 32 | protected function addValue($key, $value, $duration) |
|
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | protected function deleteValue($key) |
||
241 | |||
242 | /** |
||
243 | * Removes the expired data values. |
||
244 | * @param bool $force whether to enforce the garbage collection regardless of [[gcProbability]]. |
||
245 | * Defaults to false, meaning the actual deletion happens with the probability as specified by [[gcProbability]]. |
||
246 | */ |
||
247 | 32 | public function gc($force = false) |
|
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | 22 | public function clear() |
|
267 | } |
||
268 |