MemcachedStore::decrement()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Inspirum\Mcrouter\Services;
4
5
use Illuminate\Cache\MemcachedStore as LaravelMemcachedStore;
6
use Illuminate\Cache\TaggedCache;
7
use Inspirum\Mcrouter\Model\Values\Mcrouter;
8
use Inspirum\Mcrouter\Model\Values\TagSet;
9
use Memcached;
10
11
class MemcachedStore extends LaravelMemcachedStore
12
{
13
    /**
14
     * Mcrouter config
15
     *
16
     * @var \Inspirum\Mcrouter\Model\Values\Mcrouter
17
     */
18
    private $mcrouter;
19
20
    /**
21
     * Create a new Memcached store.
22
     *
23
     * @param \Memcached                                    $memcached
24
     * @param string                                        $prefix
25
     * @param \Inspirum\Mcrouter\Model\Values\Mcrouter|null $mcrouter
26
     */
27 20
    public function __construct(Memcached $memcached, string $prefix = '', Mcrouter $mcrouter = null)
28
    {
29 20
        parent::__construct($memcached, $prefix);
30
31 20
        $this->mcrouter = $mcrouter ?: new Mcrouter('');
32 20
    }
33
34
    /**
35
     * Begin executing a new tags operation.
36
     *
37
     * @param string|array<string> $names
38
     *
39
     * @return \Illuminate\Cache\TaggedCache
40
     */
41 3
    public function tags($names)
42
    {
43 3
        return new TaggedCache($this, new TagSet($this, is_array($names) ? $names : func_get_args(), $this->mcrouter));
44
    }
45
46
    /**
47
     * Retrieve an item from the cache by key.
48
     *
49
     * @param string $key
50
     *
51
     * @return mixed
52
     */
53 9
    public function get($key)
54
    {
55 9
        $value = $this->memcached->get($this->getPrefixedKey($key));
56
57 9
        if ($this->memcached->getResultCode() !== 0) {
58 4
            return null;
59
        }
60
61 5
        return $value;
62
    }
63
64
    /**
65
     * Retrieve multiple items from the cache by key.
66
     *
67
     * Items not found in the cache will have a null value.
68
     *
69
     * @param array<string> $keys
70
     *
71
     * @return array<string,mixed>
72
     */
73 2
    public function many(array $keys)
74
    {
75
        $prefixedKeys = array_map(function ($key) {
76 2
            return $this->getPrefixedKey($key);
77 2
        }, $keys);
78
79
        /** @var array<mixed> $values */
80 2
        $values = $this->memcached->getMulti($prefixedKeys, Memcached::GET_PRESERVE_ORDER);
81
82 2
        if ($this->memcached->getResultCode() !== 0) {
83 1
            return array_fill_keys($keys, null);
84
        }
85
86
        /** @var array<mixed> $values */
87 1
        $values = array_combine($keys, $values);
88
89 1
        return $values;
90
    }
91
92
    /**
93
     * Store an item in the cache for a given number of minutes.
94
     *
95
     * @param string $key
96
     * @param mixed  $value
97
     * @param int    $seconds
98
     *
99
     * @return bool
100
     */
101 5
    public function put($key, $value, $seconds)
102
    {
103 5
        return $this->memcached->set($this->getPrefixedKey($key), $value, $this->toTimestamp($seconds));
104
    }
105
106
    /**
107
     * Store multiple items in the cache for a given number of minutes.
108
     *
109
     * @param array<string,mixed> $values
110
     * @param int                 $seconds
111
     *
112
     * @return bool
113
     */
114 1
    public function putMany(array $values, $seconds)
115
    {
116 1
        $prefixedValues = [];
117
118 1
        foreach ($values as $key => $value) {
119 1
            $prefixedValues[$this->getPrefixedKey($key)] = $value;
120
        }
121
122 1
        return $this->memcached->setMulti($prefixedValues, $this->toTimestamp($seconds));
123
    }
124
125
    /**
126
     * Store an item in the cache if the key doesn't exist.
127
     *
128
     * @param string $key
129
     * @param mixed  $value
130
     * @param int    $seconds
131
     *
132
     * @return bool
133
     */
134 1
    public function add($key, $value, $seconds)
135
    {
136 1
        return $this->memcached->add($this->getPrefixedKey($key), $value, $this->toTimestamp($seconds));
137
    }
138
139
    /**
140
     * Increment the value of an item in the cache.
141
     *
142
     * @param string $key
143
     * @param int    $value
144
     *
145
     * @return int|bool
146
     */
147 1
    public function increment($key, $value = 1)
148
    {
149 1
        return $this->memcached->increment($this->getPrefixedKey($key), $value);
150
    }
151
152
    /**
153
     * Decrement the value of an item in the cache.
154
     *
155
     * @param string $key
156
     * @param int    $value
157
     *
158
     * @return int|bool
159
     */
160 1
    public function decrement($key, $value = 1)
161
    {
162 1
        return $this->memcached->decrement($this->getPrefixedKey($key), $value);
163
    }
164
165
    /**
166
     * Remove an item from the cache.
167
     *
168
     * @param string $key
169
     *
170
     * @return bool
171
     */
172 1
    public function forget($key)
173
    {
174 1
        return $this->memcached->delete($this->getPrefixedKey($key));
175
    }
176
177
    /**
178
     * Get the cache key prefix.
179
     *
180
     * @param string $key
181
     *
182
     * @return string
183
     */
184 18
    protected function getPrefixedKey(string $key): string
185
    {
186 18
        $prefixes = $this->mcrouter->getPrefixes();
187
188 18
        foreach ($prefixes as $prefix) {
189 3
            $position = strpos($key, $prefix);
190
191 3
            if ($position !== false) {
192 2
                return substr_replace($key, $this->prefix, $position + strlen($prefix), 0);
193
            }
194
        }
195
196 17
        return $this->prefix . $key;
197
    }
198
}
199