Memcached::multiGet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\PHPCacheAdapter\SimpleCache;
6
7
use Psr\SimpleCache;
8
use RemotelyLiving\PHPCacheAdapter\Assertions;
9
10
final class Memcached extends AbstractAdapter
11
{
12
    private \Memcached $memcached;
13
14
    private function __construct(\Memcached $memcached)
15
    {
16
        Assertions::assertExtensionLoaded('memcached');
17
        $this->memcached = $memcached;
18
    }
19
20
    public static function create(\Memcached $memcached): SimpleCache\CacheInterface
21
    {
22
        return new self($memcached);
23
    }
24
25
    protected function flush(): bool
26
    {
27
        return $this->memcached->flush();
28
    }
29
30
    protected function exists(string $key): bool
31
    {
32
        return $this->memcached->get($key) !== false;
33
    }
34
35
    /**
36
     * @inheritDoc
37
     */
38
    protected function multiGet(array $keys, $default = null): \Generator
39
    {
40
        foreach ($this->memcached->getMulti($keys, \Memcached::GET_PRESERVE_ORDER) as $key => $value) {
41
            yield $key => $value ?? $default;
42
        }
43
    }
44
45
    protected function multiDelete(array $keys): bool
46
    {
47
        return count($this->memcached->deleteMulti($keys)) === count($keys);
48
    }
49
50
    protected function multiSave(array $values, int $ttl = null): bool
51
    {
52
        return $this->memcached->setMulti($values, (int) $ttl);
53
    }
54
}
55