* new cache engine: plain php

This commit is contained in:
slawkens 2019-04-24 12:08:42 +02:00
parent 14cb37725e
commit d3e3efd05e
3 changed files with 82 additions and 1 deletions

1
TODO
View File

@ -84,7 +84,6 @@ x.x - At any time between (version not specified)
* update account.management page to be more realistic (like on tibia.com)
* update guilds page to be more realistic (like on tibia.com)
* possibility to add extra cache engines with plugins
* new cache engine - plain php, is good with pure php 7.0+ and opcache
* preferably configurable (enable/disable) forum TinyMCE editor
* OTAdmin support in Admin Panel
* database towns table support for TFS 1.3

View File

@ -64,6 +64,11 @@ class Cache
self::$instance = new Cache_File($prefix, CACHE);
break;
case 'php':
require 'cache_php.php';
self::$instance = new Cache_PHP($prefix, CACHE);
break;
case 'auto':
self::$instance = self::generateInstance(self::detect(), $prefix);
break;

77
system/libs/cache_php.php Normal file
View File

@ -0,0 +1,77 @@
<?php
/**
* PHP cache class
*
* @package MyAAC
* @author Slawkens <slawkens@gmail.com>
* @copyright 2017 MyAAC
* @link http://my-aac.org
*/
defined('MYAAC') or die('Direct access not allowed!');
class Cache_PHP
{
private $prefix;
private $dir;
private $enabled;
public function __construct($prefix = '', $dir = '') {
$this->prefix = $prefix;
$this->dir = $dir;
$this->enabled = (file_exists($this->dir) && is_dir($this->dir) && is_writable($this->dir));
}
public function set($key, $var, $ttl = 0)
{
$var = var_export($var, true);
// Write to temp file first to ensure atomicity
$tmp = $this->dir . "tmp_$key." . uniqid('', true) . '.tmp';
file_put_contents($tmp, '<?php $var = ' . $var . ';', LOCK_EX);
$file = $this->_name($key);
rename($tmp, $file);
if($ttl !== 0) {
touch($file, time() + $ttl);
}
else {
touch($file, time() + 24 * 60 * 60);
}
}
public function get($key)
{
$tmp = '';
if($this->fetch($key, $tmp))
return $tmp;
return '';
}
public function fetch($key, &$var)
{
$file = $this->_name($key);
if(!file_exists($file) || filemtime($file) < time())
return false;
@include $file;
$var = isset($var) ? $var : null;
return true;
}
public function delete($key)
{
$file = $this->_name($key);
if(file_exists($file))
unlink($file);
}
public function enabled() {
return $this->enabled;
}
private function _name($key) {
return sprintf('%s%s%s', $this->dir, $this->prefix, sha1($key) . '.php');
}
}
?>