Fix #371 - fast APCu memory caching support

This commit is contained in:
Znote 2021-07-24 00:34:54 +02:00
parent ac2b909805
commit 9e790a71f6
5 changed files with 155 additions and 100 deletions

View File

@ -54,6 +54,7 @@ if (user_logged_in()) {
<h1>Changelog</h1>
<?php
$cache = new Cache('engine/cache/changelog');
$cache->useMemory(false);
if ($updateCache === true) {
$changelogs = mysql_select_multi("SELECT `id`, `text`, `time`, `report_id`, `status` FROM `znote_changelog` ORDER BY `id` DESC;");

View File

@ -658,8 +658,15 @@
'name' => 'Forgotten' // Must be identical to config.lua (OT config file) server name.
);
// How often do you want highscores to update?
$config['cache_lifespan'] = 5; // 60 * 15; // 15 minutes.
// How often do you want highscores (cache) to update?
$config['cache'] = array(
// If you have two instances installed on same server, make each instance prefix unique
'prefix' => 'znote_',
// 60 * 15; // 15 minutes.
'lifespan' => 5,
// Store cache in memory/RAM? Requires PHP extension APCu
'memory' => true
);
// WARNING! Account names written here will have admin access to web page!
$config['page_admin_access'] = array(

View File

@ -5,6 +5,8 @@
protected $_file = false;
protected $_lifespan = 0;
protected $_content;
protected $_memory = false;
protected $_canMemory = false;
const EXT = '.cache.php';
@ -15,8 +17,22 @@
* @return void
**/
public function __construct($file) {
$cfg = config('cache');
$this->setExpiration($cfg['lifespan']);
if (function_exists('apcu_fetch')) {
$this->_canMemory = true;
$this->_memory = $cfg['memory'];
}
$this->_file = $file . self::EXT;
$this->setExpiration(config('cache_lifespan'));
if (!$this->_memory && $cfg['memory']) die("
<p><strong>Configuration error!</strong>
<br>Cannot save cache to memory, but it is configured to do so.
<br>You need to enable PHP extension APCu to enable memory cache.
<br>Install it or set \$config['cache']['memory'] to false!
<br><strong>Ubuntu install:</strong> sudo apt install php-apcu</p>
");
}
@ -32,6 +48,23 @@
}
/**
* Enable or disable memory RAM storage.
*
* @param bool $bool
* @access public
* @return bool $status
**/
public function useMemory($bool) {
if ($bool and $this->_canMemory) {
$this->_memory = true;
return true;
}
$this->_memory = false;
return false;
}
/**
* Set the content you'd like to cache.
*
@ -40,15 +73,7 @@
* @return void
**/
public function setContent($content) {
switch (strtolower(gettype($content))) {
case 'array':
$this->_content = json_encode($content);
break;
default:
$this->_content = $content;
break;
}
$this->_content = (!$this->_memory && strtolower(gettype($content)) == 'array') ? json_encode($content) : $content;
}
@ -59,10 +84,12 @@
* @return boolean
**/
public function hasExpired() {
if ($this->_memory) {
return !apcu_exists($this->_file);
}
if (is_file($this->_file) && time() < filemtime($this->_file) + $this->_lifespan) {
return false;
}
return true;
}
@ -74,6 +101,18 @@
**/
public function remainingTime() {
$remaining = 0;
if ($this->_memory) {
if (apcu_exists($this->_file)) {
$meta = apc_cache_info('user');
foreach ($meta['cache_list'] AS $item) {
if ($item['info'] == $this->_file) {
$remaining = ($item['creation_time'] + $item['ttl']) - time();
return ($remaining > 0) ? $remaining : 0;
}
}
}
return $remaining;
}
if (!$this->hasExpired()) {
$remaining = (filemtime($this->_file) + $this->_lifespan) - time();
}
@ -88,6 +127,9 @@
* @return void
**/
public function save() {
if ($this->_memory) {
return apcu_store($this->_file, $this->_content, $this->_lifespan);
}
$handle = fopen($this->_file, 'w');
fwrite($handle, $this->_content);
fclose($handle);
@ -101,6 +143,9 @@
* @return mixed
**/
public function load() {
if ($this->_memory) {
return apcu_fetch($this->_file);
}
if (!is_file($this->_file)) {
return false;
}

View File

@ -15,6 +15,7 @@ require_once 'engine/init.php'; include 'layout/overall/header.php';
// Changelog ticker //
// Load from cache
$changelogCache = new Cache('engine/cache/changelog');
$changelogCache->useMemory(false);
$changelogs = $changelogCache->load();
if (isset($changelogs) && !empty($changelogs) && $changelogs !== false) {

View File

@ -4,6 +4,7 @@ if ($config['UseChangelogTicker']) {
// Changelog ticker //
// Load from cache
$changelogCache = new Cache('engine/cache/changelog');
$changelogCache->useMemory(false);
$changelogs = $changelogCache->load();
if (isset($changelogs) && !empty($changelogs) && $changelogs !== false) {