Completed
Push — master ( 5cfb46...2936f6 )
by Lars
01:39
created

AdapterMemcached::setSettings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

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.4285
c 0
b 0
f 0
cc 2
eloc 9
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
 * @package voku\cache
13
 */
14
class AdapterMemcached implements iAdapter
15
{
16
  /**
17
   * @var bool
18
   */
19
  public $installed = false;
20
21
  /**
22
   * @var \Memcached
23
   */
24
  private $memcached;
25
26
  /**
27
   * __construct
28
   *
29
   * @param \Memcached|null $memcached
30
   */
31 9
  public function __construct($memcached = null)
32
  {
33 9
    if ($memcached instanceof \Memcached) {
34 9
      $this->setMemcached($memcached);
35
    }
36 9
  }
37
38
  /**
39
   * @param \Memcached $memcached
40
   */
41 9
  public function setMemcached(\Memcached $memcached) {
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 5
  public function get(string $key)
60
  {
61 5
    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 9
  private function setSettings()
117
  {
118
    // Use faster compression if available
119 9
    if (\Memcached::HAVE_IGBINARY) {
120
      $this->memcached->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_IGBINARY);
121
    }
122 9
    $this->memcached->setOption(\Memcached::OPT_DISTRIBUTION, \Memcached::DISTRIBUTION_CONSISTENT);
123 9
    $this->memcached->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
124 9
    $this->memcached->setOption(\Memcached::OPT_NO_BLOCK, true);
125 9
    $this->memcached->setOption(\Memcached::OPT_TCP_NODELAY, true);
126 9
    $this->memcached->setOption(\Memcached::OPT_COMPRESSION, false);
127 9
    $this->memcached->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 2);
128 9
  }
129
130
}
131