Passed
Pull Request — master (#251)
by Gabriel
10:21
created

ApcCache::doGetStats()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 0
dl 0
loc 18
ccs 0
cts 15
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Common\Cache;
4
5
use const PHP_VERSION_ID;
6
use function apc_cache_info;
7
use function apc_clear_cache;
8
use function apc_delete;
9
use function apc_exists;
10
use function apc_fetch;
11
use function apc_sma_info;
12
use function apc_store;
13
14
/**
15
 * APC cache provider.
16
 *
17
 * @link       www.doctrine-project.org
18
 * @deprecated since version 1.6, use ApcuCache instead
19
 */
20
class ApcCache extends CacheProvider
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function doFetch($id)
26
    {
27
        return apc_fetch($id);
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function doContains($id)
34
    {
35
        return apc_exists($id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apc_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...
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function doSave($id, $data, $lifeTime = 0)
42
    {
43
        return apc_store($id, $data, $lifeTime);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apc_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...
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected function doDelete($id)
50
    {
51
        // apc_delete returns false if the id does not exist
52
        return apc_delete($id) || ! apc_exists($id);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function doFlush()
59
    {
60
        return apc_clear_cache() && apc_clear_cache('user');
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected function doFetchMultiple(array $keys)
67
    {
68
        return apc_fetch($keys) ?: [];
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
75
    {
76
        $result = apc_store($keysAndValues, null, $lifetime);
77
78
        return empty($result);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    protected function doGetStats()
85
    {
86
        $info = apc_cache_info('', true);
87
        $sma  = apc_sma_info();
88
89
        // @TODO - Temporary fix @see https://github.com/krakjoe/apcu/pull/42
90
        if (PHP_VERSION_ID >= 50500) {
91
            $info['num_hits']   = $info['num_hits'] ?? $info['nhits'];
92
            $info['num_misses'] = $info['num_misses'] ?? $info['nmisses'];
93
            $info['start_time'] = $info['start_time'] ?? $info['stime'];
94
        }
95
96
        return [
97
            Cache::STATS_HITS             => $info['num_hits'],
98
            Cache::STATS_MISSES           => $info['num_misses'],
99
            Cache::STATS_UPTIME           => $info['start_time'],
100
            Cache::STATS_MEMORY_USAGE     => $info['mem_size'],
101
            Cache::STATS_MEMORY_AVAILABLE => $sma['avail_mem'],
102
        ];
103
    }
104
}
105