Completed
Pull Request — master (#220)
by Luís
03:30
created

PredisCache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\Common\Cache;
21
22
use Predis\ClientInterface;
23
24
/**
25
 * Predis cache provider.
26
 *
27
 * @author othillo <[email protected]>
28
 */
29
class PredisCache extends CacheProvider
30
{
31
    /**
32
     * @var ClientInterface
33
     */
34
    private $client;
35
36
    /**
37
     * @param ClientInterface $client
38
     *
39
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

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