provider = $provider; } private function testTTL(string $key) { $meta = $this->provider->getItemMeta('fwcache_' . $key); if (!is_null($meta) && $meta['ttl'] > 0 && time() > $meta['time'] + $meta['ttl']) $this->provider->deleteItem('fwcache_' . $key); } public function get($key, $default = null) { // Remove the item if its TTL has expired $this->testTTL($key); // Fetch the value $res = $this->provider->getItem('fwcache_' . $key); // If there is no value, return the default return is_null($res) ? $default : $res; } public function set($key, $value, $ttl = null): bool { $meta = [ 'time' => time(), 'ttl' => is_int($ttl) ? $ttl : 0 ]; return $this->provider->save('fwcache_' . $key, $value, $meta); } public function delete($key): bool { return $this->provider->deleteItem('fwcache_' . $key); } public function clear(): bool { // Fetch the index set $index = $this->provider->getIndex(); foreach ($index as $entry) { if (substr($entry, 0, 8) === 'fwcache_') $this->provider->deleteItem($entry); } return true; } public function getMultiple($keys, $default = null): array { $out = []; foreach ($keys as $key) { $out[$key] = $this->get($key, $default); } return $out; } public function setMultiple($values, $ttl = null): bool { foreach ($values as $key => $value) $this->set($key, $value, $ttl); return true; } public function deleteMultiple($keys): bool { foreach ($keys as $key) $this->delete($key); return true; } public function has($key): bool { $this->testTTL($key); return $this->provider->hasItem('fwcache_' . $key); } }