-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
258 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2003-2024 The Galette Team | ||
* | ||
* This file is part of Galette (https://galette.eu). | ||
* | ||
* Galette is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Galette is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Galette. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace Galette\Features; | ||
|
||
use Analog\Analog; | ||
use Galette\Core\Galette; | ||
use Throwable; | ||
|
||
/** | ||
* Cacheable objects trait | ||
* | ||
* @author Johan Cwiklinski <johan@x-tnd.be> | ||
*/ | ||
|
||
trait Cacheable | ||
{ | ||
//number of hours until cache will be invalid | ||
protected int $cache_timeout = 24; | ||
|
||
/** | ||
* Handle cache | ||
* | ||
* @param boolean $nocache Do not try to cache | ||
* | ||
* @return void | ||
*/ | ||
protected function handleCache(bool $nocache = false): void | ||
{ | ||
if ($nocache === false && !Galette::isDebugEnabled()) { | ||
if (!$this->checkCache()) { | ||
$this->makeCache(); | ||
} else { | ||
$this->loadCache(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Check if cache is valid | ||
* | ||
* @return boolean | ||
*/ | ||
private function checkCache(): bool | ||
{ | ||
$cfile = GALETTE_CACHE_DIR . $this->getCacheFilename(); | ||
if (file_exists($cfile)) { | ||
try { | ||
$dformat = 'Y-m-d H:i:s'; | ||
$mdate = \DateTime::createFromFormat( | ||
$dformat, | ||
date( | ||
$dformat, | ||
filemtime($cfile) | ||
) | ||
); | ||
$expire = $mdate->add( | ||
new \DateInterval('PT' . $this->cache_timeout . 'H') | ||
); | ||
$now = new \DateTime(); | ||
$has_expired = $now > $expire; | ||
return !$has_expired; | ||
} catch (Throwable $e) { | ||
Analog::log( | ||
'Unable check cache expiry. Are you sure you have ' . | ||
'properly configured PHP timezone settings on your server?', | ||
Analog::WARNING | ||
); | ||
return false; | ||
} | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Complete path to cache file | ||
* | ||
* @return string | ||
*/ | ||
abstract protected function getCacheFilename(): string; | ||
|
||
/** | ||
* Ensure data to cache are present | ||
* | ||
* @return void | ||
*/ | ||
abstract protected function prepareForCache(): void; | ||
|
||
/** | ||
* Creates/update the cache | ||
* | ||
* @return void | ||
*/ | ||
protected function makeCache(): void | ||
{ | ||
$this->prepareForCache(); | ||
$cfile = $this->getCacheFilename(); | ||
$cdir = dirname($cfile); | ||
if (!file_exists($cdir)) { | ||
mkdir($cdir, 0755, true); | ||
} | ||
$stream = fopen($cfile, 'w+'); | ||
fwrite( | ||
$stream, | ||
$this->getDataTocache() | ||
); | ||
fclose($stream); | ||
} | ||
|
||
/** | ||
* Get data to cache | ||
* | ||
* @return string | ||
*/ | ||
protected function getDataTocache(): string | ||
{ | ||
throw new \RuntimeException('Method not implemented'); | ||
} | ||
|
||
/** | ||
* Loads entries from cache | ||
* | ||
* @return void | ||
*/ | ||
protected function loadCache(): void | ||
{ | ||
$cfile = $this->getCacheFilename(); | ||
$fcontents = file_get_contents($cfile); | ||
|
||
if (!$this->cacheLoaded($fcontents)) { | ||
$this->makeCache(); | ||
} | ||
} | ||
|
||
/** | ||
* Called once cache has been loaded. | ||
* | ||
* @param mixed $content Content from cache | ||
* | ||
* @return bool | ||
*/ | ||
abstract protected function cacheLoaded($content): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.