php使用redis做缓存,PHP程序连接多个redis实例做缓存

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");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值