class redisDB{
   
    private $redis; //redis对象
   
   
    /**
     * 初始化Redis
     * $config = array(
     *  'server' => '127.0.0.1' 服务器
     *  'port'   => '6379' 端口号
     * )
     * @param array $config
     */
    function __construct($config = array()){
        $this->redis = new Redis();
        $this->redis->connect(REDIS_SERVER,REDIS_PORT);
        return $this->redis;
    }
   
    /**
     * 设置值
     * @param string $key KEY名称
     * @param string|array $value 获取得到的数据
     * @param int $timeOut 时间
     */
    public function set($key, $value, $timeOut = ,$type='json') {
        if($type=='serialize')
        {
            $value = serialize($value);
        }
        else
        {
            $value = json_encode($value);
        }
           
        $retRes = $this->redis->set($key, $value);
        if ($timeOut > ) $this->redis->setTimeout($key, $timeOut);
        return $retRes;
    }
   
    /**
     * 通过KEY获取数据
     * @param string $key KEY名称
     */
    public function get($key,$type='json') {
        $result = $this->redis->get($key);
           
        if($type=='serialize')
        {
            return unserialize($result);
        }
        else
        {
            return json_decode($result);
        }
    }
   
    /**
     * 删除一条数据
     * @param string $key KEY名称
     */
    public function delete($key) {
        return $this->redis->delete($key);
    }
   
    /**
     * 清空数据
     */
    public function flushAll() {
        return $this->redis->flushAll();
    }
   
    /**
     * 数据入队列
     * @param string $key KEY名称
     * @param string|array $value 获取得到的数据
     * @param bool $right 是否从右边开始入
     */
    public function push($key, $value ,$right = true) {
        $value = json_encode($value);
        return $right ? $this->redis->rPush($key, $value) : $this->redis->lPush($key, $value);
    }
   
    /**
     * 数据出队列
     * @param string $key KEY名称
     * @param bool $left 是否从左边开始出数据
     */
    public function pop($key , $left = true) {
        $val = $left ? $this->redis->lPop($key) : $this->redis->rPop($key);
        return json_decode($val);
    }
   
    /**
     * 数据自增
     * @param string $key KEY名称
     */
    public function increment($key) {
        return $this->redis->incr($key);
    }
   
    /**
     * 数据自减
     * @param string $key KEY名称
     */
    public function decrement($key) {
        return $this->redis->decr($key);
    }
   
    /**
     * key是否存在,存在返回ture
     * @param string $key KEY名称
     */
    public function exists($key) {
        return $this->redis->exists($key);
    }
   
    /**
     * 返回redis对象
     * redis有非常多的操作方法,我们只封装了一部分
     * 拿着这个对象就可以直接调用redis自身方法
     */
    public function redis() {
        return $this->redis;
    }
}

使用方法

include 'redis.php';
$redis =  new redisDB();
$key = 'fields';
$value = '好脚本';   //value可以是字符串或者数组
$redis->set($key,$value);
//获取fields的值也很简单
$fvalue = $redis->get('fields');
print_r($fvalue);

最新文章

  1. 改变字典内的value
  2. Linux的watch命令 — 实时监测命令的运行结果
  3. JQuery 的几个有用的技巧
  4. mysql 字段操作
  5. HITAG 1/2/S
  6. IOS 7 Study - Manipulating a Navigation Controller’s Array of View
  7. ORA-01033: ORACLE initialization or shutdown in progress 实用的处理方法
  8. (2015年郑州轻工业学院ACM校赛题) J 堆
  9. Linux/UNIX数据文件和信息系统
  10. EasyUI DataGrid 添加 Footer
  11. # hadoop入门第六篇:Hive实例
  12. ioremap_nocache() 函数的使用【转】
  13. SharePoint中你不知道的图片库(实战)
  14. jquery 中 this 的范围
  15. 为什么我觉得Python烂的要死?
  16. redis-cluster无备节点,安装脚本
  17. 在Windows Server 2008 R2 Server中,上传视频遇到的问题(一)
  18. Oracle数据库三种备份方案
  19. 数据迁移_老集群RAC迁移数据恢复到新集群RAC
  20. 使用Google-Colab训练PyTorch神经网络

热门文章

  1. php导入导出
  2. ACdream 1157 Segments(CDQ分治)
  3. 使用streaming window函数统计用户不同时间段平均消费金额等指标
  4. C#语言基础2016/3/6
  5. 查看linux的出错信息
  6. 用javascript在客户端删除某一个cookie键值对
  7. MVC架构剖析--ASP.NET MVC图解(二)
  8. Android中Listview实现分页加载效果OnScrollListener
  9. jQuery上传插件,文件上传测试用例
  10. Android 常用工具类之 DimenUtil