<?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操作类的更多相关文章

  1. php的redis 操作类,适用于单台或多台、多组redis服务器操作

    redis 操作类,包括单台或多台.多组redis服务器操作,适用于业务复杂.高性能要求的 php web 应用. redis.php: <?php /* redis 操作类,适用于单台或多台. ...

  2. 设计模式之PHP项目应用——单例模式设计Memcache和Redis操作类

    1 单例模式简单介绍 单例模式是一种经常使用的软件设计模式. 在它的核心结构中仅仅包括一个被称为单例类的特殊类. 通过单例模式能够保证系统中一个类仅仅有一个实例并且该实例易于外界訪问.从而方便对实例个 ...

  3. 封装一个redis操作类来操作hash格式

    最近项目要用redis,依然是基于tp3.2. 发现thinkphp3.2自带的缓存类并不好使用,就自己封装了一个 目前只支持hash格式,其他数据类型的操作后面用到的时候再补充 <?php / ...

  4. spring 的redis操作类RedisTemplate

    spring 集成的redis操作几乎都在RedisTemplate内了. 已spring boot为例, 再properties属性文件内配置好 redis的参数 spring.redis.host ...

  5. Java的redis 操作类-优化通用版本

    java操作redis多节点处理方式;http://blog.itpub.net/29254281/viewspace-1188644/首先maven引入依赖包 <dependency> ...

  6. 用C#封装的ServiceStack.redis操作类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. <记录> PHP Redis操作类

    namespace common\controller; class Redis { public $redisObj = null; //redis实例化时静态变量 static protected ...

  8. PHP redis操作类 个人总结

    <pre name="code" class="php"><span style="font-size:18px;"> ...

  9. 一个简单清晰的Redis操作类

    <?php /** * redis处理的二次封装 * */ class Redis{ private $_redis; private $_config; public function __c ...

随机推荐

  1. js实现事件模型bind与trigger

    function Emitter() { this._listener = [];//_listener[自定义的事件名] = [所用执行的匿名函数1, 所用执行的匿名函数2] } //注册事件 Em ...

  2. 第2章 使用JavaScript

    学习要点: 1.创建一张HTML页面 2.<Script>标签解析 3.JS代码嵌入的一些问题

  3. ellipsis

    语法:  text-overflow : clip | ellipsis 参数:  clip : 不显示省略标记(...),而是简单的裁切(clip这个参数是不常用的!)      ellipsis ...

  4. js打开没有地址栏下拉条新窗口

    <script type="text/javascript" language="javascript"> function vNodeAuditL ...

  5. Web前端开发基础 第一天(Html和CSS)

    学习web前端开发基础技术需要掌握:HTML.CSS.JavaScript语言.下面我们就来了解下这三门技术都是用来实现什么的: 1. HTML是网页内容的载体.内容就是网页制作者放在页面上想要让用户 ...

  6. c#根据后台数据,自动生成checkbox

    前端在aspx中,添加生成checkbox的容器div: <div id="container" runat="server"></div&g ...

  7. How to bind data to a user control

    http://support.microsoft.com/kb/327413 Create a user control  by inheriting from the System.Windows. ...

  8. Git command line

    # Pull the repo from master git pull # Create branch for myself in local git branch john/jenkins_cod ...

  9. dns服务

    http://33024.blog.163.com/blog/static/12307042220119179237568/

  10. Elasticsearch基本操作

    ElasticSearch操作说明   活动 方法 url Body 集群的相关操作 查看健康 Get http://localhost:9200/_cluster/health 查看节点 Get h ...