mirror of
https://github.com/Znote/ZnoteAAC.git
synced 2025-04-29 18:59:21 +02:00
Fix #371 - fast APCu memory caching support
This commit is contained in:
parent
ac2b909805
commit
9e790a71f6
@ -54,6 +54,7 @@ if (user_logged_in()) {
|
|||||||
<h1>Changelog</h1>
|
<h1>Changelog</h1>
|
||||||
<?php
|
<?php
|
||||||
$cache = new Cache('engine/cache/changelog');
|
$cache = new Cache('engine/cache/changelog');
|
||||||
|
$cache->useMemory(false);
|
||||||
if ($updateCache === true) {
|
if ($updateCache === true) {
|
||||||
$changelogs = mysql_select_multi("SELECT `id`, `text`, `time`, `report_id`, `status` FROM `znote_changelog` ORDER BY `id` DESC;");
|
$changelogs = mysql_select_multi("SELECT `id`, `text`, `time`, `report_id`, `status` FROM `znote_changelog` ORDER BY `id` DESC;");
|
||||||
|
|
||||||
|
11
config.php
11
config.php
@ -658,8 +658,15 @@
|
|||||||
'name' => 'Forgotten' // Must be identical to config.lua (OT config file) server name.
|
'name' => 'Forgotten' // Must be identical to config.lua (OT config file) server name.
|
||||||
);
|
);
|
||||||
|
|
||||||
// How often do you want highscores to update?
|
// How often do you want highscores (cache) to update?
|
||||||
$config['cache_lifespan'] = 5; // 60 * 15; // 15 minutes.
|
$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!
|
// WARNING! Account names written here will have admin access to web page!
|
||||||
$config['page_admin_access'] = array(
|
$config['page_admin_access'] = array(
|
||||||
|
@ -1,122 +1,167 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class Cache
|
class Cache
|
||||||
{
|
{
|
||||||
protected $_file = false;
|
protected $_file = false;
|
||||||
protected $_lifespan = 0;
|
protected $_lifespan = 0;
|
||||||
protected $_content;
|
protected $_content;
|
||||||
|
protected $_memory = false;
|
||||||
|
protected $_canMemory = false;
|
||||||
|
|
||||||
const EXT = '.cache.php';
|
const EXT = '.cache.php';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $file
|
* @param string $file
|
||||||
* @access public
|
* @access public
|
||||||
* @return void
|
* @return void
|
||||||
**/
|
**/
|
||||||
public function __construct($file) {
|
public function __construct($file) {
|
||||||
$this->_file = $file . self::EXT;
|
$cfg = config('cache');
|
||||||
$this->setExpiration(config('cache_lifespan'));
|
|
||||||
|
$this->setExpiration($cfg['lifespan']);
|
||||||
|
if (function_exists('apcu_fetch')) {
|
||||||
|
$this->_canMemory = true;
|
||||||
|
$this->_memory = $cfg['memory'];
|
||||||
}
|
}
|
||||||
|
$this->_file = $file . self::EXT;
|
||||||
|
|
||||||
|
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>
|
||||||
|
");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the cache expiration limit (IMPORTANT NOTE: seconds, NOT ms!).
|
* Sets the cache expiration limit (IMPORTANT NOTE: seconds, NOT ms!).
|
||||||
*
|
*
|
||||||
* @param integer $span
|
* @param integer $span
|
||||||
* @access public
|
* @access public
|
||||||
* @return void
|
* @return void
|
||||||
**/
|
**/
|
||||||
public function setExpiration($span) {
|
public function setExpiration($span) {
|
||||||
$this->_lifespan = $span;
|
$this->_lifespan = $span;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the content you'd like to cache.
|
* Enable or disable memory RAM storage.
|
||||||
*
|
*
|
||||||
* @param mixed $content
|
* @param bool $bool
|
||||||
* @access public
|
* @access public
|
||||||
* @return void
|
* @return bool $status
|
||||||
**/
|
**/
|
||||||
public function setContent($content) {
|
public function useMemory($bool) {
|
||||||
switch (strtolower(gettype($content))) {
|
if ($bool and $this->_canMemory) {
|
||||||
case 'array':
|
$this->_memory = true;
|
||||||
$this->_content = json_encode($content);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
$this->_content = $content;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validates whether it is time to refresh the cache data or not.
|
|
||||||
*
|
|
||||||
* @access public
|
|
||||||
* @return boolean
|
|
||||||
**/
|
|
||||||
public function hasExpired() {
|
|
||||||
if (is_file($this->_file) && time() < filemtime($this->_file) + $this->_lifespan) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
$this->_memory = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns remaining time before scoreboard will update itself.
|
/**
|
||||||
*
|
* Set the content you'd like to cache.
|
||||||
* @access public
|
*
|
||||||
* @return integer
|
* @param mixed $content
|
||||||
**/
|
* @access public
|
||||||
public function remainingTime() {
|
* @return void
|
||||||
$remaining = 0;
|
**/
|
||||||
if (!$this->hasExpired()) {
|
public function setContent($content) {
|
||||||
$remaining = (filemtime($this->_file) + $this->_lifespan) - time();
|
$this->_content = (!$this->_memory && strtolower(gettype($content)) == 'array') ? json_encode($content) : $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates whether it is time to refresh the cache data or not.
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns remaining time before scoreboard will update itself.
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @return integer
|
||||||
|
**/
|
||||||
|
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;
|
return $remaining;
|
||||||
}
|
}
|
||||||
|
if (!$this->hasExpired()) {
|
||||||
|
$remaining = (filemtime($this->_file) + $this->_lifespan) - time();
|
||||||
|
}
|
||||||
|
return $remaining;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves the content into its appropriate cache file.
|
* Saves the content into its appropriate cache file.
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @return void
|
* @return void
|
||||||
**/
|
**/
|
||||||
public function save() {
|
public function save() {
|
||||||
$handle = fopen($this->_file, 'w');
|
if ($this->_memory) {
|
||||||
fwrite($handle, $this->_content);
|
return apcu_store($this->_file, $this->_content, $this->_lifespan);
|
||||||
fclose($handle);
|
}
|
||||||
|
$handle = fopen($this->_file, 'w');
|
||||||
|
fwrite($handle, $this->_content);
|
||||||
|
fclose($handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the content from a specified cache file.
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @return mixed
|
||||||
|
**/
|
||||||
|
public function load() {
|
||||||
|
if ($this->_memory) {
|
||||||
|
return apcu_fetch($this->_file);
|
||||||
|
}
|
||||||
|
if (!is_file($this->_file)) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
include_once($this->_file);
|
||||||
|
$content = ob_get_clean();
|
||||||
|
|
||||||
/**
|
if (!isset($content) && strlen($content) == 0) {
|
||||||
* Loads the content from a specified cache file.
|
return false;
|
||||||
*
|
}
|
||||||
* @access public
|
|
||||||
* @return mixed
|
|
||||||
**/
|
|
||||||
public function load() {
|
|
||||||
if (!is_file($this->_file)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ob_start();
|
if ($content = json_decode($content, true)) {
|
||||||
include_once($this->_file);
|
return (array) $content;
|
||||||
$content = ob_get_clean();
|
} else {
|
||||||
|
return $content;
|
||||||
if (!isset($content) && strlen($content) == 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($content = json_decode($content, true)) {
|
|
||||||
return (array) $content;
|
|
||||||
} else {
|
|
||||||
return $content;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -15,6 +15,7 @@ require_once 'engine/init.php'; include 'layout/overall/header.php';
|
|||||||
// Changelog ticker //
|
// Changelog ticker //
|
||||||
// Load from cache
|
// Load from cache
|
||||||
$changelogCache = new Cache('engine/cache/changelog');
|
$changelogCache = new Cache('engine/cache/changelog');
|
||||||
|
$changelogCache->useMemory(false);
|
||||||
$changelogs = $changelogCache->load();
|
$changelogs = $changelogCache->load();
|
||||||
|
|
||||||
if (isset($changelogs) && !empty($changelogs) && $changelogs !== false) {
|
if (isset($changelogs) && !empty($changelogs) && $changelogs !== false) {
|
||||||
|
@ -4,6 +4,7 @@ if ($config['UseChangelogTicker']) {
|
|||||||
// Changelog ticker //
|
// Changelog ticker //
|
||||||
// Load from cache
|
// Load from cache
|
||||||
$changelogCache = new Cache('engine/cache/changelog');
|
$changelogCache = new Cache('engine/cache/changelog');
|
||||||
|
$changelogCache->useMemory(false);
|
||||||
$changelogs = $changelogCache->load();
|
$changelogs = $changelogCache->load();
|
||||||
|
|
||||||
if (isset($changelogs) && !empty($changelogs) && $changelogs !== false) {
|
if (isset($changelogs) && !empty($changelogs) && $changelogs !== false) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user