Completed
Pull Request — master (#207)
by Luís
04:10
created

ApcuCache::doFetch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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
/**
23
 * APCu cache provider.
24
 *
25
 * @link   www.doctrine-project.org
26
 * @since  1.6
27
 * @author Kévin Dunglas <[email protected]>
28
 */
29
class ApcuCache extends CacheProvider
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34 74
    protected function doFetch($id)
35
    {
36 74
        return apcu_fetch($id);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 69
    protected function doContains($id)
43
    {
44 69
        return apcu_exists($id);
0 ignored issues
show
Bug Compatibility introduced by
The expression apcu_exists($id); of type boolean|string[] adds the type string[] to the return on line 44 which is incompatible with the return type declared by the abstract method Doctrine\Common\Cache\CacheProvider::doContains of type boolean.
Loading history...
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 72
    protected function doSave($id, $data, $lifeTime = 0)
51
    {
52 72
        return apcu_store($id, $data, $lifeTime);
0 ignored issues
show
Bug Compatibility introduced by
The expression apcu_store($id, $data, $lifeTime); of type boolean|array adds the type array to the return on line 52 which is incompatible with the return type declared by the abstract method Doctrine\Common\Cache\CacheProvider::doSave of type boolean.
Loading history...
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 44
    protected function doDelete($id)
59
    {
60
        // apcu_delete returns false if the id does not exist
61 44
        return apcu_delete($id) || ! apcu_exists($id);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    protected function doDeleteMultiple(array $keys)
68
    {
69 1
        $result = apcu_delete($keys);
70
71 1
        return false !== $result && count($result) !== count($keys);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 2
    protected function doFlush()
78
    {
79 2
        return apcu_clear_cache();
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 2
    protected function doFetchMultiple(array $keys)
86
    {
87 2
        return apcu_fetch($keys) ?: [];
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 1
    protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
94
    {
95 1
        $result = apcu_store($keysAndValues, null, $lifetime);
96
97 1
        return empty($result);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 1
    protected function doGetStats()
104
    {
105 1
        $info = apcu_cache_info(true);
106 1
        $sma  = apcu_sma_info();
107
108
        return [
109 1
            Cache::STATS_HITS             => $info['num_hits'],
110 1
            Cache::STATS_MISSES           => $info['num_misses'],
111 1
            Cache::STATS_UPTIME           => $info['start_time'],
112 1
            Cache::STATS_MEMORY_USAGE     => $info['mem_size'],
113 1
            Cache::STATS_MEMORY_AVAILABLE => $sma['avail_mem'],
114 1
        ];
115
    }
116
}
117