PredisCache::doSaveMultiple()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 7.1753

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 2
dl 0
loc 25
ccs 5
cts 12
cp 0.4167
crap 7.1753
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Common\Cache;
4
5
use Predis\ClientInterface;
6
use function array_combine;
7
use function array_filter;
8
use function array_map;
9
use function call_user_func_array;
10
use function serialize;
11
use function unserialize;
12
13
/**
14
 * Predis cache provider.
15
 */
16
class PredisCache extends CacheProvider
17
{
18
    /** @var ClientInterface */
19
    private $client;
20
21 78
    public function __construct(ClientInterface $client)
22
    {
23 78
        $this->client = $client;
24 78
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 74
    protected function doFetch($id)
30
    {
31 74
        $result = $this->client->get($id);
32 74
        if ($result === null) {
33 64
            return false;
34
        }
35
36 69
        return unserialize($result);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 2
    protected function doFetchMultiple(array $keys)
43
    {
44 2
        $fetchedItems = call_user_func_array([$this->client, 'mget'], $keys);
45
46 2
        return array_map('unserialize', array_filter(array_combine($keys, $fetchedItems)));
0 ignored issues
show
Bug introduced by
It seems like array_combine($keys, $fetchedItems) can also be of type false; however, parameter $input of array_filter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

46
        return array_map('unserialize', array_filter(/** @scrutinizer ignore-type */ array_combine($keys, $fetchedItems)));
Loading history...
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 1
    protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
53
    {
54 1
        if ($lifetime) {
55
            $success = true;
56
57
            // Keys have lifetime, use SETEX for each of them
58
            foreach ($keysAndValues as $key => $value) {
59
                $response = (string) $this->client->setex($key, $lifetime, serialize($value));
60
61
                if ($response == 'OK') {
62
                    continue;
63
                }
64
65
                $success = false;
66
            }
67
68
            return $success;
69
        }
70
71
        // No lifetime, use MSET
72
        $response = $this->client->mset(array_map(static function ($value) {
73 1
            return serialize($value);
74 1
        }, $keysAndValues));
75
76 1
        return (string) $response == 'OK';
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 69
    protected function doContains($id)
83
    {
84 69
        return (bool) $this->client->exists($id);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 72
    protected function doSave($id, $data, $lifeTime = 0)
91
    {
92 72
        $data = serialize($data);
93 72
        if ($lifeTime > 0) {
94 3
            $response = $this->client->setex($id, $lifeTime, $data);
95
        } else {
96 70
            $response = $this->client->set($id, $data);
97
        }
98
99 72
        return $response === true || $response == 'OK';
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 43
    protected function doDelete($id)
106
    {
107 43
        return $this->client->del($id) >= 0;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 1
    protected function doDeleteMultiple(array $keys)
114
    {
115 1
        return $this->client->del($keys) >= 0;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 2
    protected function doFlush()
122
    {
123 2
        $response = $this->client->flushdb();
124
125 2
        return $response === true || $response == 'OK';
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 2
    protected function doGetStats()
132
    {
133 2
        $info = $this->client->info();
134
135
        return [
136 2
            Cache::STATS_HITS              => $info['Stats']['keyspace_hits'],
137 2
            Cache::STATS_MISSES            => $info['Stats']['keyspace_misses'],
138 2
            Cache::STATS_UPTIME            => $info['Server']['uptime_in_seconds'],
139 2
            Cache::STATS_MEMORY_USAGE      => $info['Memory']['used_memory'],
140
            Cache::STATS_MEMORY_AVAILABLE  => false,
141
        ];
142
    }
143
}
144