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