Passed
Pull Request — master (#251)
by Gabriel
10:21
created

RedisCache::doSave()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 3
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
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
10
/**
11
 * Redis cache provider.
12
 *
13
 * @link   www.doctrine-project.org
14
 */
15
class RedisCache extends CacheProvider
16
{
17
    /** @var Redis|null */
18
    private $redis;
19
20
    /**
21
     * Sets the redis instance to use.
22
     *
23
     * @return void
24
     */
25 81
    public function setRedis(Redis $redis)
26
    {
27 81
        $redis->setOption(Redis::OPT_SERIALIZER, $this->getSerializerValue());
28 81
        $this->redis = $redis;
29 81
    }
30
31
    /**
32
     * Gets the redis instance used by the cache.
33
     *
34
     * @return Redis|null
35
     */
36 2
    public function getRedis()
37
    {
38 2
        return $this->redis;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 76
    protected function doFetch($id)
45
    {
46 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

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