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

ApcuCache::doGetStats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 11
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Common\Cache;
4
5
use function apcu_cache_info;
6
use function apcu_clear_cache;
7
use function apcu_delete;
8
use function apcu_exists;
9
use function apcu_fetch;
10
use function apcu_sma_info;
11
use function apcu_store;
12
use function count;
13
14
/**
15
 * APCu cache provider.
16
 *
17
 * @link   www.doctrine-project.org
18
 */
19
class ApcuCache extends CacheProvider
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected function doFetch($id)
25
    {
26
        return apcu_fetch($id);
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function doContains($id)
33
    {
34 75
        return apcu_exists($id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apcu_exists($id) also could return the type string[] which is incompatible with the return type mandated by Doctrine\Common\Cache\CacheProvider::doContains() of boolean.
Loading history...
35
    }
36 75
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function doSave($id, $data, $lifeTime = 0)
41
    {
42 70
        return apcu_store($id, $data, $lifeTime);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apcu_store($id, $data, $lifeTime) also could return the type array which is incompatible with the return type mandated by Doctrine\Common\Cache\CacheProvider::doSave() of boolean.
Loading history...
43
    }
44 70
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function doDelete($id)
49
    {
50 73
        // apcu_delete returns false if the id does not exist
51
        return apcu_delete($id) || ! apcu_exists($id);
52 73
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function doDeleteMultiple(array $keys)
58 44
    {
59
        $result = apcu_delete($keys);
60
61 44
        return $result !== false && count($result) !== count($keys);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

61
        return $result !== false && count(/** @scrutinizer ignore-type */ $result) !== count($keys);
Loading history...
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    protected function doFlush()
68
    {
69 1
        return apcu_clear_cache();
70
    }
71 1
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function doFetchMultiple(array $keys)
76
    {
77 2
        return apcu_fetch($keys) ?: [];
78
    }
79 2
80
    /**
81
     * {@inheritdoc}
82
     */
83
    protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
84
    {
85 2
        $result = apcu_store($keysAndValues, null, $lifetime);
86
87 2
        return empty($result);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 1
    protected function doGetStats()
94
    {
95 1
        $info = apcu_cache_info(true);
96
        $sma  = apcu_sma_info();
97 1
98
        return [
99
            Cache::STATS_HITS             => $info['num_hits'],
100
            Cache::STATS_MISSES           => $info['num_misses'],
101
            Cache::STATS_UPTIME           => $info['start_time'],
102
            Cache::STATS_MEMORY_USAGE     => $info['mem_size'],
103 1
            Cache::STATS_MEMORY_AVAILABLE => $sma['avail_mem'],
104
        ];
105 1
    }
106
}
107