Browse Source

Add cache mechanism for any data

tags/v1.6.5
Adam Bukowski 6 years ago
parent
commit
ad6e34bb4a
2 changed files with 91 additions and 0 deletions
  1. +1
    -0
      include.php
  2. +90
    -0
      include/cache.php

+ 1
- 0
include.php View File

@@ -21,6 +21,7 @@ require_once('include/error.php');
require_once('include/magicquotes.php');
require_once('include/database.php');
require_once('include/session.php');
require_once('include/cache.php');
require_once('include/variables.php');
require_once('include/history.php');
require_once('include/misc.php');


+ 90
- 0
include/cache.php View File

@@ -0,0 +1,90 @@
<?php
/**
* @author Adam Bukowski <abukowski@telaxus.com>
* @version 1.0
* @copyright Copyright &copy; 2015, Telaxus LLC
* @license MIT
* @package epesi-base
*/
defined("_VALID_ACCESS") || die('Direct access forbidden');

class FileCache
{
private $file;
private $data = array();

function __construct($file)
{
$this->file = $file;
$this->init();
}

protected function init()
{
$data = false;
if (file_exists($this->file)) {
$file_data = file_get_contents($this->file);
if ($file_data !== false) {
$data = @unserialize($file_data);
}
}
$this->data = is_array($data) ? $data : array();
}

protected function save()
{
$data = serialize($this->data);
file_put_contents($this->file, $data);
}

public function get($name, $default = null)
{
$ret = isset($this->data[$name]) ? $this->data[$name] : $default;
return $ret;
}

public function set($name, $value)
{
$this->data[$name] = $value;
$this->save();
}

public function clear($name = null)
{
if ($name === null) {
$this->data = array();
} else {
unset($this->data[$name]);
}
$this->save();
}

}

class Cache
{
protected static $cache_object;

public static function init($cache_object)
{
self::$cache_object = $cache_object;
}

public static function get($name, $default = null)
{
return self::$cache_object->get($name, $default);
}

public static function set($name, $value)
{
return self::$cache_object->set($name, $value);
}

public static function clear($name = null)
{
return self::$cache_object->clear($name);
}

}

Cache::init(new FileCache(rtrim(FILE_SESSION_DIR, '\\/') . '/' . FILE_SESSION_TOKEN . 'cache'));

Loading…
Cancel
Save