Redis::purge()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
11
 * THE SOFTWARE.
12
 *
13
 * This software consists of voluntary contributions made by many individuals
14
 * and is licensed under the MIT license.
15
 *
16
 * Copyright (c) 2014-2019 Yuuki Takezawa
17
 *
18
 */
19
20
namespace Ytake\LaravelSmarty\Cache;
21
22
use Predis\Client;
23
24
/**
25
 * Class Redis
26
 *
27
 * @author yuuki.takezawa<[email protected]>
28
 * @license http://opensource.org/licenses/MIT MIT
29
 */
30
class Redis extends KeyValueStorage
31
{
32
    /** @var Client */
33
    protected $redis;
34
35
    /**
36
     * @param array $servers
37
     */
38
    public function __construct(array $servers)
39
    {
40
        if (count($servers) === 1) {
41
            $this->redis = new Client($servers[0]);
42
        } else {
43
            $this->redis = new Client($servers);
44
        }
45
    }
46
47
    /**
48
     * Read values for a set of keys from cache
49
     *
50
     * @param  array $keys list of keys to fetch
51
     *
52
     * @return array   list of values with the given keys used as indexes
53
     */
54
    protected function read(array $keys)
55
    {
56
        $map = $lookup = [];
57
        list($map, $lookup) = $this->eachKeys($keys, $map, $lookup);
58
        $result = [];
59
        foreach ($map as $key) {
60
            $result[$lookup[$key]] = $this->redis->get($key);
61
        }
62
        return $result;
63
    }
64
65
    /**
66
     * Save values for a set of keys to cache
67
     *
68
     * @param  array $keys list of values to save
69
     * @param  int   $expire expiration time
70
     *
71
     * @return bool true on success, false on failure
72
     */
73
    protected function write(array $keys, $expire = 1)
74
    {
75
        foreach ($keys as $k => $v) {
76
            $k = sha1($k);
77
            $this->redis->setex($k, $expire, $v);
78
        }
79
        return true;
80
    }
81
82
    /**
83
     * Remove values from cache
84
     *
85
     * @param  array $keys list of keys to delete
86
     *
87
     * @return bool true on success, false on failure
88
     */
89
    protected function delete(array $keys)
90
    {
91
        foreach ($keys as $k) {
92
            $k = sha1($k);
93
            $this->redis->del($k);
94
        }
95
        return true;
96
    }
97
98
    /**
99
     * Remove *all* values from cache
100
     *
101
     * @return bool true on success, false on failure
102
     */
103
    protected function purge()
104
    {
105
        $this->redis->flushdb();
106
    }
107
}
108