Skip to content
This repository was archived by the owner on Jun 13, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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