MemcacheCache::doFetch()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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
 * @deprecated
12
 *
13
 * @link   www.doctrine-project.org
14
 */
15
class MemcacheCache extends CacheProvider
16
{
17
    /** @var Memcache|null */
18
    private $memcache;
19
20
    /**
21
     * Sets the memcache instance to use.
22
     *
23
     * @return void
24
     */
25
    public function setMemcache(Memcache $memcache)
26
    {
27
        $this->memcache = $memcache;
28
    }
29
30
    /**
31
     * Gets the memcache instance used by the cache.
32
     *
33
     * @return Memcache|null
34
     */
35
    public function getMemcache()
36
    {
37
        return $this->memcache;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function doFetch($id)
44
    {
45
        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

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