Completed
Push — master ( b3fbb6...d0db0e )
by Lars
02:47
created

AdapterMemcached::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.7462

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 3
cts 7
cp 0.4286
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2.7462
1
<?php
2
3
namespace voku\cache;
4
5
/**
6
 * AdapterMemcached: Memcached-adapter
7
 *
8
 * @package   voku\cache
9
 */
10
class AdapterMemcached implements iAdapter
11
{
12
  /**
13
   * @var bool
14
   */
15
  public $installed = false;
16
17
  /**
18
   * @var \Memcached
19
   */
20
  private $memcached;
21
22
  /**
23
   * __construct
24
   *
25
   * @param \Memcached $memcached
26
   */
27 9
  public function __construct($memcached)
28
  {
29 9
    if ($memcached instanceof \Memcached) {
0 ignored issues
show
Bug introduced by
The class Memcached does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
30 9
      $this->memcached = $memcached;
31 9
      $this->installed = true;
32
33 9
      $this->setSettings();
34 9
    }
35 9
  }
36
37
  /**
38
   * @inheritdoc
39
   */
40 2
  public function exists($key)
41
  {
42 2
    return $this->get($key) !== false;
43
  }
44
45
  /**
46
   * @inheritdoc
47
   */
48 6
  public function get($key)
49
  {
50 6
    static $memcachedCache;
51
52 6
    if (array_key_exists($key, $memcachedCache) === true) {
53
      return $memcachedCache[$key];
54
    } else {
55
      $return = $this->memcached->get($key);
56
      $memcachedCache[$key] = $return;
57
58
      return $return;
59
    }
60
  }
61
62
  /**
63
   * @inheritdoc
64
   */
65 9
  public function installed()
66
  {
67 9
    return $this->installed;
68
  }
69
70
  /**
71
   * @inheritdoc
72
   */
73
  public function remove($key)
74
  {
75
    return $this->memcached->delete($key);
76
  }
77
78
  /**
79
   * @inheritdoc
80
   */
81
  public function removeAll()
82
  {
83
    return $this->memcached->flush();
84
  }
85
86
  /**
87
   * @inheritdoc
88
   */
89 3
  public function set($key, $value)
90
  {
91
    // Make sure we are under the proper limit
92
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
93
    if (strlen($this->memcached->getOption(\Memcached::OPT_PREFIX_KEY) . $key) > 250) {
94
      throw new \Exception('The passed cache key is over 250 bytes');
95
    }
96
    */
97
98 3
    return $this->memcached->set($key, $value);
99
  }
100
101
  /**
102
   * @inheritdoc
103
   */
104 1
  public function setExpired($key, $value, $ttl)
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