Skip to content

Instantly share code, notes, and snippets.

@lbp0200
Created January 2, 2016 14:43
Show Gist options
  • Save lbp0200/260ef35bba94adf4d369 to your computer and use it in GitHub Desktop.
Save lbp0200/260ef35bba94adf4d369 to your computer and use it in GitHub Desktop.
PHP RedisClient
class Redis
{
const CONFIG_FILE = '/config/redis.php';
protected static $redis;
public static function init()
{
self::$redis = new Client(require BASE_PATH . self::CONFIG_FILE);
}
public static function set($key, $value, $time = null, $unit = null)
{
self::init();
if ($time) {
switch ($unit) {
case 'h':
$time *= 3600;
break;
case 'm':
$time *= 60;
break;
case 's':
case 'ms':
break;
default:
throw new InvalidArgumentException('单位只能是 h m s ms');
break;
}
if ($unit == 'ms') {
self::_psetex($key, $value, $time);
} else {
self::_setex($key, $value, $time);
}
} else {
self::$redis->set($key, $value);
}
}
public static function get($key)
{
self::init();
return self::$redis->get($key);
}
public static function delete($key)
{
self::init();
return self::$redis->del($key);
}
private static function _setex($key, $value, $time)
{
self::$redis->setex($key, $time, $value);
}
private static function _psetex($key, $value, $time)
{
self::$redis->psetex($key, $time, $value);
}
public static function __callStatic($name, $arguments)
{
self::init();
return call_user_func_array([self::$redis, $name], $arguments);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment