Completed
Push — 11401-fix-dbsession-concurrenc... ( 3ed3ac )
by Alexander
41:37 queued 38:18
created

DbCache::deleteValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\caching;
9
10
use Yii;
11
use yii\base\InvalidConfigException;
12
use yii\db\Connection;
13
use yii\db\PdoValue;
14
use yii\db\Query;
15
use yii\di\Instance;
16
17
/**
18
 * DbCache implements a cache application component by storing cached data in a database.
19
 *
20
 * By default, DbCache stores session data in a DB table named 'cache'. This table
21
 * must be pre-created. The table name can be changed by setting [[cacheTable]].
22
 *
23
 * Please refer to [[Cache]] for common cache operations that are supported by DbCache.
24
 *
25
 * The following example shows how you can configure the application to use DbCache:
26
 *
27
 * ```php
28
 * 'cache' => [
29
 *     'class' => 'yii\caching\DbCache',
30
 *     // 'db' => 'mydb',
31
 *     // 'cacheTable' => 'my_cache',
32
 * ]
33
 * ```
34
 *
35
 * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
36
 *
37
 * @author Qiang Xue <[email protected]>
38
 * @since 2.0
39
 */
40
class DbCache extends Cache
41
{
42
    /**
43
     * @var Connection|array|string the DB connection object or the application component ID of the DB connection.
44
     * After the DbCache object is created, if you want to change this property, you should only assign it
45
     * with a DB connection object.
46
     * Starting from version 2.0.2, this can also be a configuration array for creating the object.
47
     */
48
    public $db = 'db';
49
    /**
50
     * @var string name of the DB table to store cache content.
51
     * The table should be pre-created as follows:
52
     *
53
     * ```php
54
     * CREATE TABLE cache (
55
     *     id char(128) NOT NULL PRIMARY KEY,
56
     *     expire int(11),
57
     *     data BLOB
58
     * );
59
     * ```
60
     *
61
     * where 'BLOB' refers to the BLOB-type of your preferred DBMS. Below are the BLOB type
62
     * that can be used for some popular DBMS:
63
     *
64
     * - MySQL: LONGBLOB
65
     * - PostgreSQL: BYTEA
66
     * - MSSQL: BLOB
67
     *
68
     * When using DbCache in a production server, we recommend you create a DB index for the 'expire'
69
     * column in the cache table to improve the performance.
70
     */
71
    public $cacheTable = '{{%cache}}';
72
    /**
73
     * @var int the probability (parts per million) that garbage collection (GC) should be performed
74
     * when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
75
     * This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
76
     */
77
    public $gcProbability = 100;
78
79
80
    /**
81
     * Initializes the DbCache component.
82
     * This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
83
     * @throws InvalidConfigException if [[db]] is invalid.
84
     */
85 37
    public function init()
86
    {
87 37
        parent::init();
88 37
        $this->db = Instance::ensure($this->db, Connection::className());
89 37
    }
90
91
    /**
92
     * Checks whether a specified key exists in the cache.
93
     * This can be faster than getting the value from the cache if the data is big.
94
     * Note that this method does not check whether the dependency associated
95
     * with the cached data, if there is any, has changed. So a call to [[get]]
96
     * may return false while exists returns true.
97
     * @param mixed $key a key identifying the cached value. This can be a simple string or
98
     * a complex data structure consisting of factors representing the key.
99
     * @return bool true if a value exists in cache, false if the value is not in the cache or expired.
100
     */
101 2
    public function exists($key)
102
    {
103 2
        $key = $this->buildKey($key);
104
105 2
        $query = new Query();
106 2
        $query->select(['COUNT(*)'])
107 2
            ->from($this->cacheTable)
108 2
            ->where('[[id]] = :id AND ([[expire]] = 0 OR [[expire]] >' . time() . ')', [':id' => $key]);
109 2
        if ($this->db->enableQueryCache) {
110
            // temporarily disable and re-enable query caching
111 2
            $this->db->enableQueryCache = false;
112 2
            $result = $query->createCommand($this->db)->queryScalar();
113 2
            $this->db->enableQueryCache = true;
114
        } else {
115
            $result = $query->createCommand($this->db)->queryScalar();
116
        }
117
118 2
        return $result > 0;
119
    }
120
121
    /**
122
     * Retrieves a value from cache with a specified key.
123
     * This is the implementation of the method declared in the parent class.
124
     * @param string $key a unique key identifying the cached value
125
     * @return string|false the value stored in cache, false if the value is not in the cache or expired.
126
     */
127 30
    protected function getValue($key)
128
    {
129 30
        $query = new Query();
130 30
        $query->select(['data'])
131 30
            ->from($this->cacheTable)
132 30
            ->where('[[id]] = :id AND ([[expire]] = 0 OR [[expire]] >' . time() . ')', [':id' => $key]);
133 30
        if ($this->db->enableQueryCache) {
134
            // temporarily disable and re-enable query caching
135 30
            $this->db->enableQueryCache = false;
136 30
            $result = $query->createCommand($this->db)->queryScalar();
137 30
            $this->db->enableQueryCache = true;
138
139 30
            return $result;
140
        }
141
142
        return $query->createCommand($this->db)->queryScalar();
143
    }
144
145
    /**
146
     * Retrieves multiple values from cache with the specified keys.
147
     * @param array $keys a list of keys identifying the cached values
148
     * @return array a list of cached values indexed by the keys
149
     */
150 4
    protected function getValues($keys)
151
    {
152 4
        if (empty($keys)) {
153
            return [];
154
        }
155 4
        $query = new Query();
156 4
        $query->select(['id', 'data'])
157 4
            ->from($this->cacheTable)
158 4
            ->where(['id' => $keys])
159 4
            ->andWhere('([[expire]] = 0 OR [[expire]] > ' . time() . ')');
160
161 4
        if ($this->db->enableQueryCache) {
162 4
            $this->db->enableQueryCache = false;
163 4
            $rows = $query->createCommand($this->db)->queryAll();
164 4
            $this->db->enableQueryCache = true;
165
        } else {
166
            $rows = $query->createCommand($this->db)->queryAll();
167
        }
168
169 4
        $results = [];
170 4
        foreach ($keys as $key) {
171 4
            $results[$key] = false;
172
        }
173 4
        foreach ($rows as $row) {
174 4
            if (is_resource($row['data']) && get_resource_type($row['data']) === 'stream') {
175 2
                $results[$row['id']] = stream_get_contents($row['data']);
176
            } else {
177 4
                $results[$row['id']] = $row['data'];
178
            }
179
        }
180
181 4
        return $results;
182
    }
183
184
    /**
185
     * Stores a value identified by a key in cache.
186
     * This is the implementation of the method declared in the parent class.
187
     *
188
     * @param string $key the key identifying the value to be cached
189
     * @param string $value the value to be cached. Other types (if you have disabled [[serializer]]) cannot be saved.
190
     * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
191
     * @return bool true if the value is successfully stored into cache, false otherwise
192
     */
193 30
    protected function setValue($key, $value, $duration)
194
    {
195
        $result = $this->db->noCache(function (Connection $db) use ($key, $value, $duration) {
196 30
            $command = $db->createCommand()
197 30
                ->update($this->cacheTable, [
198 30
                    'expire' => $duration > 0 ? $duration + time() : 0,
199 30
                    'data' => new PdoValue($value, \PDO::PARAM_LOB),
200 30
                ], ['id' => $key]);
201 30
            return $command->execute();
202 30
        });
203
204 30
        if ($result) {
205 3
            $this->gc();
206
207 3
            return true;
208
        }
209
210 29
        return $this->addValue($key, $value, $duration);
211
    }
212
213
    /**
214
     * Stores a value identified by a key into cache if the cache does not contain this key.
215
     * This is the implementation of the method declared in the parent class.
216
     *
217
     * @param string $key the key identifying the value to be cached
218
     * @param string $value the value to be cached. Other types (if you have disabled [[serializer]]) cannot be saved.
219
     * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
220
     * @return bool true if the value is successfully stored into cache, false otherwise
221
     */
222 31
    protected function addValue($key, $value, $duration)
223
    {
224 31
        $this->gc();
225
226
        try {
227
            $this->db->noCache(function (Connection $db) use ($key, $value, $duration) {
228 31
                $db->createCommand()
229 31
                    ->insert($this->cacheTable, [
230 31
                        'id' => $key,
231 31
                        'expire' => $duration > 0 ? $duration + time() : 0,
232 31
                        'data' => new PdoValue($value, \PDO::PARAM_LOB),
233 31
                    ])->execute();
234 31
            });
235
236 30
            return true;
237 5
        } catch (\Exception $e) {
238 5
            return false;
239
        }
240
    }
241
242
    /**
243
     * Deletes a value with the specified key from cache
244
     * This is the implementation of the method declared in the parent class.
245
     * @param string $key the key of the value to be deleted
246
     * @return bool if no error happens during deletion
247
     */
248
    protected function deleteValue($key)
249
    {
250 2
        $this->db->noCache(function (Connection $db) use ($key) {
251 2
            $db->createCommand()
252 2
                ->delete($this->cacheTable, ['id' => $key])
253 2
                ->execute();
254 2
        });
255
256 2
        return true;
257
    }
258
259
    /**
260
     * Removes the expired data values.
261
     * @param bool $force whether to enforce the garbage collection regardless of [[gcProbability]].
262
     * Defaults to false, meaning the actual deletion happens with the probability as specified by [[gcProbability]].
263
     */
264 32
    public function gc($force = false)
265
    {
266 32
        if ($force || mt_rand(0, 1000000) < $this->gcProbability) {
267
            $this->db->createCommand()
268
                ->delete($this->cacheTable, '[[expire]] > 0 AND [[expire]] < ' . time())
269
                ->execute();
270
        }
271 32
    }
272
273
    /**
274
     * Deletes all values from cache.
275
     * This is the implementation of the method declared in the parent class.
276
     * @return bool whether the flush operation was successful.
277
     */
278 22
    protected function flushValues()
279
    {
280 22
        $this->db->createCommand()
281 22
            ->delete($this->cacheTable)
282 22
            ->execute();
283
284 22
        return true;
285
    }
286
}
287