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