PHP redis封装类

本文详细介绍了Redis中各种数据结构的操作方法,包括队列、字符串、哈希、集合及有序集合等,并提供了PHP实现的具体代码示例。

http://redisbook.com/index.html

1.队列数据结构  lPush入队列  lPop出队列


2.字符串数据结构 set设置指定 key 的值    get获取指定 key 的值


3.哈希数据结构 hMset获取hash表的数据    hashGet获取hash表的数据


4.无序集合数据结构  sadd向集合中添加一个或多个成员   sRem移除集合中一个或多个成员


5.有序集合数据结构  zadd向有序集合中添加一个或多个成员    zRem移除有序集合中一个或多个成员


<?php

namespace Dedis;
class Dataredis{




    //连接类型 1普通连接 2长连接
    private $_CTYPE = 1;


    //实例名
    public $_REDIS = null;


    //事物对象
    private $_TRANSCATION = null;


    //初始化
    public function __construct($db=0)
    {
        if (!isset($this->_REDIS)) {
            $this->_REDIS = new \Redis; 
$this->_REDIS->connect('127.0.0.1', 6379); 
$this->_REDIS->auth('');
$this->_REDIS->select($db);
        }
    }


    /**
     * 查看redis连接是否断开
     * @return $return bool true:连接未断开 false:连接已断开
     */
    public function ping()
    {
        $return = null;


        $return = $this->_REDIS->ping();


        return 'PONG' ? true : false;
    }


    /**
     * 设置redis模式参数
     * @param $option array 参数数组键值对
     * @return $return true/false
     */
    public function setOption($option=array())
    {
        return $this->_REDIS->setOption();
    }


    /**
     * 获取redis模式参数
     * @param $option array 要获取的参数数组
     */
    public function getOption($option=array())
    {
        return $this->_REDIS->getOption();
    }


    /**
     * 写入key-value
     * @param $key string 要存储的key名
     * @param $value mixed 要存储的值
     * @param $type int 写入方式 0:不添加到现有值后面 1:添加到现有值的后面 默认0
     * @param $repeat int 0:不判断重复 1:判断重复
     * @param $time float 过期时间(S)
     * @param $old int 1:返回旧的value 默认0
     * @return $return bool true:成功 flase:失败
     */
    public function set($key,$value,$type=0,$repeat=0,$time=0,$old=0)
    {
        $return = null;


        if ($type) {
            $return = $this->_REDIS->append($key, $value);
        } else {
            if ($old) {
                $return = $this->_REDIS->getSet($key, $value);
            } else {
                if ($repeat) {
                    $return = $this->_REDIS->setnx($key, $value);
                } else {
                    if ($time && is_numeric($time)) $return = $this->_REDIS->setex($key, $time, $value);
                    else $return = $this->_REDIS->set($key, $value);
                }
            }
        }


        return $return;
    }


    /**
     * 获取某个key值 如果指定了start end 则返回key值的start跟end之间的字符
     * @param $key string/array 要获取的key或者key数组
     * @param $start int 字符串开始index
     * @param $end int 字符串结束index
     * @return $return mixed 如果key存在则返回key值 如果不存在返回false
     */
    public function get($key=null,$start=null,$end=null)
    {
        $return = null;


        if (is_array($key) && !empty($key)) {
            $return = $this->_REDIS->getMultiple($key);
        } else {
            if (isset($start) && isset($end)) $return = $this->_REDIS->getRange($key);
            else $return = $this->_REDIS->get($key);
        }


        return $return;
    }


    /**
     * 删除某个key值
     * @param $key array key数组
     * @return $return longint 删除成功的key的个数
     */
    public function delete($key=array())
    {
        $return = null;


        $return = $this->_REDIS->delete($key);


        return $return;
    }


    /**
     * 判断某个key是否存在
     * @param $key string 要查询的key名
     */
    public function exists($key)
    {
        $return = null;


        $return = $this->_REDIS->exists($key);


        return $return;
    }


    /**
     * key值自增或者自减
     * @param $key string key名
     * @param $type int 0:自减 1:自增 默认为1
     * @param $n int 自增步长 默认为1
     */
    public function deinc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

php_lidong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值