Completed
Push — master ( ea3a48...b57255 )
by Lars
02:48
created

AdapterMemcached::removeAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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
   * set Memcached settings
39
   */
40 9
  private function setSettings()
41
  {
42
    // Use faster compression if available
43 9
    if (\Memcached::HAVE_IGBINARY) {
44
      $this->memcached->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_IGBINARY);
45
    }
46 9
    $this->memcached->setOption(\Memcached::OPT_DISTRIBUTION, \Memcached::DISTRIBUTION_CONSISTENT);
47 9
    $this->memcached->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
48 9
    $this->memcached->setOption(\Memcached::OPT_NO_BLOCK, true);
49 9
    $this->memcached->setOption(\Memcached::OPT_TCP_NODELAY, true);
50 9
    $this->memcached->setOption(\Memcached::OPT_COMPRESSION, false);
51 9
    $this->memcached->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 2);
52 9
  }
53
54
  /**
55
   * set cache-item by key => value
56
   *
57
   * @param string $key
58
   * @param mixed  $value
59
   *
60
   * @return mixed|void
61
   */
62 3
  public function set($key, $value)
63
  {
64
    // Make sure we are under the proper limit
65
    /*
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...
66
    if (strlen($this->memcached->getOption(\Memcached::OPT_PREFIX_KEY) . $key) > 250) {
67
      throw new \Exception('The passed cache key is over 250 bytes');
68
    }
69
    */
70
71 3
    return $this->memcached->set($key, $value);
72
  }
73
74
  /**
75
   * set cache-item by key => value + ttl
76
   *
77
   * @param string $key
78
   * @param mixed  $value
79
   * @param int    $ttl
80
   *
81
   * @return boolean
82
   */
83 1
  public function setExpired($key, $value, $ttl)
84
  {
85 1
    if ($ttl > 2592000) {
86
      $ttl = 2592000;
87
    }
88
89 1
    return $this->memcached->set($key, $value, $ttl);
90
  }
91
92
  /**
93
   * remove cached-item by key
94
   *
95
   * @param string $key
96
   *
97
   * @return boolean
98
   */
99
  public function remove($key)
100
  {
101
    return $this->memcached->delete($key);
102
  }
103
104
  /**
105
   * remove all cached items
106
   *
107
   * @return bool
108
   */
109
  public function removeAll()
110
  {
111
    return $this->memcached->flush();
112
  }
113
114
  /**
115
   * check if cached-item exists
116
   *
117
   * @param string $key
118
   *
119
   * @return bool
120
   */
121 2
  public function exists($key)
122
  {
123 2
    return $this->get($key) !== false;
124
  }
125
126
  /**
127
   * get cached-item by key
128
   *
129
   * @param String $key
130
   *
131
   * @return mixed
132
   */
133 6 View Code Duplication
  public function get($key)
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...
134
  {
135 6
    static $memcachedCache;
136
137 6
    if (isset($memcachedCache[$key])) {
138 2
      return $memcachedCache[$key];
139
    } else {
140 4
      $return = $this->memcached->get($key);
141 4
      $memcachedCache[$key] = $return;
142 4
      return $return;
143
    }
144
  }
145
146
  /**
147
   * check if cache is installed
148
   *
149
   * @return boolean
150
   */
151 9
  public function installed()
152
  {
153 9
    return $this->installed;
154
  }
155
156
}
157