1*69675509SGreg Roach<?php 2*69675509SGreg Roach 3*69675509SGreg Roach/** 4*69675509SGreg Roach * webtrees: online genealogy 5*69675509SGreg Roach * Copyright (C) 2020 webtrees development team 6*69675509SGreg Roach * This program is free software: you can redistribute it and/or modify 7*69675509SGreg Roach * it under the terms of the GNU General Public License as published by 8*69675509SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9*69675509SGreg Roach * (at your option) any later version. 10*69675509SGreg Roach * This program is distributed in the hope that it will be useful, 11*69675509SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12*69675509SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13*69675509SGreg Roach * GNU General Public License for more details. 14*69675509SGreg Roach * You should have received a copy of the GNU General Public License 15*69675509SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16*69675509SGreg Roach */ 17*69675509SGreg Roach 18*69675509SGreg Roachdeclare(strict_types=1); 19*69675509SGreg Roach 20*69675509SGreg Roachnamespace Fisharebest\Webtrees\Factories; 21*69675509SGreg Roach 22*69675509SGreg Roachuse Fisharebest\Webtrees\Cache; 23*69675509SGreg Roachuse Fisharebest\Webtrees\Contracts\CacheFactoryInterface; 24*69675509SGreg Roachuse Fisharebest\Webtrees\Webtrees; 25*69675509SGreg Roachuse Symfony\Component\Cache\Adapter\ArrayAdapter; 26*69675509SGreg Roachuse Symfony\Component\Cache\Adapter\FilesystemAdapter; 27*69675509SGreg Roach 28*69675509SGreg Roachuse function random_int; 29*69675509SGreg Roach 30*69675509SGreg Roach/** 31*69675509SGreg Roach * Make a cache. 32*69675509SGreg Roach */ 33*69675509SGreg Roachclass CacheFactory implements CacheFactoryInterface 34*69675509SGreg Roach{ 35*69675509SGreg Roach // How frequently to perform garbage collection. 36*69675509SGreg Roach private const GC_PROBABILITY = 1000; 37*69675509SGreg Roach 38*69675509SGreg Roach // Filesystem cache parameters. 39*69675509SGreg Roach private const FILES_TTL = 8640000; 40*69675509SGreg Roach private const FILES_DIR = Webtrees::DATA_DIR . 'cache/'; 41*69675509SGreg Roach 42*69675509SGreg Roach /** @var ArrayAdapter */ 43*69675509SGreg Roach private $array_adapter; 44*69675509SGreg Roach 45*69675509SGreg Roach /** @var FilesystemAdapter */ 46*69675509SGreg Roach private $filesystem_adapter; 47*69675509SGreg Roach 48*69675509SGreg Roach /** 49*69675509SGreg Roach * CacheFactory constructor. 50*69675509SGreg Roach */ 51*69675509SGreg Roach public function __construct() 52*69675509SGreg Roach { 53*69675509SGreg Roach $this->array_adapter = new ArrayAdapter(0, false); 54*69675509SGreg Roach $this->filesystem_adapter = new FilesystemAdapter('', self::FILES_TTL, self::FILES_DIR); 55*69675509SGreg Roach } 56*69675509SGreg Roach 57*69675509SGreg Roach /** 58*69675509SGreg Roach * Create an array-based cache. 59*69675509SGreg Roach * 60*69675509SGreg Roach * @return Cache 61*69675509SGreg Roach */ 62*69675509SGreg Roach public function array(): Cache 63*69675509SGreg Roach { 64*69675509SGreg Roach return new Cache($this->array_adapter); 65*69675509SGreg Roach } 66*69675509SGreg Roach 67*69675509SGreg Roach /** 68*69675509SGreg Roach * Create an file-based cache. 69*69675509SGreg Roach * 70*69675509SGreg Roach * @return Cache 71*69675509SGreg Roach */ 72*69675509SGreg Roach public function file(): Cache 73*69675509SGreg Roach { 74*69675509SGreg Roach return new Cache($this->filesystem_adapter); 75*69675509SGreg Roach } 76*69675509SGreg Roach 77*69675509SGreg Roach /** 78*69675509SGreg Roach * Perform garbage collection. 79*69675509SGreg Roach */ 80*69675509SGreg Roach public function __destruct() 81*69675509SGreg Roach { 82*69675509SGreg Roach if (random_int(1, self::GC_PROBABILITY) === 1) { 83*69675509SGreg Roach $this->filesystem_adapter->prune(); 84*69675509SGreg Roach } 85*69675509SGreg Roach } 86*69675509SGreg Roach} 87