1<?php
2/*
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 *
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
18 */
19
20namespace Doctrine\Common\Cache;
21
22/**
23 * APC cache provider.
24 *
25 * @link       www.doctrine-project.org
26 * @deprecated since version 1.6, use ApcuCache instead
27 * @since      2.0
28 * @author     Benjamin Eberlei <kontakt@beberlei.de>
29 * @author     Guilherme Blanco <guilhermeblanco@hotmail.com>
30 * @author     Jonathan Wage <jonwage@gmail.com>
31 * @author     Roman Borschel <roman@code-factory.org>
32 * @author     David Abdemoulaie <dave@hobodave.com>
33 */
34class ApcCache extends CacheProvider
35{
36    /**
37     * {@inheritdoc}
38     */
39    protected function doFetch($id)
40    {
41        return apc_fetch($id);
42    }
43
44    /**
45     * {@inheritdoc}
46     */
47    protected function doContains($id)
48    {
49        return apc_exists($id);
50    }
51
52    /**
53     * {@inheritdoc}
54     */
55    protected function doSave($id, $data, $lifeTime = 0)
56    {
57        return apc_store($id, $data, $lifeTime);
58    }
59
60    /**
61     * {@inheritdoc}
62     */
63    protected function doDelete($id)
64    {
65        // apc_delete returns false if the id does not exist
66        return apc_delete($id) || ! apc_exists($id);
67    }
68
69    /**
70     * {@inheritdoc}
71     */
72    protected function doFlush()
73    {
74        return apc_clear_cache() && apc_clear_cache('user');
75    }
76
77    /**
78     * {@inheritdoc}
79     */
80    protected function doFetchMultiple(array $keys)
81    {
82        return apc_fetch($keys) ?: [];
83    }
84
85    /**
86     * {@inheritdoc}
87     */
88    protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
89    {
90        $result = apc_store($keysAndValues, null, $lifetime);
91
92        return empty($result);
93    }
94
95    /**
96     * {@inheritdoc}
97     */
98    protected function doGetStats()
99    {
100        $info = apc_cache_info('', true);
101        $sma  = apc_sma_info();
102
103        // @TODO - Temporary fix @see https://github.com/krakjoe/apcu/pull/42
104        if (PHP_VERSION_ID >= 50500) {
105            $info['num_hits']   = isset($info['num_hits'])   ? $info['num_hits']   : $info['nhits'];
106            $info['num_misses'] = isset($info['num_misses']) ? $info['num_misses'] : $info['nmisses'];
107            $info['start_time'] = isset($info['start_time']) ? $info['start_time'] : $info['stime'];
108        }
109
110        return array(
111            Cache::STATS_HITS             => $info['num_hits'],
112            Cache::STATS_MISSES           => $info['num_misses'],
113            Cache::STATS_UPTIME           => $info['start_time'],
114            Cache::STATS_MEMORY_USAGE     => $info['mem_size'],
115            Cache::STATS_MEMORY_AVAILABLE => $sma['avail_mem'],
116        );
117    }
118}
119