Redis連接類代碼, 注意, 本代碼是阻塞的,需要搭配Yaf配置文档。
class Redis_Worker
{
/**
* 業務邏輯用長連接線程
* @var array
*/
private static $_redisp = [];
/**
* 啓動連接
* @access public static
* @param integer $dbIndex
* @return object
*/
public static function connect(int $dbIndex = 0)
{
$redis = new \Redis();
$config = Yaf\Registry::get('config')->redis;
$redis->connect($config->host, (int) $config->port);
$redis->auth($config->auth);
$redis->select($dbIndex);
return $redis;
}
/**
* 長連接句柄
* 根據庫序號産生多個句柄容器
* @access public
* @param integer $dbIndex 庫號
* @return object
*/
public static function pconnect(int $dbIndex = 0)
{
if (!isset(self::$_redisp[$dbIndex])) {
self::$_redisp[$dbIndex] = new \Redis();
}
$redis = &self::$_redisp[$dbIndex];
$config = Yaf\Registry::get('config')->redis;
$redis->pconnect($config->host, (int) $config->port);
$redis->auth($config->auth);
$redis->select($dbIndex);
return self::$_redisp[$dbIndex];
}
}爲什麽不封裝其他數據操作方法?
簡直多餘。不想浪費別人腦細胞,phpredis庫本身已經封裝的很好了, 還封裝個屁啊。。。