3 namespace Friendica\Core\Cache;
6 use Friendica\Core\BaseCache;
11 * @author Philipp Holzer <admin@philipp.info>
13 class APCuCache extends BaseCache implements IMemoryCache
16 use TraitCompareDelete;
21 public function __construct(string $hostname)
23 if (!self::isAvailable()) {
24 throw new Exception('APCu is not available.');
27 parent::__construct($hostname);
33 public function getAllKeys($prefix = null)
35 $ns = $this->getCacheKey($prefix);
36 $ns = preg_quote($ns, '/');
38 if (class_exists('\APCIterator')) {
39 $iterator = new \APCIterator('user', '/^' . $ns. '/', APC_ITER_KEY);
41 $iterator = new \APCUIterator('/^' . $ns . '/', APC_ITER_KEY);
45 foreach ($iterator as $item) {
46 array_push($keys, $item['key']);
49 return $this->getOriginalKeys($keys);
55 public function get($key)
58 $cachekey = $this->getCacheKey($key);
60 $cached = apcu_fetch($cachekey, $success);
65 $value = unserialize($cached);
67 // Only return a value if the serialized value is valid.
68 // We also check if the db entry is a serialized
69 // boolean 'false' value (which we want to return).
70 if ($cached === serialize(false) || $value !== false) {
80 public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
82 $cachekey = $this->getCacheKey($key);
84 $cached = serialize($value);
103 public function delete($key)
105 $cachekey = $this->getCacheKey($key);
106 return apcu_delete($cachekey);
112 public function clear($outdated = true)
117 $prefix = $this->getPrefix();
118 $prefix = preg_quote($prefix, '/');
120 if (class_exists('\APCIterator')) {
121 $iterator = new \APCIterator('user', '/^' . $prefix . '/', APC_ITER_KEY);
123 $iterator = new \APCUIterator('/^' . $prefix . '/', APC_ITER_KEY);
126 return apcu_delete($iterator);
133 public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
135 $cachekey = $this->getCacheKey($key);
136 $cached = serialize($value);
138 return apcu_add($cachekey, $cached);
141 public static function isAvailable()
143 if (!extension_loaded('apcu')) {
145 } elseif (!ini_get('apc.enabled') && !ini_get('apc.enable_cli')) {
148 version_compare(phpversion('apc') ?: '0.0.0', '4.0.6') === -1 &&
149 version_compare(phpversion('apcu') ?: '0.0.0', '5.1.0') === -1
160 public function getName()