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
26c486ef27
commit
32e5b6278e
@ -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(
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
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';
|
||||||
|
|
||||||
@ -15,8 +17,22 @@
|
|||||||
* @return void
|
* @return void
|
||||||
**/
|
**/
|
||||||
public function __construct($file) {
|
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->_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.
|
* Set the content you'd like to cache.
|
||||||
*
|
*
|
||||||
@ -40,15 +73,7 @@
|
|||||||
* @return void
|
* @return void
|
||||||
**/
|
**/
|
||||||
public function setContent($content) {
|
public function setContent($content) {
|
||||||
switch (strtolower(gettype($content))) {
|
$this->_content = (!$this->_memory && strtolower(gettype($content)) == 'array') ? json_encode($content) : $content;
|
||||||
case 'array':
|
|
||||||
$this->_content = json_encode($content);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
$this->_content = $content;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -59,10 +84,12 @@
|
|||||||
* @return boolean
|
* @return boolean
|
||||||
**/
|
**/
|
||||||
public function hasExpired() {
|
public function hasExpired() {
|
||||||
|
if ($this->_memory) {
|
||||||
|
return !apcu_exists($this->_file);
|
||||||
|
}
|
||||||
if (is_file($this->_file) && time() < filemtime($this->_file) + $this->_lifespan) {
|
if (is_file($this->_file) && time() < filemtime($this->_file) + $this->_lifespan) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,6 +101,18 @@
|
|||||||
**/
|
**/
|
||||||
public function remainingTime() {
|
public function remainingTime() {
|
||||||
$remaining = 0;
|
$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()) {
|
if (!$this->hasExpired()) {
|
||||||
$remaining = (filemtime($this->_file) + $this->_lifespan) - time();
|
$remaining = (filemtime($this->_file) + $this->_lifespan) - time();
|
||||||
}
|
}
|
||||||
@ -88,6 +127,9 @@
|
|||||||
* @return void
|
* @return void
|
||||||
**/
|
**/
|
||||||
public function save() {
|
public function save() {
|
||||||
|
if ($this->_memory) {
|
||||||
|
return apcu_store($this->_file, $this->_content, $this->_lifespan);
|
||||||
|
}
|
||||||
$handle = fopen($this->_file, 'w');
|
$handle = fopen($this->_file, 'w');
|
||||||
fwrite($handle, $this->_content);
|
fwrite($handle, $this->_content);
|
||||||
fclose($handle);
|
fclose($handle);
|
||||||
@ -101,6 +143,9 @@
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
**/
|
**/
|
||||||
public function load() {
|
public function load() {
|
||||||
|
if ($this->_memory) {
|
||||||
|
return apcu_fetch($this->_file);
|
||||||
|
}
|
||||||
if (!is_file($this->_file)) {
|
if (!is_file($this->_file)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -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