3 * @copyright Copyright (C) 2010-2023, 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;
24 use Friendica\Core\Cache\Enum\Duration;
25 use Friendica\Core\Cache\Capability\ICanCacheInMemory;
26 use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
31 class APCuCache extends AbstractCache implements ICanCacheInMemory
33 public static $NAME = 'apcu';
36 use CompareDeleteTrait;
39 * @throws InvalidCacheDriverException
41 public function __construct(string $hostname)
43 if (!self::isAvailable()) {
44 throw new InvalidCacheDriverException('APCu is not available.');
47 parent::__construct($hostname);
53 public function getAllKeys(?string $prefix = null): array
55 $ns = $this->getCacheKey($prefix ?? '');
56 $ns = preg_quote($ns, '/');
58 if (class_exists('\APCIterator')) {
59 $iterator = new \APCIterator('user', '/^' . $ns. '/', APC_ITER_KEY);
61 $iterator = new \APCUIterator('/^' . $ns . '/', APC_ITER_KEY);
65 foreach ($iterator as $item) {
66 array_push($keys, $item['key']);
69 return $this->getOriginalKeys($keys);
75 public function get(string $key)
77 $cacheKey = $this->getCacheKey($key);
79 $cached = apcu_fetch($cacheKey, $success);
84 $value = unserialize($cached);
86 // Only return a value if the serialized value is valid.
87 // We also check if the db entry is a serialized
88 // boolean 'false' value (which we want to return).
89 if ($cached === serialize(false) || $value !== false) {
99 public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
101 $cacheKey = $this->getCacheKey($key);
103 $cached = serialize($value);
122 public function delete(string $key): bool
124 $cacheKey = $this->getCacheKey($key);
125 return apcu_delete($cacheKey);
131 public function clear(bool $outdated = true): bool
136 $prefix = $this->getPrefix();
137 $prefix = preg_quote($prefix, '/');
139 if (class_exists('\APCIterator')) {
140 $iterator = new \APCIterator('user', '/^' . $prefix . '/', APC_ITER_KEY);
142 $iterator = new \APCUIterator('/^' . $prefix . '/', APC_ITER_KEY);
145 return apcu_delete($iterator);
152 public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
154 $cacheKey = $this->getCacheKey($key);
155 $cached = serialize($value);
157 return apcu_add($cacheKey, $cached);
160 public static function isAvailable(): bool
162 if (!extension_loaded('apcu')) {
164 } elseif (!ini_get('apc.enabled') && !ini_get('apc.enable_cli')) {
167 version_compare(phpversion('apc') ?: '0.0.0', '4.0.6') === -1 &&
168 version_compare(phpversion('apcu') ?: '0.0.0', '5.1.0') === -1