169675509SGreg Roach<?php 269675509SGreg Roach 369675509SGreg Roach/** 469675509SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 669675509SGreg Roach * This program is free software: you can redistribute it and/or modify 769675509SGreg Roach * it under the terms of the GNU General Public License as published by 869675509SGreg Roach * the Free Software Foundation, either version 3 of the License, or 969675509SGreg Roach * (at your option) any later version. 1069675509SGreg Roach * This program is distributed in the hope that it will be useful, 1169675509SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1269675509SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1369675509SGreg Roach * GNU General Public License for more details. 1469675509SGreg 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/>. 1669675509SGreg Roach */ 1769675509SGreg Roach 1869675509SGreg Roachdeclare(strict_types=1); 1969675509SGreg Roach 2069675509SGreg Roachnamespace Fisharebest\Webtrees\Factories; 2169675509SGreg Roach 2269675509SGreg Roachuse Fisharebest\Webtrees\Cache; 2369675509SGreg Roachuse Fisharebest\Webtrees\Contracts\CacheFactoryInterface; 2469675509SGreg Roachuse Fisharebest\Webtrees\Webtrees; 2569675509SGreg Roachuse Symfony\Component\Cache\Adapter\ArrayAdapter; 2669675509SGreg Roachuse Symfony\Component\Cache\Adapter\FilesystemAdapter; 2769675509SGreg Roach 2869675509SGreg Roachuse function random_int; 2969675509SGreg Roach 3069675509SGreg Roach/** 3169675509SGreg Roach * Make a cache. 3269675509SGreg Roach */ 3369675509SGreg Roachclass CacheFactory implements CacheFactoryInterface 3469675509SGreg Roach{ 3569675509SGreg Roach // How frequently to perform garbage collection. 36*e873f434SGreg Roach private const int GC_PROBABILITY = 1000; 3769675509SGreg Roach 3869675509SGreg Roach // Filesystem cache parameters. 39*e873f434SGreg Roach private const int FILES_TTL = 8640000; 40*e873f434SGreg Roach private const string FILES_DIR = Webtrees::DATA_DIR . 'cache/'; 4169675509SGreg Roach 42c4943cffSGreg Roach private ArrayAdapter $array_adapter; 4369675509SGreg Roach 44c4943cffSGreg Roach private FilesystemAdapter $filesystem_adapter; 4569675509SGreg Roach 46e93a8df2SGreg Roach /**/ 4769675509SGreg Roach public function __construct() 4869675509SGreg Roach { 4969675509SGreg Roach $this->array_adapter = new ArrayAdapter(0, false); 5069675509SGreg Roach $this->filesystem_adapter = new FilesystemAdapter('', self::FILES_TTL, self::FILES_DIR); 5169675509SGreg Roach } 5269675509SGreg Roach 5369675509SGreg Roach /** 5469675509SGreg Roach * Create an array-based cache. 5569675509SGreg Roach * 5669675509SGreg Roach * @return Cache 5769675509SGreg Roach */ 5869675509SGreg Roach public function array(): Cache 5969675509SGreg Roach { 6069675509SGreg Roach return new Cache($this->array_adapter); 6169675509SGreg Roach } 6269675509SGreg Roach 6369675509SGreg Roach /** 6469675509SGreg Roach * Create an file-based cache. 6569675509SGreg Roach * 6669675509SGreg Roach * @return Cache 6769675509SGreg Roach */ 6869675509SGreg Roach public function file(): Cache 6969675509SGreg Roach { 7069675509SGreg Roach return new Cache($this->filesystem_adapter); 7169675509SGreg Roach } 7269675509SGreg Roach 7369675509SGreg Roach /** 7469675509SGreg Roach * Perform garbage collection. 7569675509SGreg Roach */ 7669675509SGreg Roach public function __destruct() 7769675509SGreg Roach { 7869675509SGreg Roach if (random_int(1, self::GC_PROBABILITY) === 1) { 7969675509SGreg Roach $this->filesystem_adapter->prune(); 8069675509SGreg Roach } 8169675509SGreg Roach } 8269675509SGreg Roach} 83