1<?php
2/**
3 * 2007-2016 PrestaShop
4 *
5 * thirty bees is an extension to the PrestaShop e-commerce software developed by PrestaShop SA
6 * Copyright (C) 2017-2018 thirty bees
7 *
8 * NOTICE OF LICENSE
9 *
10 * This source file is subject to the Open Software License (OSL 3.0)
11 * that is bundled with this package in the file LICENSE.txt.
12 * It is also available through the world-wide-web at this URL:
13 * http://opensource.org/licenses/osl-3.0.php
14 * If you did not receive a copy of the license and are unable to
15 * obtain it through the world-wide-web, please send an email
16 * to license@thirtybees.com so we can send you a copy immediately.
17 *
18 * DISCLAIMER
19 *
20 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
21 * versions in the future. If you wish to customize PrestaShop for your
22 * needs please refer to https://www.thirtybees.com for more information.
23 *
24 * @author    thirty bees <contact@thirtybees.com>
25 * @author    PrestaShop SA <contact@prestashop.com>
26 * @copyright 2017-2018 thirty bees
27 * @copyright 2007-2016 PrestaShop SA
28 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
29 *  PrestaShop is an internationally registered trademark & property of PrestaShop SA
30 */
31
32/**
33 * This class requires the PECL APC extension or PECL APCu extension to be installed
34 *
35 * @since 1.5.0
36 *
37 * @deprecated 1.1.0 Will be converted into a caching module following the PSR-6 standard: http://www.php-fig.org/psr/psr-6/
38 */
39class CacheApcuCore extends CacheCore
40{
41    /**
42     * CacheApcCore constructor.
43     *
44     * @throws PrestaShopException
45     */
46    public function __construct()
47    {
48        if (!extension_loaded('apcu')) {
49            throw new PrestaShopException('APCu cache has been enabled, but the APCu extension is not available');
50        }
51    }
52
53    /**
54     * Delete one or several data from cache (* joker can be used, but avoid it !)
55     *    E.g.: delete('*'); delete('my_prefix_*'); delete('my_key_name');
56     *
57     * @param string $key Cache key
58     *
59     * @return bool Whether the key was deleted
60     *
61     * @since 1.0.0
62     * @version 1.0.0 Initial version
63     */
64    public function delete($key)
65    {
66        if ($key == '*') {
67            $this->flush();
68        } elseif (strpos($key, '*') === false) {
69            $this->_delete($key);
70        } else {
71            $pattern = str_replace('\\*', '.*', preg_quote($key));
72
73            $cacheInfo = apcu_cache_info('');
74            foreach ($cacheInfo['cache_list'] as $entry) {
75                if (isset($entry['key'])) {
76                    $key = $entry['key'];
77                } else {
78                    $key = $entry['info'];
79                }
80                if (preg_match('#^'.$pattern.'$#', $key)) {
81                    $this->_delete($key);
82                }
83            }
84        }
85
86        return true;
87    }
88
89    /**
90     * @see Cache::_set()
91     *
92     * @since 1.0.0
93     * @version 1.0.0 Initial version
94     */
95    protected function _set($key, $value, $ttl = 0)
96    {
97        return apcu_store($key, $value, $ttl);
98    }
99
100    /**
101     * @see Cache::_get()
102     *
103     * @since 1.0.0
104     * @version 1.0.0 Initial version
105     */
106    protected function _get($key)
107    {
108        return apcu_fetch($key);
109    }
110
111    /**
112     * @see Cache::_exists()
113     *
114     * @since 1.0.0
115     * @version 1.0.0 Initial version
116     */
117    protected function _exists($key)
118    {
119        return apcu_exists($key);
120    }
121
122    /**
123     * @see Cache::_delete()
124     *
125     * @since 1.0.0
126     * @version 1.0.0 Initial version
127     */
128    protected function _delete($key)
129    {
130        return apcu_delete($key);
131    }
132
133    /**
134     * @see Cache::_writeKeys()
135     *
136     * @since 1.0.0
137     * @version 1.0.0 Initial version
138     */
139    protected function _writeKeys()
140    {
141    }
142
143    /**
144     * @see Cache::flush()
145     *
146     * @since 1.0.0
147     * @version 1.0.0 Initial version
148     */
149    public function flush()
150    {
151        return apcu_clear_cache();
152    }
153
154    /**
155     * Store data in the cache
156     *
157     * @param string $key   Cache Key
158     * @param mixed  $value Value
159     * @param int    $ttl   Time to live in the cache
160     *                      0 = unlimited
161     *
162     * @return bool Whether the data was successfully stored.
163     *
164     * @since 1.0.0
165     * @version 1.0.0 Initial version
166     */
167    public function set($key, $value, $ttl = 0)
168    {
169        return $this->_set($key, $value, $ttl);
170    }
171
172    /**
173     * Retrieve data from the cache
174     *
175     * @param string $key Cache key
176     *
177     * @return mixed Data
178     *
179     * @since 1.0.0
180     * @version 1.0.0 Initial version
181     */
182    public function get($key)
183    {
184        return $this->_get($key);
185    }
186
187    /**
188     * Check if data has been cached
189     *
190     * @param string $key Cache key
191     *
192     * @return bool Whether the data has been cached
193     *
194     * @since 1.0.0
195     * @version 1.0.0 Initial version
196     */
197    public function exists($key)
198    {
199        return $this->_exists($key);
200    }
201}
202