Completed
Push — master ( 9d79d8...280484 )
by Michael
11s
created

RedisCache::doSaveMultiple()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 8.7414

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 2
dl 0
loc 19
ccs 3
cts 9
cp 0.3333
crap 8.7414
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Common\Cache;
4
5
use Redis;
6
use function array_combine;
7
use function defined;
8
use function extension_loaded;
9
use function is_bool;
10
11
/**
12
 * Redis cache provider.
13
 *
14
 * @link   www.doctrine-project.org
15
 */
16
class RedisCache extends CacheProvider
17
{
18
    /** @var Redis|null */
19
    private $redis;
20
21
    /**
22
     * Sets the redis instance to use.
23
     *
24
     * @return void
25
     */
26 81
    public function setRedis(Redis $redis)
27
    {
28 81
        $redis->setOption(Redis::OPT_SERIALIZER, $this->getSerializerValue());
29 81
        $this->redis = $redis;
30 81
    }
31
32
    /**
33
     * Gets the redis instance used by the cache.
34
     *
35
     * @return Redis|null
36
     */
37 2
    public function getRedis()
38
    {
39 2
        return $this->redis;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 76
    protected function doFetch($id)
46
    {
47 76
        return $this->redis->get($id);
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        return $this->redis->/** @scrutinizer ignore-call */ get($id);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2
    protected function doFetchMultiple(array $keys)
54
    {
55 2
        $fetchedItems = array_combine($keys, $this->redis->mget($keys));
56
57
        // Redis mget returns false for keys that do not exist. So we need to filter those out unless it's the real data.
58 2
        $foundItems = [];
59
60 2
        foreach ($fetchedItems as $key => $value) {
61 2
            if ($value === false && ! $this->redis->exists($key)) {
62 1
                continue;
63
            }
64
65 2
            $foundItems[$key] = $value;
66
        }
67
68 2
        return $foundItems;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 1
    protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
75
    {
76 1
        if ($lifetime) {
77
            $success = true;
78
79
            // Keys have lifetime, use SETEX for each of them
80
            foreach ($keysAndValues as $key => $value) {
81
                if ($this->redis->setex($key, $lifetime, $value)) {
82
                    continue;
83
                }
84
85
                $success = false;
86
            }
87
88
            return $success;
89
        }
90
91
        // No lifetime, use MSET
92 1
        return (bool) $this->redis->mset($keysAndValues);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 71
    protected function doContains($id)
99
    {
100 71
        $exists = $this->redis->exists($id);
101
102 71
        if (is_bool($exists)) {
1 ignored issue
show
introduced by
The condition is_bool($exists) is always true.
Loading history...
103 71
            return $exists;
104
        }
105
106
        return $exists > 0;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 74
    protected function doSave($id, $data, $lifeTime = 0)
113
    {
114 74
        if ($lifeTime > 0) {
115 3
            return $this->redis->setex($id, $lifeTime, $data);
116
        }
117
118 72
        return $this->redis->set($id, $data);
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 44
    protected function doDelete($id)
125
    {
126 44
        return $this->redis->delete($id) >= 0;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 1
    protected function doDeleteMultiple(array $keys)
133
    {
134 1
        return $this->redis->delete($keys) >= 0;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 2
    protected function doFlush()
141
    {
142 2
        return $this->redis->flushDB();
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 2
    protected function doGetStats()
149
    {
150 2
        $info = $this->redis->info();
151
        return [
152 2
            Cache::STATS_HITS   => $info['keyspace_hits'],
153 2
            Cache::STATS_MISSES => $info['keyspace_misses'],
154 2
            Cache::STATS_UPTIME => $info['uptime_in_seconds'],
155 2
            Cache::STATS_MEMORY_USAGE      => $info['used_memory'],
156
            Cache::STATS_MEMORY_AVAILABLE  => false,
157
        ];
158
    }
159
160
    /**
161
     * Returns the serializer constant to use. If Redis is compiled with
162
     * igbinary support, that is used. Otherwise the default PHP serializer is
163
     * used.
164
     *
165
     * @return int One of the Redis::SERIALIZER_* constants
166
     */
167 81
    protected function getSerializerValue()
168
    {
169 81
        if (defined('Redis::SERIALIZER_IGBINARY') && extension_loaded('igbinary')) {
170
            return Redis::SERIALIZER_IGBINARY;
171
        }
172
173 81
        return Redis::SERIALIZER_PHP;
174
    }
175
}
176