3 namespace Friendica\Core\Cache;
6 use Friendica\Core\Cache;
11 * @author Philipp Holzer <admin@philipp.info>
13 class APCuCache extends AbstractCacheDriver implements IMemoryCacheDriver
16 use TraitCompareDelete;
21 public function __construct()
23 if (!self::isAvailable()) {
24 throw new Exception('APCu is not available.');
31 public function getAllKeys($prefix = null)
33 $ns = $this->getCacheKey($prefix);
34 $ns = preg_quote($ns, '/');
36 if (class_exists('\APCIterator')) {
37 $iterator = new \APCIterator('user', '/^' . $ns. '/', APC_ITER_KEY);
39 $iterator = new \APCUIterator('/^' . $ns . '/', APC_ITER_KEY);
43 foreach ($iterator as $item) {
44 array_push($keys, $item['key']);
47 return $this->getOriginalKeys($keys);
53 public function get($key)
56 $cachekey = $this->getCacheKey($key);
58 $cached = apcu_fetch($cachekey, $success);
63 $value = unserialize($cached);
65 // Only return a value if the serialized value is valid.
66 // We also check if the db entry is a serialized
67 // boolean 'false' value (which we want to return).
68 if ($cached === serialize(false) || $value !== false) {
78 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
80 $cachekey = $this->getCacheKey($key);
82 $cached = serialize($value);
101 public function delete($key)
103 $cachekey = $this->getCacheKey($key);
104 return apcu_delete($cachekey);
110 public function clear($outdated = true)
115 $prefix = $this->getPrefix();
116 $prefix = preg_quote($prefix, '/');
118 if (class_exists('\APCIterator')) {
119 $iterator = new \APCIterator('user', '/^' . $prefix . '/', APC_ITER_KEY);
121 $iterator = new \APCUIterator('/^' . $prefix . '/', APC_ITER_KEY);
124 return apcu_delete($iterator);
131 public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
133 $cachekey = $this->getCacheKey($key);
134 $cached = serialize($value);
136 return apcu_add($cachekey, $cached);
139 public static function isAvailable()
141 if (!extension_loaded('apcu')) {
143 } elseif (!ini_get('apc.enabled') && !ini_get('apc.enable_cli')) {
146 version_compare(phpversion('apc') ?: '0.0.0', '4.0.6') === -1 &&
147 version_compare(phpversion('apcu') ?: '0.0.0', '5.1.0') === -1