3 namespace Friendica\Core\Cache;
10 * @author Philipp Holzer <admin@philipp.info>
12 class APCuCache extends Cache implements IMemoryCache
15 use TraitCompareDelete;
20 public function __construct(string $hostname)
22 if (!self::isAvailable()) {
23 throw new Exception('APCu is not available.');
26 parent::__construct($hostname);
32 public function getAllKeys($prefix = null)
34 $ns = $this->getCacheKey($prefix);
35 $ns = preg_quote($ns, '/');
37 if (class_exists('\APCIterator')) {
38 $iterator = new \APCIterator('user', '/^' . $ns. '/', APC_ITER_KEY);
40 $iterator = new \APCUIterator('/^' . $ns . '/', APC_ITER_KEY);
44 foreach ($iterator as $item) {
45 array_push($keys, $item['key']);
48 return $this->getOriginalKeys($keys);
54 public function get($key)
57 $cachekey = $this->getCacheKey($key);
59 $cached = apcu_fetch($cachekey, $success);
64 $value = unserialize($cached);
66 // Only return a value if the serialized value is valid.
67 // We also check if the db entry is a serialized
68 // boolean 'false' value (which we want to return).
69 if ($cached === serialize(false) || $value !== false) {
79 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
81 $cachekey = $this->getCacheKey($key);
83 $cached = serialize($value);
102 public function delete($key)
104 $cachekey = $this->getCacheKey($key);
105 return apcu_delete($cachekey);
111 public function clear($outdated = true)
116 $prefix = $this->getPrefix();
117 $prefix = preg_quote($prefix, '/');
119 if (class_exists('\APCIterator')) {
120 $iterator = new \APCIterator('user', '/^' . $prefix . '/', APC_ITER_KEY);
122 $iterator = new \APCUIterator('/^' . $prefix . '/', APC_ITER_KEY);
125 return apcu_delete($iterator);
132 public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
134 $cachekey = $this->getCacheKey($key);
135 $cached = serialize($value);
137 return apcu_add($cachekey, $cached);
140 public static function isAvailable()
142 if (!extension_loaded('apcu')) {
144 } elseif (!ini_get('apc.enabled') && !ini_get('apc.enable_cli')) {
147 version_compare(phpversion('apc') ?: '0.0.0', '4.0.6') === -1 &&
148 version_compare(phpversion('apcu') ?: '0.0.0', '5.1.0') === -1
159 public function getName()
161 return self::TYPE_APCU;