Completed
Pull Request — master (#340)
by
unknown
34:53
created

ApcuCache::doFetchAtomic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 3
dl 0
loc 7
ccs 1
cts 1
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Common\Cache;
4
5
use RuntimeException;
6
use function apcu_cache_info;
7
use function apcu_clear_cache;
8
use function apcu_delete;
9
use function apcu_entry;
10
use function apcu_exists;
11
use function apcu_fetch;
12
use function apcu_sma_info;
13
use function apcu_store;
14
use function count;
15
use function function_exists;
16
17
/**
18
 * APCu cache provider.
19
 *
20
 * @link   www.doctrine-project.org
21
 */
22
class ApcuCache extends CacheProvider
23
{
24 75
    /**
25
     * {@inheritdoc}
26 75
     */
27
    protected function doFetch($id)
28
    {
29
        return apcu_fetch($id);
30
    }
31
32 70
    /**
33
     * {@inheritdoc}
34 70
     */
35
    protected function doContains($id)
36
    {
37
        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...
38
    }
39
40 73
    /**
41
     * {@inheritdoc}
42 73
     */
43
    protected function doSave($id, $data, $lifeTime = 0)
44
    {
45
        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...
46
    }
47
48 44
    /**
49
     * {@inheritdoc}
50
     */
51 44
    protected function doDelete($id)
52
    {
53
        // apcu_delete returns false if the id does not exist
54
        return apcu_delete($id) || ! apcu_exists($id);
55
    }
56
57 1
    /**
58
     * {@inheritdoc}
59 1
     */
60
    protected function doDeleteMultiple(array $keys)
61 1
    {
62
        $result = apcu_delete($keys);
63
64
        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

64
        return $result !== false && count(/** @scrutinizer ignore-type */ $result) !== count($keys);
Loading history...
65
    }
66
67 2
    /**
68
     * {@inheritdoc}
69 2
     */
70
    protected function doFlush()
71
    {
72
        return apcu_clear_cache();
73
    }
74
75 2
    /**
76
     * {@inheritdoc}
77 2
     */
78
    protected function doFetchAtomic(string $id, callable $generator, int $ttl)
79
    {
80
        if (! function_exists('apcu_entry')) {
81
            throw new RuntimeException('Atomic fetch (apcu_entry) is not supported by this version of apcu');
82
        }
83 1
84
        return apcu_entry($id, $generator, $ttl);
85 1
    }
86
87 1
    /**
88
     * {@inheritdoc}
89
     */
90
    protected function doFetchMultiple(array $keys)
91
    {
92
        return apcu_fetch($keys) ?: [];
93 1
    }
94
95 1
    /**
96 1
     * {@inheritdoc}
97
     */
98
    protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
99 1
    {
100 1
        $result = apcu_store($keysAndValues, null, $lifetime);
101 1
102 1
        return empty($result);
103 1
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    protected function doGetStats()
109
    {
110
        $info = apcu_cache_info(true);
111
        $sma  = apcu_sma_info();
112
113
        return [
114
            Cache::STATS_HITS             => $info['num_hits'],
115
            Cache::STATS_MISSES           => $info['num_misses'],
116
            Cache::STATS_UPTIME           => $info['start_time'],
117
            Cache::STATS_MEMORY_USAGE     => $info['mem_size'],
118
            Cache::STATS_MEMORY_AVAILABLE => $sma['avail_mem'],
119
        ];
120
    }
121
}
122