Skip to content
This repository has been archived by the owner on Jun 13, 2020. It is now read-only.

Write files to memory & disk or just memory #537

Open
wants to merge 10 commits into
base: public-site
Choose a base branch
from
63 changes: 49 additions & 14 deletions inc/cache.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Cache {
private static $cache;
public static function init() {
global $config;

switch ($config['cache']['enabled']) {
case 'memcached':
self::$cache = new Memcached();
Expand All @@ -31,9 +31,9 @@ public static function init() {
}
public static function get($key) {
global $config, $debug;

$key = $config['cache']['prefix'] . $key;

$data = false;
switch ($config['cache']['enabled']) {
case 'memcached':
Expand Down Expand Up @@ -67,20 +67,20 @@ public static function get($key) {
$data = json_decode(self::$cache->get($key), true);
break;
}

if ($config['debug'])
$debug['cached'][] = $key . ($data === false ? ' (miss)' : ' (hit)');

return $data;
}
public static function set($key, $value, $expires = false) {
global $config, $debug;

$key = $config['cache']['prefix'] . $key;

if (!$expires)
$expires = $config['cache']['timeout'];

switch ($config['cache']['enabled']) {
case 'memcached':
if (!self::$cache)
Expand All @@ -107,15 +107,50 @@ public static function set($key, $value, $expires = false) {
self::$cache[$key] = $value;
break;
}


if ($config['debug'])
$debug['cached'][] = $key . ' (set)';
}
public static function store($key, $value) {
global $config, $debug;

$key = $config['cache']['prefix'] . $key;

switch ($config['cache']['enabled']) {
case 'memcached':
if (!self::$cache)
self::init();
self::$cache->set($key, $value);
break;
case 'redis':
if (!self::$cache)
self::init();
self::$cache->set($key, json_encode($value));
break;
case 'apc':
apc_store($key, $value);
break;
case 'xcache':
xcache_set($key, $value);
break;
case 'fs':
$key = str_replace('/', '::', $key);
$key = str_replace("\0", '', $key);
file_put_contents('tmp/cache/'.$key, json_encode($value));
break;
case 'php':
self::$cache[$key] = $value;
break;
}

if ($config['debug'])
$debug['cached'][] = $key . ' (set)';
}
public static function delete($key) {
global $config, $debug;

$key = $config['cache']['prefix'] . $key;

switch ($config['cache']['enabled']) {
case 'memcached':
case 'redis':
Expand All @@ -138,13 +173,13 @@ public static function delete($key) {
unset(self::$cache[$key]);
break;
}

if ($config['debug'])
$debug['cached'][] = $key . ' (deleted)';
}
public static function flush() {
global $config;

switch ($config['cache']['enabled']) {
case 'memcached':
if (!self::$cache)
Expand All @@ -166,7 +201,7 @@ public static function flush() {
self::init();
return self::$cache->flushDB();
}

return false;
}
}
Expand Down
Loading