MemcachedClient::internalMultiSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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