php的redis 操作类,适用于单台或多台、多组redis服务器操作
redis 操作类,包括单台或多台、多组redis服务器操作,适用于业务复杂、高性能要求的 php web 应用。
redis.php:
<?php
/*
redis 操作类,适用于单台或多台、多组redis服务器操作 使用方法:
1、$rs=new mz_redis();$rs->load_config_file('redis_config1.php');$www=$rs->connect(1,true,0)==单台读连接,连接read_array第一个元素对应的redis服务器中的随机一台;$rs->get($www[0],'abc'),获取$www连接对象里的'abc'key的值。
2、$rs=new mz_redis();$rs->load_config_file('redis_config2.php');$www=$rs->connect(1,true,1)==单台读连接,连接read_array第二个元素对应的redis服务器中的随机一台
3、$rs=new mz_redis();$rs->load_config_file('redis_config3.php');$www=$rs->connect(1,false,0)==多台读连接,连接read_array每一个元素对应的redis服务器中的随机一台;数组形式的连接对象$www,需要循环去操作,与第一种方式有所区别
4、$rs=new mz_redis();$rs->load_config_file('redis_config4.php');$www=$rs->connect(2,false,0)==多台写连接,连接write_array每一个元素对应的redis服务器
5、$rs=new mz_redis();$rs->load_config_file('redis_config5.php');$www=$rs->connect(2,true,0)==单台写连接,连接write_array第一个元素对应的redis服务器
注意:$www是数组,redis有很多操作方法,本类并未完全包括,简单的都可以自己扩展,这个类主要“单台或多台、多组redis服务器连接操作”
有问题联系 QQ 8704953 。
*/ class pub_redis{ private $read_link=array(); // 一维数组读资源
private $write_link=array(); // 一维数组写资源 private $read_array=array(); // 二维数组
private $write_array=array(); // 二维数组 /*
* 构造函数
*/
public function __construct(){ if (!extension_loaded('redis')) {
exit('服务器不支持redis扩展');
} } /*
* 初始化 redis 读写配置数组,都是二维数组
* 不能业务类型的redis应用,配置到不同的文件中
* 切换不同业务类型redis的连接,只需要执行本方法导入不同的redis配置文件,然后connect()
*/
public function load_config_file($redis_config_file='redis_config1.php'){ require_once($redis_config_file);
$this->read_array=$read_array;
$this->write_array=$write_array;
$read_array=$write_array=null; } /*
* 连接函数,redis链接入口
* $single==true,单台操作 ; false就是多台操作
* type==1:read ; type==2:write
* $index,单台操作,指定操作某一台,数组的索引
* 返回redis链接资源,一维数组形式,下标为从0开始的数字
*/
public function connect($type=1,$single=true,$index=0){ if($type==1){
if($single){
$idx=array_rand($this->read_array[$index]);
$data=array(array($this->read_array[$index][$idx]));
}
else{
$data=array();
foreach($this->read_array as $key=>$val){
$idx=array_rand($val);
$data[]=array($this->read_array[$key][$idx]);
}
}
$this->mz_connect($data,$this->read_link,$single,$index);
$rs=$this->read_link;
}
else if($type==2){
$this->mz_connect($this->write_array,$this->write_link,$single,$index);
$rs=$this->write_link;
}
else{
exit('参数错误');
} sort($rs);
return $rs; } /*
* 连接资源数组化
*/
public function mz_connect($array,&$link,$single,$index){
if($single){
if(!isset($link[$array[$index]['ip']]) || $link[$array[$index]['ip']]===false){
$link[$array[$index]['ip']]=$this->do_connect($array[$index]['ip'],$array[$index]['pwd'],$array[$index]['port'],$array[$index]['time_out'],$array[$index]['db']);
}
}
else{
$num=count($array);
for($i=0;$i<$num;++$i){
$index=array_rand($array);
if(!isset($link[$array[$index]['ip']]) || $link[$array[$index]['ip']]===false){
$link[$array[$index]['ip']]=$this->do_connect($array[$index]['ip'],$array[$index]['pwd'],$array[$index]['port'],$array[$index]['time_out'],$array[$index]['db']);
}
unset($array[$index]);
}
}
} /*
* 连接函数,执行连接
* 连接redis与选择数据库,并确认是否可以正常连接,连接不上就返回false
*/
public function do_connect($ip,$pwd='',$port=6379,$time_out=0.3,$db=1){ $redis = new Redis();
try {
$redis->connect($ip,$port,$time_out);
if($pwd!=''){
$redis->auth($pwd);
}
$redis->select($db); } catch (Exception $e) {
$redis=false;
}
return $redis;
} /*
* 判断key是否存在
* $obj redis连接对象
*/
public function key_exists($obj,$key){
return $obj->exists($key);
} /*
* 判断key剩余有效时间,单位秒
* $obj redis连接对象
*/
public function get_ttl($obj,$key){
return $obj->ttl($key);
} /*
* 获取字符串对象
* $obj redis连接对象
*/
public function get($obj,$key){
return json_decode($obj->get($key));
} /*
* 设置字符串,带生存时间
* $obj redis连接对象
*/
public function set($obj,$key,$time,$value){
$str=json_encode($value);
return $obj->setex($key,$time,$str);
} /*
* 设置锁
* $obj redis连接对象
* $str, 字符串
*/
public function set_lock($obj,$key,$value){
return $obj->setnx($key,$value);
} /*
* 删除key
* $obj redis连接对象
*/
public function delete_key($obj,$key){
return $obj->delete($key);
} /*
* 链表增加多个元素
* $obj redis连接对象
*/
public function list_add_element($obj,$key,$array,$direction='left'){
if(!is_array($array)){
$array=array($array);
}
foreach($array as $val){
($direction == 'left') ? $obj->lPush($key, json_encode($val)) : $obj->rPush($key, json_encode($val));
}
} /*
* 链表弹出多个元素
* $obj redis连接对象
* 返回数组
*/
public function list_pop_element($obj,$key,$num=1,$direction='right') {
for($i=0;$i<$num;$i++){
$value = ($direction == 'right') ? $obj->rPop($key) : $obj->lPop($key);
$data[]=json_decode($value);
}
return $data;
} /*
* 哈希表新增或修改元素
* $obj redis连接对象
* $array 关联数组
*/
public function hash_set($obj,$key,$array){
if(!$is_array($array)){
exit('设置哈希表参数异常');
}
$obj->hmset($key,$array);
} /*
* 哈希表读取元素
* $obj redis连接对象
* $array 关联数组
*/
public function hash_get($obj,$key,$array){
if(!$is_array($array)){
return $obj->hget($key,$array);
}
return $obj->hmget($key,$array);
} } ?>
redis_config1.php:
<?php
/*
* 读写redis配置
* 读写数组下标相同,为主从关系
*/
$write_array=array(
0=>array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'123456','db'=>1),
1=>array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'123456','db'=>1)
); $read_array=array(
0=>array(
array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'654321','db'=>1),
array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'123456','db'=>1)
),
1=>array(
array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'5678765','db'=>1),
array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'345678','db'=>1)
)
); ?>
redis_config2.php
<?php
/*
* 读写redis配置
* 读写数组下标相同,为主从关系
*/
$write_array=array(
0=>array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'123456','db'=>1),
1=>array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'123456','db'=>1)
); $read_array=array(
0=>array(
array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'654321','db'=>1),
array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'123456','db'=>1)
),
1=>array(
array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'5678765','db'=>1),
array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'345678','db'=>1)
)
); ?>
链接:
http://outofmemory.cn/code-snippet/2728/php-redis-operation-class-shiyong-yu-dantai-huo-duotai-duozu-redis-fuwuqi-operation
php的redis 操作类,适用于单台或多台、多组redis服务器操作的更多相关文章
- 2.NetDh框架之简单高效的日志操作类(附源码和示例代码)
前言 NetDh框架适用于C/S.B/S的服务端框架,可用于项目开发和学习.目前包含以下四个模块 1.数据库操作层封装Dapper,支持多种数据库类型.多库实例,简单强大: 此部分具体说明可参考博客: ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战
笔记 4.Redis工具类封装讲解和实战 简介:高效开发方式 Redis工具类封装讲解和实战 1.常用客户端 https://redisdesktop.com/download ...
- DbHelper数据操作类
摘要:本文介绍一下DbHelper数据操作类 微软的企业库中有一个非常不错的数据操作类.但是,不少公司(起码我遇到的几个...),对一些"封装"了些什么的东西不太敢用,虽然我推荐过 ...
- (转)C# Oracle数据库操作类
原文地址:http://www.cnblogs.com/haibing0107/p/6143922.html using System;using System.Data;using System.C ...
- 扣出thinkphp数据库操作类
假如你是一位thinkphp的使用者,想必你会觉得thinkphp操作数据库非常方便.现在在你面前有一个非常小的作业,小到完全没有必要用thinkphp去完成它.但是你又觉得不用thinkphp的话, ...
- php socket通信演示以及socket操作类
准备做Java的课程设计,一个通讯录.采用C/S架构.客户端用java FX和Java,服务器端用php,采用socket通信. 下面来讲一讲php的socket通信: 讲之前,得先讲一下TCP/IP ...
- C# Oracle数据库操作类
using System; using System.Data; using System.Collections.Generic; using System.Configuration; using ...
- Java并发基础10:原子性操作类的使用
在 java5 以后,我们接触到了线程原子性操作,也就是在修改时我们只需要保证它的那个瞬间是安全的即可,经过相应的包装后可以再处理对象的并发修改,本文总结一下Atomic系列的类的使用方法,其中包含: ...
- SQL Server跨服务器操作数据库
今天给大家来分享一下跨服务器操作数据库,还是以SQL Server的管理工具(SSMS)为平台进行操作. 什么是跨服务器操作? 跨服务器操作就是可以在本地连接到远程服务器上的数据库,可以在对方的数据库 ...
随机推荐
- JAVA UDP网络编程学习笔记
一.UDP网络编程概述 采用TCP协议通信时,客户端的Socket必须先与服务器建立连接,连接建立成功后,服务器端也会持有客户端连接的Socket,客户端的Socket与服务器端的Socket是对应的 ...
- Contoso 大学 - 9 - 实现仓储和工作单元模式
原文 Contoso 大学 - 9 - 实现仓储和工作单元模式 By Tom Dykstra, Tom Dykstra is a Senior Programming Writer on Micros ...
- WPF、Silverlight项目中使用柱状图、饼状图、折线图
在开发的过程中,可能会遇到柱状图.饼状图.折线图来更好的显示数据,最近整理了一下,遂放出来望需要的朋友可以参考.本文仅仅是简单显示,如需复杂显示效果请参考官网代码示例.----本文代码使用WPF,Si ...
- Call C# code from C++
Reference: https://support.microsoft.com/en-us/kb/828736 Calling C# .NET methods from unmanaged C/C+ ...
- Android 自定义CheckBox 样式
新建Android XML文件,类型选Drawable,根结点选selector,在这定义具体的样式. <?xml version="1.0" encoding=" ...
- Bind安装配置及应用
Bind安装配置及应用 BIND:Berkeley Internet Name Domain ,ISC.org DNS服务的实现: 监听端口:53/UDP , 53/TCP 程 ...
- Cassandra1.2文档学习(18)—— CQL数据模型(下)
三.集合列 CQL 3 引入了一下集合类型: •set •list •map 在关系型数据库中,允许用户拥有多个email地址,你可以创建一个email_addresses表与users表存在一个多对 ...
- php入门变量
变量是用于临时存储值的容器.这些值可以是数字.文本,或者是复杂得多的数据. PHP 具有8种变量. 其中包括4种标量(单值)类型——字符串型(字符).整型.浮点型(小数)和布尔型(TRUE或FALSE ...
- 实例讲解如何在Delphi中动态创建dxBarManager内容
一.dxBarManager中一些非常重要的概念: TCategorys:为了方便对dxBarManager中的项目进行归类而设计的一个属性,当然,只使用默认的名字为Default的Category也 ...
- Python操作RabbitMQ初体验(一)
由于想用Python实现一套分布式系统,来管理和监控CDN的内容与运行状态,误打误撞认识了RabbitMQ,推荐的人很多,如余锋<我为什么要选择RabbitMQ>等等. 在MQ这个词汇映入 ...