|
16 | 16 | * |
17 | 17 | * Redis Cache requires redis version 2.6.12 or higher to work properly. |
18 | 18 | * |
19 | | - * It needs to be configured with a redis [[Connection]] that is also configured as an application component. |
20 | | - * By default it will use the `redis` application component. |
| 19 | + * It needs to be configured with a redis [[Connection]]. By default it will use the `redis` application component. |
21 | 20 | * |
22 | | - * See [[Cache]] manual for common cache operations that redis Cache supports. |
| 21 | + * > Note: It is recommended to use separate [[Connection::$database|database]] for cache and do not share it with |
| 22 | + * > other components. If you need to share database, you should set [[$shareDatabase]] to `true` and make sure that |
| 23 | + * > [[$keyPrefix]] has unique value which will allow to distinguish between cache keys and other data in database. |
23 | 24 | * |
24 | | - * Unlike the [[Cache]], redis Cache allows the expire parameter of [[set]], [[add]], [[mset]] and [[madd]] to |
| 25 | + * See [[yii\caching\Cache]] manual for common cache operations that redis Cache supports. |
| 26 | + * |
| 27 | + * Unlike the [[yii\caching\Cache]], redis Cache allows the expire parameter of [[set]], [[add]], [[mset]] and [[madd]] to |
25 | 28 | * be a floating point number, so you may specify the time in milliseconds (e.g. 0.1 will be 100 milliseconds). |
26 | 29 | * |
27 | 30 | * To use redis Cache as the cache application component, configure the application as follows, |
@@ -137,6 +140,15 @@ class Cache extends \yii\caching\Cache |
137 | 140 | * @since 2.0.11 |
138 | 141 | */ |
139 | 142 | public $forceClusterMode; |
| 143 | + /** |
| 144 | + * @var bool whether redis [[Connection::$database|database]] is shared and can contain other data than cache. |
| 145 | + * Setting this to `true` will change [[flush()]] behavior - instead of using [`FLUSHDB`](https://redis.io/commands/flushdb) |
| 146 | + * command, component will iterate through all keys in database and remove only these with matching [[$keyPrefix]]. |
| 147 | + * Note that this will no longer be an atomic operation and it is much less efficient than `FLUSHDB` command. It is |
| 148 | + * recommended to use separate database for cache and leave this value as `false`. |
| 149 | + * @since 2.0.12 |
| 150 | + */ |
| 151 | + public $shareDatabase = false; |
140 | 152 |
|
141 | 153 | /** |
142 | 154 | * @var Connection currently active connection. |
@@ -334,6 +346,17 @@ protected function deleteValue($key) |
334 | 346 | */ |
335 | 347 | protected function flushValues() |
336 | 348 | { |
| 349 | + if ($this->shareDatabase) { |
| 350 | + $cursor = 0; |
| 351 | + do { |
| 352 | + list($cursor, $keys) = $this->redis->scan($cursor, 'MATCH', $this->keyPrefix . '*'); |
| 353 | + $cursor = (int) $cursor; |
| 354 | + $this->redis->executeCommand('DEL', $keys); |
| 355 | + } while ($cursor !== 0); |
| 356 | + |
| 357 | + return true; |
| 358 | + } |
| 359 | + |
337 | 360 | return $this->redis->executeCommand('FLUSHDB'); |
338 | 361 | } |
339 | 362 |
|
|
0 commit comments