Completed
Push — master ( 9fa596...e14f7e )
by Marco
02:55
created

MemcachedCache   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95.12%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 121
ccs 39
cts 41
cp 0.9512
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setMemcached() 0 4 1
A getMemcached() 0 4 1
A doFetch() 0 4 1
A doSave() 0 7 2
A doSaveMultiple() 0 8 2
A doContains() 0 5 2
A doFetchMultiple() 0 4 2
A doDeleteMultiple() 0 5 2
A doDelete() 0 5 2
A doFlush() 0 4 1
A doGetStats() 0 14 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\Common\Cache;
21
22
use \Memcached;
23
24
/**
25
 * Memcached cache provider.
26
 *
27
 * @link   www.doctrine-project.org
28
 * @since  2.2
29
 * @author Benjamin Eberlei <[email protected]>
30
 * @author Guilherme Blanco <[email protected]>
31
 * @author Jonathan Wage <[email protected]>
32
 * @author Roman Borschel <[email protected]>
33
 * @author David Abdemoulaie <[email protected]>
34
 */
35
class MemcachedCache extends CacheProvider
36
{
37
    /**
38
     * @var Memcached|null
39
     */
40
    private $memcached;
41
42
    /**
43
     * Sets the memcache instance to use.
44
     *
45
     * @param Memcached $memcached
46
     *
47
     * @return void
48
     */
49 75
    public function setMemcached(Memcached $memcached)
50
    {
51 75
        $this->memcached = $memcached;
52 75
    }
53
54
    /**
55
     * Gets the memcached instance used by the cache.
56
     *
57
     * @return Memcached|null
58
     */
59 1
    public function getMemcached()
60
    {
61 1
        return $this->memcached;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 72
    protected function doFetch($id)
68
    {
69 72
        return $this->memcached->get($id);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 2
    protected function doFetchMultiple(array $keys)
76
    {
77 2
        return $this->memcached->getMulti($keys) ?: [];
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 1
    protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
84
    {
85 1
        if ($lifetime > 30 * 24 * 3600) {
86
            $lifetime = time() + $lifetime;
87
        }
88
89 1
        return $this->memcached->setMulti($keysAndValues, $lifetime);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 67
    protected function doContains($id)
96
    {
97 67
        return false !== $this->memcached->get($id)
98 67
            || $this->memcached->getResultCode() !== Memcached::RES_NOTFOUND;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 70
    protected function doSave($id, $data, $lifeTime = 0)
105
    {
106 70
        if ($lifeTime > 30 * 24 * 3600) {
107 1
            $lifeTime = time() + $lifeTime;
108 1
        }
109 70
        return $this->memcached->set($id, $data, (int) $lifeTime);
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 1
    protected function doDeleteMultiple(array $keys)
116
    {
117 1
        return $this->memcached->deleteMulti($keys)
0 ignored issues
show
Bug introduced by
The method deleteMulti() does not exist on Memcached. Did you maybe mean delete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
118 1
            || $this->memcached->getResultCode() === Memcached::RES_NOTFOUND;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 41
    protected function doDelete($id)
125
    {
126 41
        return $this->memcached->delete($id)
127 41
            || $this->memcached->getResultCode() === Memcached::RES_NOTFOUND;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 2
    protected function doFlush()
134
    {
135 2
        return $this->memcached->flush();
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 1
    protected function doGetStats()
142
    {
143 1
        $stats   = $this->memcached->getStats();
144 1
        $servers = $this->memcached->getServerList();
145 1
        $key     = $servers[0]['host'] . ':' . $servers[0]['port'];
146 1
        $stats   = $stats[$key];
147
        return [
148 1
            Cache::STATS_HITS   => $stats['get_hits'],
149 1
            Cache::STATS_MISSES => $stats['get_misses'],
150 1
            Cache::STATS_UPTIME => $stats['uptime'],
151 1
            Cache::STATS_MEMORY_USAGE     => $stats['bytes'],
152 1
            Cache::STATS_MEMORY_AVAILABLE => $stats['limit_maxbytes'],
153 1
        ];
154
    }
155
}
156