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

AdapterMemcache::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
 * AdapterMemcache: Memcache-adapter
7
 *
8
 * @package   voku\cache
9
 */
10
class AdapterMemcache implements iAdapter
11
{
12
  /**
13
   * @var bool
14
   */
15
  public $installed = false;
16
17
  /**
18
   * @var \Memcache
19
   */
20
  private $memcache;
21
22
  /**
23
   * @var boolean
24
   */
25
  private $compressed = false;
26
27
  /**
28
   * __construct
29
   *
30
   * @param \Memcache $memcache
31
   */
32 6
  public function __construct($memcache)
33
  {
34 6
    if ($memcache instanceof \Memcache) {
0 ignored issues
show
Bug introduced by
The class Memcache 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...
35 6
      $this->memcache = $memcache;
36 6
      $this->installed = true;
37 6
    }
38 6
  }
39
40
  /**
41
   * set cache-item by key => value
42
   *
43
   * @param string $key
44
   * @param mixed  $value
45
   *
46
   * @return mixed|void
47
   */
48 2
  public function set($key, $value)
49
  {
50 2
    return $this->memcache->set($key, $value, $this->getCompressedFlag());
51
  }
52
53
  /**
54
   * get compressed-flag
55
   *
56
   * @return int 2 || 0
57
   */
58 3
  private function getCompressedFlag()
59
  {
60 3
    return $this->isCompressed() ? MEMCACHE_COMPRESSED : 0;
61
  }
62
63
  /**
64
   * check if is compressed
65
   *
66
   * @return boolean
67
   */
68 3
  public function isCompressed()
69
  {
70 3
    return $this->compressed;
71
  }
72
73
  /**
74
   * set compressed
75
   *
76
   * @param mixed $value will be converted to boolean
77
   */
78
  public function setCompressed($value)
79
  {
80
    $this->compressed = (bool)$value;
81
  }
82
83
  /**
84
   * set cache-item by key => value + ttl
85
   *
86
   * @param string $key
87
   * @param mixed  $value
88
   * @param int    $ttl
89
   *
90
   * @return mixed|void
91
   */
92 1
  public function setExpired($key, $value, $ttl)
93
  {
94 1
    if ($ttl > 2592000) {
95
      $ttl = 2592000;
96
    }
97
98 1
    return $this->memcache->set($key, $value, $this->getCompressedFlag(), $ttl);
99
  }
100
101
  /**
102
   * remove cached-item by key
103
   *
104
   * @param string $key
105
   *
106
   * @return mixed|void
107
   */
108
  public function remove($key)
109
  {
110
    return $this->memcache->delete($key);
111
  }
112
113
  /**
114
   * remove all cached items
115
   *
116
   * @return bool
117
   */
118
  public function removeAll()
119
  {
120
    return $this->memcache->flush();
121
  }
122
123
  /**
124
   * check if cached-item exists
125
   *
126
   * @param string $key
127
   *
128
   * @return bool
129
   */
130 1
  public function exists($key)
131
  {
132 1
    return $this->get($key) !== false;
133
  }
134
135
  /**
136
   * get cached-item by key
137
   *
138
   * @param String $key
139
   *
140
   * @return mixed
141
   */
142 4 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...
143
  {
144 4
    static $memcachedCache;
145
146 4
    if (isset($memcachedCache[$key])) {
147 1
      return $memcachedCache[$key];
148
    } else {
149 3
      $return = $this->memcache->get($key);
150 3
      $memcachedCache[$key] = $return;
151 3
      return $return;
152
    }
153
  }
154
155
  /**
156
   * check if cache is installed
157
   *
158
   * @return boolean
159
   */
160 6
  public function installed()
161
  {
162 6
    return $this->installed;
163
  }
164
165
}
166