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;
24 use Friendica\Core\Cache\Enum\Duration;
25 use Friendica\Core\Cache\Capability\ICanCacheInMemory;
26 use Friendica\Core\Cache\Enum\Type;
27 use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
32 class APCuCache extends AbstractCache implements ICanCacheInMemory
35 use CompareDeleteTrait;
38 * @param string $hostname
40 * @throws InvalidCacheDriverException
42 public function __construct(string $hostname)
44 if (!self::isAvailable()) {
45 throw new InvalidCacheDriverException('APCu is not available.');
48 parent::__construct($hostname);
54 public function getAllKeys(?string $prefix = null): array
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(string $key)
78 $cacheKey = $this->getCacheKey($key);
80 $cached = apcu_fetch($cacheKey, $success);
85 $value = unserialize($cached);
87 // Only return a value if the serialized value is valid.
88 // We also check if the db entry is a serialized
89 // boolean 'false' value (which we want to return).
90 if ($cached === serialize(false) || $value !== false) {
100 public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
102 $cacheKey = $this->getCacheKey($key);
104 $cached = serialize($value);
123 public function delete(string $key): bool
125 $cacheKey = $this->getCacheKey($key);
126 return apcu_delete($cacheKey);
132 public function clear(bool $outdated = true): bool
137 $prefix = $this->getPrefix();
138 $prefix = preg_quote($prefix, '/');
140 if (class_exists('\APCIterator')) {
141 $iterator = new \APCIterator('user', '/^' . $prefix . '/', APC_ITER_KEY);
143 $iterator = new \APCUIterator('/^' . $prefix . '/', APC_ITER_KEY);
146 return apcu_delete($iterator);
153 public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
155 $cacheKey = $this->getCacheKey($key);
156 $cached = serialize($value);
158 return apcu_add($cacheKey, $cached);
161 public static function isAvailable(): bool
163 if (!extension_loaded('apcu')) {
165 } elseif (!ini_get('apc.enabled') && !ini_get('apc.enable_cli')) {
168 version_compare(phpversion('apc') ?: '0.0.0', '4.0.6') === -1 &&
169 version_compare(phpversion('apcu') ?: '0.0.0', '5.1.0') === -1
180 public function getName(): string