1、redis配置:
$CONFIG_REDIS = array(
array(‘host‘ => ‘192.168.19.29‘, ‘port‘ => ‘6379‘, ‘dbIndex‘ => 0, ‘password‘=>‘3695a77369be021075b480048142a3c2‘),
array(‘host‘ => ‘192.168.19.30‘, ‘port‘ => ‘6379‘, ‘dbIndex‘ => 0, ‘password‘=>‘3695a77369be021075b480048142a3c2‘)
);
2、Redis操作封装类-->UtilRedis2
class UtilRedis2 {
private static $_self = null;
private $_servers = array();
private $_conn = array();
private $_conn_keys = array();
const CONNECT_TIMEOUT = 5;
public static function &getInstance() {
// TODO Auto-generated method stub
if (null == self::$_self)
{
self::$_self = new self();
}
return self::$_self;
}
private function __construct() {
$this->_servers = $GLOBALS[‘CONFIG_REDIS‘];
}
private function getConnection( $key ) {
$serverCnt = count( $this->_servers );
$hash = md5( $key );
$serverIndex = $hash % $serverCnt;
if ( !isset( $this->_conn[ $serverIndex ] ) ) {
$this->_conn[ $serverIndex ] = new Redis();
$this->_conn[ $serverIndex ]->pconnect(
$this->_servers[$serverIndex][‘host‘],
$this->_servers[$serverIndex][‘port‘],
self::CONNECT_TIMEOUT
);
$this->_conn[ $serverIndex ]->auth($this->_servers[$serverIndex][‘password‘]);
$this->_conn[ $serverIndex ]->select( $this->_servers[$serverIndex][‘dbIndex‘] );
}
return $this->_conn[ $serverIndex ];
}
public function set( $key, $value, $expires = 0 ) {
$conn = $this->getConnection( $key );
if( $conn->set( $key, $value ) && $expires > 0 )
return $conn->setTimeout($key, $expires);
return true;
}
......
3、使用redis操作封装类
$redis = UtilRedis2::getInstance();
$redis->set("development", "wangwu");