forked from leftypol/leftypol
cache.php: add unix socket connection support to Redis cache
This commit is contained in:
parent
696f084749
commit
87899a0988
1 changed files with 24 additions and 16 deletions
|
@ -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();
|
||||
|
@ -18,7 +18,16 @@ class Cache {
|
|||
break;
|
||||
case 'redis':
|
||||
self::$cache = new Redis();
|
||||
self::$cache->connect($config['cache']['redis'][0], $config['cache']['redis'][1]);
|
||||
|
||||
$ret = explode(':', $config['cache']['redis'][0]);
|
||||
if (count($ret) > 0) {
|
||||
// Unix socket.
|
||||
self::$cache->connect($ret[1]);
|
||||
} else {
|
||||
// IP + port.
|
||||
self::$cache->connect($ret[0], $config['cache']['redis'][1]);
|
||||
}
|
||||
|
||||
if ($config['cache']['redis'][2]) {
|
||||
self::$cache->auth($config['cache']['redis'][2]);
|
||||
}
|
||||
|
@ -31,9 +40,9 @@ class Cache {
|
|||
}
|
||||
public static function get($key) {
|
||||
global $config, $debug;
|
||||
|
||||
|
||||
$key = $config['cache']['prefix'] . $key;
|
||||
|
||||
|
||||
$data = false;
|
||||
switch ($config['cache']['enabled']) {
|
||||
case 'memcached':
|
||||
|
@ -67,20 +76,20 @@ class Cache {
|
|||
$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)
|
||||
|
@ -107,15 +116,15 @@ class Cache {
|
|||
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':
|
||||
if (!self::$cache)
|
||||
|
@ -142,13 +151,13 @@ class Cache {
|
|||
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)
|
||||
|
@ -170,8 +179,7 @@ class Cache {
|
|||
self::init();
|
||||
return self::$cache->flushDB();
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue