forked from GithubBackups/vichan
implement non-expiring store() function
this will allow longer term storage of data
This commit is contained in:
parent
b9e443a1e2
commit
303ed52812
63
inc/cache.php
Normal file → Executable file
63
inc/cache.php
Normal file → Executable file
@ -10,7 +10,7 @@ class Cache {
|
|||||||
private static $cache;
|
private static $cache;
|
||||||
public static function init() {
|
public static function init() {
|
||||||
global $config;
|
global $config;
|
||||||
|
|
||||||
switch ($config['cache']['enabled']) {
|
switch ($config['cache']['enabled']) {
|
||||||
case 'memcached':
|
case 'memcached':
|
||||||
self::$cache = new Memcached();
|
self::$cache = new Memcached();
|
||||||
@ -31,9 +31,9 @@ class Cache {
|
|||||||
}
|
}
|
||||||
public static function get($key) {
|
public static function get($key) {
|
||||||
global $config, $debug;
|
global $config, $debug;
|
||||||
|
|
||||||
$key = $config['cache']['prefix'] . $key;
|
$key = $config['cache']['prefix'] . $key;
|
||||||
|
|
||||||
$data = false;
|
$data = false;
|
||||||
switch ($config['cache']['enabled']) {
|
switch ($config['cache']['enabled']) {
|
||||||
case 'memcached':
|
case 'memcached':
|
||||||
@ -67,20 +67,20 @@ class Cache {
|
|||||||
$data = json_decode(self::$cache->get($key), true);
|
$data = json_decode(self::$cache->get($key), true);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($config['debug'])
|
if ($config['debug'])
|
||||||
$debug['cached'][] = $key . ($data === false ? ' (miss)' : ' (hit)');
|
$debug['cached'][] = $key . ($data === false ? ' (miss)' : ' (hit)');
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
public static function set($key, $value, $expires = false) {
|
public static function set($key, $value, $expires = false) {
|
||||||
global $config, $debug;
|
global $config, $debug;
|
||||||
|
|
||||||
$key = $config['cache']['prefix'] . $key;
|
$key = $config['cache']['prefix'] . $key;
|
||||||
|
|
||||||
if (!$expires)
|
if (!$expires)
|
||||||
$expires = $config['cache']['timeout'];
|
$expires = $config['cache']['timeout'];
|
||||||
|
|
||||||
switch ($config['cache']['enabled']) {
|
switch ($config['cache']['enabled']) {
|
||||||
case 'memcached':
|
case 'memcached':
|
||||||
if (!self::$cache)
|
if (!self::$cache)
|
||||||
@ -107,15 +107,50 @@ class Cache {
|
|||||||
self::$cache[$key] = $value;
|
self::$cache[$key] = $value;
|
||||||
break;
|
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'])
|
if ($config['debug'])
|
||||||
$debug['cached'][] = $key . ' (set)';
|
$debug['cached'][] = $key . ' (set)';
|
||||||
}
|
}
|
||||||
public static function delete($key) {
|
public static function delete($key) {
|
||||||
global $config, $debug;
|
global $config, $debug;
|
||||||
|
|
||||||
$key = $config['cache']['prefix'] . $key;
|
$key = $config['cache']['prefix'] . $key;
|
||||||
|
|
||||||
switch ($config['cache']['enabled']) {
|
switch ($config['cache']['enabled']) {
|
||||||
case 'memcached':
|
case 'memcached':
|
||||||
case 'redis':
|
case 'redis':
|
||||||
@ -138,13 +173,13 @@ class Cache {
|
|||||||
unset(self::$cache[$key]);
|
unset(self::$cache[$key]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($config['debug'])
|
if ($config['debug'])
|
||||||
$debug['cached'][] = $key . ' (deleted)';
|
$debug['cached'][] = $key . ' (deleted)';
|
||||||
}
|
}
|
||||||
public static function flush() {
|
public static function flush() {
|
||||||
global $config;
|
global $config;
|
||||||
|
|
||||||
switch ($config['cache']['enabled']) {
|
switch ($config['cache']['enabled']) {
|
||||||
case 'memcached':
|
case 'memcached':
|
||||||
if (!self::$cache)
|
if (!self::$cache)
|
||||||
@ -166,7 +201,7 @@ class Cache {
|
|||||||
self::init();
|
self::init();
|
||||||
return self::$cache->flushDB();
|
return self::$cache->flushDB();
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user