Improved caching
This commit is contained in:
parent
ee92c2f058
commit
a36755bb80
7 changed files with 81 additions and 19 deletions
|
@ -3,9 +3,18 @@
|
|||
namespace AntCMS;
|
||||
|
||||
use AntCMS\AntConfig;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
|
||||
class AntCache
|
||||
{
|
||||
/**
|
||||
* Caches a value for a given cache key.
|
||||
*
|
||||
* @param mixed $key The cache key to use for the cached value.
|
||||
* @param mixed $content The value to cache.
|
||||
* @return bool True if the value was successfully cached, false otherwise.
|
||||
* @throws ParseException If there is an error parsing the AntCMS configuration file.
|
||||
*/
|
||||
public function setCache($key, $content)
|
||||
{
|
||||
$cachePath = AntCachePath . "/$key.cache";
|
||||
|
@ -17,9 +26,18 @@ class AntCache
|
|||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the cached value for a given cache key.
|
||||
*
|
||||
* @param mixed $key The cache key used to retrieve the cached value.
|
||||
* @return string|false The cached value, or false if there was an error loading it or if caching is disabled.
|
||||
* @throws ParseException If there is an error parsing the AntCMS configuration file.
|
||||
*/
|
||||
public function getCache($key)
|
||||
{
|
||||
$cachePath = AntCachePath . "/$key.cache";
|
||||
|
@ -36,6 +54,13 @@ class AntCache
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a cache key has a corresponding cached value.
|
||||
*
|
||||
* @param string $key The cache key to check.
|
||||
* @return bool True if the cache key has a corresponding cached value, false otherwise. Will also return false if caching is disabled.
|
||||
* @throws ParseException If there is an error parsing the AntCMS configuration file.
|
||||
*/
|
||||
public function isCached($key)
|
||||
{
|
||||
$config = AntConfig::currentConfig();
|
||||
|
@ -46,4 +71,26 @@ class AntCache
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a unique cache key for the associated content and a salt value.
|
||||
* The salt is used to ensure that each cache key is unique to each component, even if multiple components are using the same source content but caching different results.
|
||||
*
|
||||
* @param mixed $content The content to generate a cache key for.
|
||||
* @param string $salt An optional salt value to use in the cache key generation. Default is 'cache'.
|
||||
* @return string The generated cache key.
|
||||
*/
|
||||
public function createCacheKey($content, $salt = 'cache')
|
||||
{
|
||||
/**
|
||||
* If the server is modern enough to have xxh128, use that. It is really fast and still produces long hashes
|
||||
* If not, use MD4 since it's still quite fast.
|
||||
* Source: https://php.watch/articles/php-hash-benchmark
|
||||
*/
|
||||
if (in_array('xxh128', hash_algos())) {
|
||||
return hash('xxh128', $content . $salt);
|
||||
} else {
|
||||
return hash('md5', $content . $salt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,10 +10,10 @@ class AntKeywords
|
|||
public function generateKeywords($content = '', $count = 15)
|
||||
{
|
||||
$cache = new AntCache();
|
||||
$cacheKey = hash('sha3-512', $content).'keywords';
|
||||
$cacheKey = $cache->createCacheKey($content, 'keywords');
|
||||
$currentConfig = AntConfig::currentConfig();
|
||||
|
||||
if(!$currentConfig['generateKeywords']){
|
||||
if (!$currentConfig['generateKeywords']) {
|
||||
return '';
|
||||
}
|
||||
|
||||
|
@ -29,7 +29,7 @@ class AntKeywords
|
|||
$stopWords = array(' a ', ' an ', ' and ', ' are ', ' as ', ' at ', ' be ', ' by ', ' for ', ' from ', ' has ', ' have ', ' he ', ' in ', ' is ', ' it ', ' its ', ' of ', ' on ', ' that ', ' the ', ' to ', ' was ', ' were ', ' will ', ' with ');
|
||||
$symbols = array('$', '€', '£', '¥', 'CHF', '₹', '+', '-', '×', '÷', '=', '>', '<', '.', ',', ';', ':', '!', '?', '"', '\'', '(', ')', '[', ']', '{', '}', '©', '™', '°', '§', '¶', '•', '_', '/');
|
||||
$markdownSymbols = array('#', '##', '###', '####', '#####', '~~', '__', '**', '`', '``', '```', '*', '+', '>', '[', ']', '(', ')', '!', '&', '|');
|
||||
$numbers = array('0','1','2','3','4','5','6','7','8','9');
|
||||
$numbers = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
|
||||
|
||||
//Strip the aforementioned characters away
|
||||
$content = str_replace($stopWords, ' ', $content);
|
||||
|
|
|
@ -53,7 +53,7 @@ class AntMarkdown
|
|||
public static function renderMarkdown($md)
|
||||
{
|
||||
$cache = new AntCache();
|
||||
$cacheKey = hash('sha3-512', $md).'markdown';
|
||||
$cacheKey = $cache->createCacheKey($md, 'markdown');
|
||||
|
||||
if ($cache->isCached($cacheKey)) {
|
||||
$cachedContent = $cache->getCache($cacheKey);
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace AntCMS;
|
|||
use AntCMS\AntCMS;
|
||||
use AntCMS\AntYaml;
|
||||
use AntCMS\AntConfig;
|
||||
use AntCMS\AntCache;
|
||||
|
||||
class AntPages
|
||||
{
|
||||
|
@ -43,18 +44,32 @@ class AntPages
|
|||
public static function generateNavigation($navTemplate = '')
|
||||
{
|
||||
$currentConfig = AntConfig::currentConfig();
|
||||
$baseURL = $currentConfig['baseURL'];
|
||||
$pages = AntPages::getPages();
|
||||
$cache = new AntCache;
|
||||
|
||||
$theme = $currentConfig['activeTheme'];
|
||||
$cacheKey = $cache->createCacheKey(json_encode($pages), $theme);
|
||||
|
||||
if ($cache->isCached($cacheKey)) {
|
||||
$cachedContent = $cache->getCache($cacheKey);
|
||||
|
||||
if ($cachedContent !== false && !empty($cachedContent)) {
|
||||
return $cachedContent;
|
||||
}
|
||||
}
|
||||
|
||||
$navHTML = '';
|
||||
foreach (AntPages::getPages() as $page) {
|
||||
if(!$page['showInNav']){
|
||||
$baseURL = $currentConfig['baseURL'];
|
||||
foreach ($pages as $page) {
|
||||
if (!$page['showInNav']) {
|
||||
continue;
|
||||
}
|
||||
$url = "//" . str_replace('//', '/',$baseURL . $page['functionalPagePath']);
|
||||
$url = "//" . str_replace('//', '/', $baseURL . $page['functionalPagePath']);
|
||||
$navEntry = str_replace('<!--AntCMS-PageLink-->', $url, $navTemplate);
|
||||
$navEntry = str_replace('<!--AntCMS-PageTitle-->', $page['pageTitle'], $navEntry);
|
||||
$navHTML .= $navEntry;
|
||||
}
|
||||
$cache->setCache($cacheKey, $navHTML);
|
||||
return $navHTML;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<!doctype html>
|
||||
<html lang="en" data-bs-theme="dark">
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
|
@ -8,7 +8,7 @@
|
|||
<meta name="author" content="<!--AntCMS-Author-->">
|
||||
<meta name="keywords" content="<!--AntCMS-Keywords-->">
|
||||
|
||||
<link href="/Themes/Tailwind/Assets/Dist/tailwind.css" rel="stylesheet">
|
||||
<link href="<!--AntCMS-SiteLink-->/Themes/Tailwind/Assets/Dist/tailwind.css" rel="stylesheet">
|
||||
|
||||
<title><!--AntCMS-Title--></title>
|
||||
</head>
|
||||
|
|
|
@ -18,17 +18,17 @@ use AntCMS\AntPages;
|
|||
|
||||
$antCms = new AntCMS();
|
||||
|
||||
if(!file_exists(antConfigFile)){
|
||||
if (!file_exists(antConfigFile)) {
|
||||
AntConfig::generateConfig();
|
||||
}
|
||||
|
||||
if(!file_exists(antPagesList)){
|
||||
if (!file_exists(antPagesList)) {
|
||||
AntPages::generatePages();
|
||||
}
|
||||
|
||||
$currentConfg = AntConfig::currentConfig();
|
||||
|
||||
if ($currentConfg['forceHTTPS'] && 'cli' !== PHP_SAPI){
|
||||
if ($currentConfg['forceHTTPS'] && 'cli' !== PHP_SAPI) {
|
||||
$isHTTPS = false;
|
||||
|
||||
if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
|
||||
|
@ -41,7 +41,7 @@ if ($currentConfg['forceHTTPS'] && 'cli' !== PHP_SAPI){
|
|||
$isHTTPS = true;
|
||||
}
|
||||
|
||||
if(!$isHTTPS){
|
||||
if (!$isHTTPS) {
|
||||
$url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
|
||||
header('Location: ' . $url);
|
||||
exit;
|
||||
|
|
Loading…
Reference in a new issue