. */ declare(strict_types=1); namespace Fisharebest\Webtrees; use Closure; use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Cache\ItemInterface; /** * Wrapper around the symfony PSR6 cache library. * Hash the keys to protect against characters that are not allowed in PSR6. */ class Cache { /** @var CacheInterface */ private $cache; /** * Cache constructor. * * @param CacheInterface $cache */ public function __construct(CacheInterface $cache) { $this->cache = $cache; } /** * Fetch an item from the cache - or create it where it does not exist. * * @param string $key * @param Closure $closure * @param int|null $ttl * * @return mixed */ public function remember(string $key, Closure $closure, int $ttl = null) { return $this->cache->get(md5($key), static function (ItemInterface $item) use ($closure, $ttl) { $item->expiresAfter($ttl); return $closure(); }); } /** * Remove an item from the cache. * * @param string $key */ public function forget(string $key): void { $this->cache->delete(md5($key)); } }