实用Redis操作类
<?php /**
* ------------------------------------------
* 统一redis的配置与数据存储规范,便于扩展与修改
* # redis通常用于热数据与消息列队等场景
* # list内存储array是采用json格式
*
*/ class RedisDriver
{ protected $redis; // redis对象
protected $ip = '127.0.0.1'; // redis服务器ip地址
protected $port = '6379'; // redis服务器端口
protected $passwd = null; // redis密码 public function __construct($config = array())
{
$this->redis = new Redis();
empty($config) or $this->connect($config);
} // 连接redis服务器
public function connect($config = array())
{
if (!empty($config)) {
$this->ip = $config['ip'];
$this->port = $config['port'];
if (isset($config['passwd'])) {
$this->passwd = $config['passwd'];
}
}
$state = $this->redis->connect($this->ip, $this->port);
if ($state == false) {
die('redis connect failure');
}
if (!is_null($this->passwd)) {
$this->redis->auth($this->passwd);
}
} // 设置一条String
public function setStr($key, $text, $expire = null)
{
$key = 'string:' . $key;
$this->redis->set($key, $text);
if (!is_null($expire)) {
$this->redis->setTimeout($key, $expire);
}
} // 获取一条String
public function getStr($key)
{
$key = 'string:' . $key;
$text = $this->redis->get($key);
return empty($text) ? null : $text;
} // 删除一条String
public function delStr($key)
{
$key = 'string:' . $key;
$this->redis->del($key);
} // 设置一条Hash
public function setHash($key, $arr, $expire = null)
{
$key = 'hash:' . $key;
$this->redis->hMset($key, $arr);
if (!is_null($expire)) {
$this->redis->setTimeout($key, $expire);
}
} // 获取一条Hash,$fields可为字符串或数组
public function getHash($key, $fields = null)
{
$key = 'hash:' . $key;
if (is_null($fields)) {
$arr = $this->redis->hGetAll($key);
} else {
if (is_array($fields)) { $arr = $this->redis->hmGet($key, $fields);
foreach ($arr as $key => $value) {
if ($value === false) {
unset($arr[$key]);
}
}
} else {
$arr = $this->redis->hGet($key, $fields);
}
}
return empty($arr) ? null : (is_array($arr) ? $arr : array($fields => $arr));
} // 删除一条Hash,$field为字符串
public function delHash($key, $field = null)
{
$key = 'hash:' . $key;
if (is_null($field)) {
$this->redis->del($key);
} else {
$this->redis->hDel($key, $field);
}
} // 在Hash的field内增加一个值 (值之间使用“,”分隔)
public function fieldAddVal($key, $field, $val)
{
$arr = $this->getHash($key, $field);
if (!is_null($arr)) {
$str = reset($arr);
$arr = explode(',', $str);
foreach ($arr as $v) {
if ($v == $val) {
return;
}
}
$str .= ",{$val}";
$this->setHash($key, array($field => $str));
} else {
$this->setHash($key, array($field => $val));
}
} // 在Hash的field内删除一个值
public function fieldDelVal($key, $field, $val)
{
$arr = $this->getHash($key, $field);
if (!is_null($arr)) {
$arr = explode(',', reset($arr));
$tmpStr = '';
foreach ($arr as $v) {
if ($v != $val) {
$tmpStr .= ",{$v}";
}
}
if ($tmpStr == '') {
$this->delHash($key, $field);
} else {
$this->setHash($key, array($field => substr($tmpStr, 1)));
}
}
} // 设置表格的一行数据
public function setTableRow($table, $id, $arr, $expire = null)
{
$key = '' . $table . ':' . $id;
$this->redis->hMset($key, $arr);
if (!is_null($expire)) {
$this->redis->setTimeout($key, $expire);
}
} // 获取表格的一行数据,$fields可为字符串或数组
public function getTableRow($table, $id, $fields = null)
{
$key = '' . $table . ':' . $id;
if (is_null($fields)) {
$arr = $this->redis->hGetAll($key);
} else {
if (is_array($fields)) {
$arr = $this->redis->hmGet($key, $fields);
foreach ($arr as $key => $value) {
if ($value === false) {
unset($arr[$key]);
}
}
} else {
$arr = $this->redis->hGet($key, $fields);
}
}
return empty($arr) ? null : (is_array($arr) ? $arr : array($fields => $arr));
} // 删除表格的一行数据
public function delTableRow($table, $id)
{
$key = '' . $table . ':' . $id;
$this->redis->del($key);
} // 推送一条数据至列表,头部
public function pushList($key, $arr)
{
$key = 'list:' . $key;
$this->redis->lPush($key, json_encode($arr));
} // 从列表拉取一条数据,尾部
public function pullList($key, $timeout = 0)
{
$key = 'list:' . $key;
if ($timeout > 0) {
$val = $this->redis->brPop($key, $timeout); // 该函数返回的是一个数组, 0=key 1=value
} else {
$val = $this->redis->rPop($key);
}
$val = is_array($val) && isset($val[1]) ? $val[1] : $val;
return empty($val) ? null : $this->objectToArray(json_decode($val));
} // 取得列表的数据总条数
public function getListSize($key)
{
$key = 'list:' . $key;
return $this->redis->lSize($key);
} // 删除列表
public function delList($key)
{
$key = 'list:' . $key;
$this->redis->del($key);
} // 使用递归,将stdClass转为array
protected function objectToArray($obj)
{
if (is_object($obj)) {
$arr = (array) $obj;
}
if (is_array($obj)) {
foreach ($obj as $key => $value) {
$arr[$key] = $this->objectToArray($value);
}
}
return !isset($arr) ? $obj : $arr;
} }
实用Redis操作类的更多相关文章
- php的redis 操作类,适用于单台或多台、多组redis服务器操作
redis 操作类,包括单台或多台.多组redis服务器操作,适用于业务复杂.高性能要求的 php web 应用. redis.php: <?php /* redis 操作类,适用于单台或多台. ...
- 设计模式之PHP项目应用——单例模式设计Memcache和Redis操作类
1 单例模式简单介绍 单例模式是一种经常使用的软件设计模式. 在它的核心结构中仅仅包括一个被称为单例类的特殊类. 通过单例模式能够保证系统中一个类仅仅有一个实例并且该实例易于外界訪问.从而方便对实例个 ...
- 封装一个redis操作类来操作hash格式
最近项目要用redis,依然是基于tp3.2. 发现thinkphp3.2自带的缓存类并不好使用,就自己封装了一个 目前只支持hash格式,其他数据类型的操作后面用到的时候再补充 <?php / ...
- spring 的redis操作类RedisTemplate
spring 集成的redis操作几乎都在RedisTemplate内了. 已spring boot为例, 再properties属性文件内配置好 redis的参数 spring.redis.host ...
- Java的redis 操作类-优化通用版本
java操作redis多节点处理方式;http://blog.itpub.net/29254281/viewspace-1188644/首先maven引入依赖包 <dependency> ...
- 用C#封装的ServiceStack.redis操作类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- <记录> PHP Redis操作类
namespace common\controller; class Redis { public $redisObj = null; //redis实例化时静态变量 static protected ...
- PHP redis操作类 个人总结
<pre name="code" class="php"><span style="font-size:18px;"> ...
- 一个简单清晰的Redis操作类
<?php /** * redis处理的二次封装 * */ class Redis{ private $_redis; private $_config; public function __c ...
随机推荐
- 学习Jquery
早就听说了Jquery的大名,一直没有细心的学习一下,通过阅读收集的一些资料,感觉Jquery真的很强大.决定开始自己的学习Jquery之旅.在这里不是为大家讲解Jquery(深知水平有限),只是将自 ...
- 2016HUAS暑假集训训练题 B - Catch That Cow
B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...
- cpu为什么使用虚拟地址到物理地址的空间映射,解决了什么样的问题?
当处理器读或写入内存位置时,它会使用虚拟地址.作为读或写操作的一部分,处理器将虚拟地址转换为物理地址.通过虚拟地址访问内存有以下优势: 程序可以使用一系列相邻的虚拟地址来访问物理内存中不相邻的大内存缓 ...
- 四个查找命令find,locate,whereis,which的区别
find最强大,但是检索硬盘,比较慢: whereis只能查二进制文件.说明文档,源文件等: locate能查所有文件,但跟whereis一样都是查数据库里的内容,速度快,但有延时: which 只能 ...
- Python的时间模块小结(转自:不懂真人)
import datetimeprint time.time() #时间戳 print time.localtime(time.time()) #时间元组 print time.strftime('% ...
- nginx编译安装
Nginx编译安装 1.nginx官网:http://wiki.nginx.org/Install下载:http://nginx.org/en/download.html 2.编译安装# wget h ...
- 【ionic】Mac IOS下真机调试
模拟调试不能保证真机一定没问题,所以真机调试是非常必要的一步 IOS设备 启用设备调试 在IOS设备中(Iphone,Ipad)中开始web检测器 设备->safari->高级->w ...
- maketrans translate
1. makestrans()用法 语法: str.maketrans(intab, outtab]); Python maketrans() 方法用于创建字符映射的转换表,对于接受两个参数的最简单的 ...
- 集合中list、ArrayList、LinkedList、Vector的区别、Collection接口的共性方法以及数据结构的总结
List (链表|线性表) 特点: 接口,可存放重复元素,元素存取是有序的,允许在指定位置插入元素,并通过索引来访问元素 1.创建一个用指定可视行数初始化的新滚动列表.默认情况下,不允许进行多项选择. ...
- 11月30日《奥威Power-BI智能分析报表制作方法》腾讯课堂开课啦
这么快一周就过去了,奥威公开课又要与大家见面咯,上节课老师教的三种报表集成方法你们都掌握了吗?大家都知道,学习的结果在于实际应用,想要熟练掌握新内容的要点就在于去应用它.正是基于这一要点,每一期的课程 ...