Completed
Push — master ( 99f77d...5621e4 )
by Lars
02:23
created

AdapterMemcached::setSettings()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.074

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 10
cts 12
cp 0.8333
rs 9.7666
c 0
b 0
f 0
cc 4
nc 3
nop 0
crap 4.074
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
use Memcached;
8
use voku\cache\Exception\InvalidArgumentException;
9
10
/**
11
 * AdapterMemcached: Memcached-adapter
12
 */
13
class AdapterMemcached implements iAdapter
14
{
15
    /**
16
     * @var bool
17
     */
18
    public $installed = false;
19
20
    /**
21
     * @var Memcached
22
     */
23
    private $memcached;
24
25
    /**
26
     * __construct
27
     *
28
     * @param Memcached|null $memcached
29
     */
30 9
    public function __construct($memcached = null)
31
    {
32 9
        if ($memcached instanceof Memcached) {
33 9
            $this->setMemcached($memcached);
34
        }
35 9
    }
36
37
    /**
38
     * @param Memcached $memcached
39
     */
40 9
    public function setMemcached(Memcached $memcached)
41
    {
42 9
        $this->memcached = $memcached;
43 9
        $this->installed = true;
44
45 9
        $this->setSettings();
46 9
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 2
    public function exists(string $key): bool
52
    {
53 2
        return $this->get($key) !== false;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 6
    public function get(string $key)
60
    {
61 6
        return $this->memcached->get($key);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 9
    public function installed(): bool
68
    {
69 9
        return $this->installed;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function remove(string $key): bool
76
    {
77
        return $this->memcached->delete($key);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function removeAll(): bool
84
    {
85
        return $this->memcached->flush();
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 3 View Code Duplication
    public function set(string $key, $value): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93
        // Make sure we are under the proper limit
94 3
        if (\strlen($this->memcached->getOption(Memcached::OPT_PREFIX_KEY) . $key) > 250) {
95
            throw new InvalidArgumentException('The passed cache key is over 250 bytes:' . \print_r($key, true));
96
        }
97
98 3
        return $this->memcached->set($key, $value);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 1
    public function setExpired(string $key, $value, int $ttl = 0): bool
105
    {
106 1
        if ($ttl > 2592000) {
107
            $ttl = 2592000;
108
        }
109
110 1
        return $this->memcached->set($key, $value, $ttl);
111
    }
112
113
    /**
114
     * Set the MemCached settings.
115
     *
116
     * @noinspection PhpUndefinedClassConstantInspection -> MSGPACK is not added into phpstorm stubs
117
     */
118 9
    private function setSettings()
119
    {
120
        // Use faster compression if available
121 9
        if (Memcached::HAVE_IGBINARY) {
122
            $this->memcached->setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY);
123 9
        } elseif (defined('Memcached::HAVE_MSGPACK') && Memcached::HAVE_MSGPACK) {
124
            $this->memcached->setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_MSGPACK);
125
        }
126 9
        $this->memcached->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
127 9
        $this->memcached->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
128 9
        $this->memcached->setOption(Memcached::OPT_NO_BLOCK, true);
129 9
        $this->memcached->setOption(Memcached::OPT_TCP_NODELAY, true);
130 9
        $this->memcached->setOption(Memcached::OPT_COMPRESSION, false);
131 9
        $this->memcached->setOption(Memcached::OPT_CONNECT_TIMEOUT, 2);
132 9
    }
133
}
134