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. using System.Reflection;

    基础代码: public interface IDBHelper { void Query(); } public class DBHelper : IDBHelper { public int Id ...

  2. 自定义TREEVIEW UL无限极嵌套

    背景:做一个多级图片分类管理,当然要用到TreeView,在asp.net中已经提供了此服务器控件,参照效果,自定义一个简单可控性高的就当做练手吧! 效果:如图,小图标 折叠 展开    ico-tr ...

  3. 4月8日学习笔记(js基础)

    <script>标签放在<body>和<head> 放在 <head></head> 里的会比放在 <body></bod ...

  4. android 数据库的创建

    主java package com.itheima.createdatabase; import android.app.Activity; import android.content.Contex ...

  5. JS自定义属性的设置与获取

    以前感觉用JQuery来设置自定义属性很方便,现在没有用JQuery,要用原生的JavaScript来操作自定义属性. Jquery操作自定义属性的方法,很简洁: $("#test" ...

  6. NGINX+UWSGI 莫名发生Nginx 502 Bad Gateway错误的排查过程

    自己有个阿里云UBUNTU运行的Django站,使用NGINX+UWSGI驱动,今天登陆系统后台更新内容出现了几个大字:Nginx 502 Bad Gateway,一看情况不好,这是要糟糕啊. 啊西八 ...

  7. Setfocus - IE 需要使用setTimeout

    setTimeout(function () { $('#controlid').focus(); }, 100); document.getElementById('filterPopupInput ...

  8. php 操作数组 (合并,拆分,追加,查找,删除等)

    1. 合并数组 array_merge()函数将数组合并到一起,返回一个联合的数组.所得到的数组以第一个输入数组参数开始,按后面数组参数出现的顺序依次迫加.其形式为: array array_merg ...

  9. 考研路之C语言

    今天在学习C的时候,又学到了一些新的内容,所以赶紧记录下来. case 1: #include <stdio.h> union exa{ struct{ int a; int b; }ou ...

  10. FIFO、LRU、OPT这三种置换算法的缺页次数

    考虑下述页面走向: 1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6 当内存块数量分别为3时,试问FIFO.LRU.OPT这三种置换算法的缺页次数各是多少? 答:缺页定义 ...