. */ 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. */ class Cache { // Default time-to-live for cache items; 100 days. private const DEFAULT_TTL = 8640000; /** @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 $ttl * * @return mixed */ public function remember(string $key, Closure $closure, int $ttl = self::DEFAULT_TTL) { return $this->cache->get($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($key); } }