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服务器操作的更多相关文章

  1. 2.NetDh框架之简单高效的日志操作类(附源码和示例代码)

    前言 NetDh框架适用于C/S.B/S的服务端框架,可用于项目开发和学习.目前包含以下四个模块 1.数据库操作层封装Dapper,支持多种数据库类型.多库实例,简单强大: 此部分具体说明可参考博客: ...

  2. 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战

    笔记 4.Redis工具类封装讲解和实战     简介:高效开发方式 Redis工具类封装讲解和实战         1.常用客户端 https://redisdesktop.com/download ...

  3. DbHelper数据操作类

    摘要:本文介绍一下DbHelper数据操作类 微软的企业库中有一个非常不错的数据操作类.但是,不少公司(起码我遇到的几个...),对一些"封装"了些什么的东西不太敢用,虽然我推荐过 ...

  4. (转)C# Oracle数据库操作类

    原文地址:http://www.cnblogs.com/haibing0107/p/6143922.html using System;using System.Data;using System.C ...

  5. 扣出thinkphp数据库操作类

    假如你是一位thinkphp的使用者,想必你会觉得thinkphp操作数据库非常方便.现在在你面前有一个非常小的作业,小到完全没有必要用thinkphp去完成它.但是你又觉得不用thinkphp的话, ...

  6. php socket通信演示以及socket操作类

    准备做Java的课程设计,一个通讯录.采用C/S架构.客户端用java FX和Java,服务器端用php,采用socket通信. 下面来讲一讲php的socket通信: 讲之前,得先讲一下TCP/IP ...

  7. C# Oracle数据库操作类

    using System; using System.Data; using System.Collections.Generic; using System.Configuration; using ...

  8. Java并发基础10:原子性操作类的使用

    在 java5 以后,我们接触到了线程原子性操作,也就是在修改时我们只需要保证它的那个瞬间是安全的即可,经过相应的包装后可以再处理对象的并发修改,本文总结一下Atomic系列的类的使用方法,其中包含: ...

  9. SQL Server跨服务器操作数据库

    今天给大家来分享一下跨服务器操作数据库,还是以SQL Server的管理工具(SSMS)为平台进行操作. 什么是跨服务器操作? 跨服务器操作就是可以在本地连接到远程服务器上的数据库,可以在对方的数据库 ...

随机推荐

  1. 第三十二篇、iOS 10开发

    1.语音识别 苹果官方在文档中新增了API   Speech,那么在以前我们处理语音识别非常的繁琐甚至很多时候可能需要借助于第三方框架处理,那么苹果推出了这个后,我们以后处理起来就非常的方便了,spe ...

  2. XCode中的特殊快捷键图标

    ⌘——Command () ⌃ ——Control ⌥——Option (alt) ⇧——Shift ⇪——Caps Lock fn——功能键就是fn

  3. krpano资料

  4. @Resource和@Autowired的区别

    @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了.@Resource有两个属性是比较重要的,分 ...

  5. (UVALive 7261)Xiongnu's Land 二分

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  6. java基础-基础类型包装类型

    想要对基本类型数据进行更多的操作,最方便的方式就是将其封装成对象. 为啥呢?因为在对象描述中就可以定义更多的属性和行为对该基本数据类型进行操作. [八种基本数据类型的包装类] byte --Byte ...

  7. iOS 非ARC基本内存管理系列 2-多对象内存管理(2)

    /* 多对象内存管理: 以人拥有车为例涉及到@property底层set方法管理内存的实现 注意:人在换车的时候要进行当前传入的车和人所拥有的车进行判断 */ /******************* ...

  8. 在Mac OS X中使用VIM开发STM32(4)

    本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重!       在上三篇文章中,我们基本搭建好了开发STM32的IDE环境,当然vim.ctags.tagl ...

  9. [转帖]译文:如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)

    原文链接:http://norke.blog.163.com/blog/static/276572082011828104315941/ 引言 我一直在探寻一个高性能的Socket客户端代码.以前,我 ...

  10. CocoaPods安装和使用及问题:Setting up CocoaPods master repo

    CocoaPods是什么? 当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等.可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库,而 ...