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 * @link       www.doctrine-project.org
18 * @deprecated since version 1.6, use ApcuCache instead
19 */
20class 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);
36    }
37
38    /**
39     * {@inheritdoc}
40     */
41    protected function doSave($id, $data, $lifeTime = 0)
42    {
43        return apc_store($id, $data, $lifeTime);
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