Completed
Push — master ( b7f9bf...48ba4b )
by Lars
02:40
created

AdapterMemcached::setSettings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 10
cp 0.9
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.004
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
use voku\cache\Exception\InvalidArgumentException;
8
9
/**
10
 * AdapterMemcached: Memcached-adapter
11
 */
12
class AdapterMemcached implements iAdapter
13
{
14
  /**
15
   * @var bool
16
   */
17
  public $installed = false;
18
19
  /**
20
   * @var \Memcached
21
   */
22
  private $memcached;
23
24
  /**
25
   * __construct
26
   *
27
   * @param \Memcached|null $memcached
28
   */
29 9
  public function __construct($memcached = null)
30
  {
31 9
    if ($memcached instanceof \Memcached) {
32 9
      $this->setMemcached($memcached);
33
    }
34 9
  }
35
36
  /**
37
   * @param \Memcached $memcached
38
   */
39 9
  public function setMemcached(\Memcached $memcached)
40
  {
41 9
    $this->memcached = $memcached;
42 9
    $this->installed = true;
43
44 9
    $this->setSettings();
45 9
  }
46
47
  /**
48
   * @inheritdoc
49
   */
50 2
  public function exists(string $key): bool
51
  {
52 2
    return $this->get($key) !== false;
53
  }
54
55
  /**
56
   * @inheritdoc
57
   */
58 6
  public function get(string $key)
59
  {
60 6
    return $this->memcached->get($key);
61
  }
62
63
  /**
64
   * @inheritdoc
65
   */
66 9
  public function installed(): bool
67
  {
68 9
    return $this->installed;
69
  }
70
71
  /**
72
   * @inheritdoc
73
   */
74
  public function remove(string $key): bool
75
  {
76
    return $this->memcached->delete($key);
77
  }
78
79
  /**
80
   * @inheritdoc
81
   */
82
  public function removeAll(): bool
83
  {
84
    return $this->memcached->flush();
85
  }
86
87
  /**
88
   * @inheritdoc
89
   */
90 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...
91
  {
92
    // Make sure we are under the proper limit
93 3
    if (\strlen($this->memcached->getOption(\Memcached::OPT_PREFIX_KEY) . $key) > 250) {
94
      throw new InvalidArgumentException('The passed cache key is over 250 bytes:' . print_r($key, true));
95
    }
96
97 3
    return $this->memcached->set($key, $value);
98
  }
99
100
  /**
101
   * @inheritdoc
102
   */
103 1
  public function setExpired(string $key, $value, int $ttl = 0): bool
104
  {
105 1
    if ($ttl > 2592000) {
106
      $ttl = 2592000;
107
    }
108
109 1
    return $this->memcached->set($key, $value, $ttl);
110
  }
111
112
  /**
113
   * Set the MemCached settings.
114
   */
115 9
  private function setSettings()
116
  {
117
    // Use faster compression if available
118 9
    if (\Memcached::HAVE_IGBINARY) {
119
      $this->memcached->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_IGBINARY);
120
    }
121 9
    $this->memcached->setOption(\Memcached::OPT_DISTRIBUTION, \Memcached::DISTRIBUTION_CONSISTENT);
122 9
    $this->memcached->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
123 9
    $this->memcached->setOption(\Memcached::OPT_NO_BLOCK, true);
124 9
    $this->memcached->setOption(\Memcached::OPT_TCP_NODELAY, true);
125 9
    $this->memcached->setOption(\Memcached::OPT_COMPRESSION, false);
126 9
    $this->memcached->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 2);
127 9
  }
128
129
}
130