1c692965aSGreg Roach<?php 2c692965aSGreg Roach 3c692965aSGreg Roach/** 4c692965aSGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6c692965aSGreg Roach * This program is free software: you can redistribute it and/or modify 7c692965aSGreg Roach * it under the terms of the GNU General Public License as published by 8c692965aSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9c692965aSGreg Roach * (at your option) any later version. 10c692965aSGreg Roach * This program is distributed in the hope that it will be useful, 11c692965aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12c692965aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13c692965aSGreg Roach * GNU General Public License for more details. 14c692965aSGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16c692965aSGreg Roach */ 17c692965aSGreg Roach 18c692965aSGreg Roachdeclare(strict_types=1); 19c692965aSGreg Roach 20c692965aSGreg Roachnamespace Fisharebest\Webtrees; 21c692965aSGreg Roach 22c692965aSGreg Roachuse Closure; 23c692965aSGreg Roachuse Symfony\Contracts\Cache\CacheInterface; 24c692965aSGreg Roachuse Symfony\Contracts\Cache\ItemInterface; 25c692965aSGreg Roach 26c692965aSGreg Roach/** 27c692965aSGreg Roach * Wrapper around the symfony PSR6 cache library. 287476e8a5SGreg Roach * Hash the keys to protect against characters that are not allowed in PSR6. 29c692965aSGreg Roach */ 30c692965aSGreg Roachclass Cache 31c692965aSGreg Roach{ 32c4943cffSGreg Roach private CacheInterface $cache; 33c692965aSGreg Roach 34c692965aSGreg Roach /** 35c692965aSGreg Roach * @param CacheInterface $cache 36c692965aSGreg Roach */ 37c692965aSGreg Roach public function __construct(CacheInterface $cache) 38c692965aSGreg Roach { 39c692965aSGreg Roach $this->cache = $cache; 40c692965aSGreg Roach } 41c692965aSGreg Roach 42c692965aSGreg Roach /** 43c692965aSGreg Roach * Fetch an item from the cache - or create it where it does not exist. 44c692965aSGreg Roach * 455994d605SGreg Roach * @template T 465994d605SGreg Roach * 47c692965aSGreg Roach * @param string $key 485994d605SGreg Roach * @param Closure(): T $closure 49c2048608SGreg Roach * @param int|null $ttl 50c692965aSGreg Roach * 515994d605SGreg Roach * @return T 52c692965aSGreg Roach */ 53*2c6f1bd5SGreg Roach public function remember(string $key, Closure $closure, int|null $ttl = null) 54c692965aSGreg Roach { 557476e8a5SGreg Roach return $this->cache->get(md5($key), static function (ItemInterface $item) use ($closure, $ttl) { 56c692965aSGreg Roach $item->expiresAfter($ttl); 57c692965aSGreg Roach 58c692965aSGreg Roach return $closure(); 59c692965aSGreg Roach }); 60c692965aSGreg Roach } 61c692965aSGreg Roach 62c692965aSGreg Roach /** 63c692965aSGreg Roach * Remove an item from the cache. 64c692965aSGreg Roach * 65c692965aSGreg Roach * @param string $key 66c692965aSGreg Roach */ 67c692965aSGreg Roach public function forget(string $key): void 68c692965aSGreg Roach { 697476e8a5SGreg Roach $this->cache->delete(md5($key)); 70c692965aSGreg Roach } 71c692965aSGreg Roach} 72