3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core\Cache\Type;
25 use Friendica\Core\Cache\Enum\Duration;
26 use Friendica\Core\Cache\IMemoryCache;
27 use Friendica\Core\Cache\Type\TraitCompareDelete;
28 use Friendica\Core\Cache\Type\TraitCompareSet;
29 use Friendica\Core\Cache\Enum\Type;
34 class APCuCache extends BaseCache implements IMemoryCache
37 use TraitCompareDelete;
42 public function __construct(string $hostname)
44 if (!self::isAvailable()) {
45 throw new Exception('APCu is not available.');
48 parent::__construct($hostname);
54 public function getAllKeys($prefix = null)
56 $ns = $this->getCacheKey($prefix);
57 $ns = preg_quote($ns, '/');
59 if (class_exists('\APCIterator')) {
60 $iterator = new \APCIterator('user', '/^' . $ns. '/', APC_ITER_KEY);
62 $iterator = new \APCUIterator('/^' . $ns . '/', APC_ITER_KEY);
66 foreach ($iterator as $item) {
67 array_push($keys, $item['key']);
70 return $this->getOriginalKeys($keys);
76 public function get($key)
79 $cachekey = $this->getCacheKey($key);
81 $cached = apcu_fetch($cachekey, $success);
86 $value = unserialize($cached);
88 // Only return a value if the serialized value is valid.
89 // We also check if the db entry is a serialized
90 // boolean 'false' value (which we want to return).
91 if ($cached === serialize(false) || $value !== false) {
101 public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
103 $cachekey = $this->getCacheKey($key);
105 $cached = serialize($value);
124 public function delete($key)
126 $cachekey = $this->getCacheKey($key);
127 return apcu_delete($cachekey);
133 public function clear($outdated = true)
138 $prefix = $this->getPrefix();
139 $prefix = preg_quote($prefix, '/');
141 if (class_exists('\APCIterator')) {
142 $iterator = new \APCIterator('user', '/^' . $prefix . '/', APC_ITER_KEY);
144 $iterator = new \APCUIterator('/^' . $prefix . '/', APC_ITER_KEY);
147 return apcu_delete($iterator);
154 public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
156 $cachekey = $this->getCacheKey($key);
157 $cached = serialize($value);
159 return apcu_add($cachekey, $cached);
162 public static function isAvailable()
164 if (!extension_loaded('apcu')) {
166 } elseif (!ini_get('apc.enabled') && !ini_get('apc.enable_cli')) {
169 version_compare(phpversion('apc') ?: '0.0.0', '4.0.6') === -1 &&
170 version_compare(phpversion('apcu') ?: '0.0.0', '5.1.0') === -1
181 public function getName()