Passed
Push — master ( 5a87d9...5af04f )
by Mihail
03:04
created

MemcachedClient::delete()   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 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Koded package.
5
 *
6
 * (c) Mihail Binev <[email protected]>
7
 *
8
 * Please view the LICENSE distributed with this source code
9
 * for the full copyright and license information.
10
 *
11
 */
12
13
namespace Koded\Caching\Client;
14
15
use Koded\Caching\Cache;
16
use function Koded\Caching\verify_key;
17
18
/**
19
 * @property \Memcached client
20
 */
21
final class MemcachedClient implements Cache
22
{
23
    use ClientTrait, MultiplesTrait;
24
25 197
    public function __construct(\Memcached $client, int $ttl = null)
26
    {
27 197
        $this->ttl = $ttl;
28 197
        $this->client = $client;
29 197
    }
30
31
32 52
    public function get($key, $default = null)
33
    {
34 52
        verify_key($key);
35
36
        // Cannot return get() directly because default value
37 35
        $value = $this->client->get($key);
38
39 35
        return \Memcached::RES_SUCCESS === $this->client->getResultCode() ? $value : $default;
40
    }
41
42
43 60
    public function set($key, $value, $ttl = null)
44
    {
45 60
        verify_key($key);
46 43
        $expiration = $this->secondsWithGlobalTtl($ttl);
47
48 33
        if (null !== $ttl && $expiration < 1) {
49 2
            $this->client->delete($key);
50 2
            return true;
51
        }
52
53 32
        return $this->client->set($key, $value, $expiration);
54
    }
55
56
57 20
    public function delete($key)
58
    {
59 20
        if (false === $this->has($key)) {
60 2
            return true;
61
        }
62
63 2
        return $this->client->delete($key);
64
    }
65
66
67 195
    public function clear()
68
    {
69 195
        return $this->client->flush();
70
    }
71
72
73 42
    public function has($key)
74
    {
75 42
        verify_key($key);
76
77
        // Memcached does not have exists() or similar method
78 8
        $this->client->get($key);
79
80 8
        return \Memcached::RES_NOTFOUND !== $this->client->getResultCode();
81
    }
82
83
    /*
84
     *
85
     * Overrides
86
     *
87
     */
88
89 12
    protected function internalMultiGet(array $keys, $default = null): array
90
    {
91 12
        return array_replace(array_fill_keys($keys, $default), $this->client->getMulti($keys) ?: []);
92
    }
93
94
95 15
    protected function internalMultiSet(array $values, $ttl = null): bool
96
    {
97 15
        return $this->client->setMulti($values, $ttl);
98
    }
99
100
101 4
    protected function internalMultiDelete(array $keys): bool
102
    {
103 4
        $this->client->deleteMulti($keys);
104
105 4
        return \Memcached::RES_FAILURE !== $this->client->getResultCode();
106
    }
107
}
108