Passed
Push — master ( b2988e...9f380c )
by Jonathan
33:05
created

MemcacheCache::doFetch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Common\Cache;
4
5
use Memcache;
6
use function time;
7
8
/**
9
 * Memcache cache provider.
10
 *
11
 * @link   www.doctrine-project.org
12
 */
13
class MemcacheCache extends CacheProvider
14
{
15
    /** @var Memcache|null */
16
    private $memcache;
17
18
    /**
19
     * Sets the memcache instance to use.
20
     *
21
     * @return void
22
     */
23
    public function setMemcache(Memcache $memcache)
24
    {
25
        $this->memcache = $memcache;
26
    }
27
28
    /**
29
     * Gets the memcache instance used by the cache.
30
     *
31
     * @return Memcache|null
32
     */
33
    public function getMemcache()
34
    {
35
        return $this->memcache;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function doFetch($id)
42
    {
43
        return $this->memcache->get($id);
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        return $this->memcache->/** @scrutinizer ignore-call */ get($id);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected function doContains($id)
50
    {
51
        $flags = null;
52
        $this->memcache->get($id, $flags);
53
54
        //if memcache has changed the value of "flags", it means the value exists
55
        return $flags !== null;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function doSave($id, $data, $lifeTime = 0)
62
    {
63
        if ($lifeTime > 30 * 24 * 3600) {
64
            $lifeTime = time() + $lifeTime;
65
        }
66
        return $this->memcache->set($id, $data, 0, (int) $lifeTime);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function doDelete($id)
73
    {
74
        // Memcache::delete() returns false if entry does not exist
75
        return $this->memcache->delete($id) || ! $this->doContains($id);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    protected function doFlush()
82
    {
83
        return $this->memcache->flush();
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    protected function doGetStats()
90
    {
91
        $stats = $this->memcache->getStats();
92
        return [
93
            Cache::STATS_HITS   => $stats['get_hits'],
94
            Cache::STATS_MISSES => $stats['get_misses'],
95
            Cache::STATS_UPTIME => $stats['uptime'],
96
            Cache::STATS_MEMORY_USAGE     => $stats['bytes'],
97
            Cache::STATS_MEMORY_AVAILABLE => $stats['limit_maxbytes'],
98
        ];
99
    }
100
}
101