<?php
/**
* redis处理的二次封装
*
*/
class Redis{ private $_redis; private $_config; public function __construct() {
$this->_config = Yaf_Application::app()->getConfig()->get("Redis"); if(empty($this->_config)){
throw new Exception("email config can not be empty!");
}
if ($this->_config['servers']['host'] == '') {
$this->_config['servers']['host'] = '127.0.0.1';
}
if ($this->_config['servers']['port'] == ''){
$this->_config['servers']['port'] = '';
}
$this->_redis = new redis();
$this->_redis->connect($this->_config['servers']['host'], $this->_config['servers']['port']);
//$this->_redis->pconnect($this->_config['servers']['host'], $this->_config['servers']['port']);
$this->_redis->auth($this->_config['servers']['password']);
} /**
* 设置值
* @param string $key KEY名称
* @param string|array $value 获取得到的数据
* @param int $timeOut 时间
*/
public function set($key, $value, $timeOut = ) {
$value = json_encode($value, TRUE);
$retRes = $this->_redis->set($key, $value);
if ($timeOut > ) $this->_redis->setTimeout($key, $timeOut);
return $retRes;
} /**
* 设置db
* @param int $deIndex db值
*/
public function select($deIndex) {
$deIndex = (int)$deIndex;
$retRes = $this->_redis->select($deIndex);
return $retRes;
} /**
* 通过KEY获取数据
* @param string $key KEY名称
*/
public function get($key) {
$result = $this->_redis->get($key);
return json_decode($result, TRUE);
} /**
* 删除一条数据
* @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);
} /**
* setTranction
* 执行事务添加值
* @param string $key
* @param int $count
* @access public
* @return boolean
*/
public function setTranction($key, $count){
$this->_redis->watch($key);
return $this->_redis->multi()->set($key, $count)->exec();
} /**
* getTranction
* 执行事务获取
* @param string $key
* @access public
* @return boolean
*/
public function getTranction($key){
$this->_redis->watch($key);
return $this->_redis->multi()->get($key)->exec();
} /**
* 指定步长增加
* @param string $key
* @param int $count
* @return int
*/
public function incrBy($key, $count) {
return $this->_redis->incrBy($key, $count);
} /**
* 指定步长减少
* @param string $key
* @param int $count
* @return int
*/
public function decrBy($key, $count) {
return $this->_redis->decrBy($key, $count);
} /**
* decrByTranction
* 执行事务减去某个值
* @param string $key
* @param int $count
* @access public
* @return array
*/
public function decrByTranction($key, $count){
$this->_redis->watch($key);
return $this->_redis->multi()->decrBy($key, $count)->exec();
} /**
* incrByTranction
* 执行事务,增加某个值
* @param string $key
* @param int $count
* @access public
* @return array
*/
public function incrByTranction($key, $count){
$this->_redis->watch($key);
return $this->_redis->multi()->incrBy($key, $count)->exec();
} /**
* incrByFloat
* 执行事务,增加某个值,float型运算
* @param string $key
* @param int $count
* @access public
* @return array
*/
public function incrByFloat($key, $count){
$this->_redis->watch($key);
return $this->_redis->multi()->incrByFloat($key, $count)->exec();
} /**
* key是否存在,存在返回ture
* @param string $key KEY名称
*/
public function exists($key) {
return $this->_redis->exists($key);
} /**
* setnx
* 当没有值时设置一个值
* @param string $key
* @param mixed $value
*
*/
public function setnx($key, $value){
return $this->_redis->setnx($key, $value);
} /**
* 返回redis对象
* redis有非常多的操作方法,我们只封装了一部分
* 拿着这个对象就可以直接调用redis自身方法
*/
public function redis() {
return $this->_redis;
} }

Module层中的使用

  //*实物商品*-----根据商品ID查询商品信息,指定硬条件(是否上架、是否展示、是否删除)-------使用优先
public static function getProductInfoById($product_id = )
{
$data = [];
if ( !is_positive_integer( $product_id )) {
return $data;
}
$Cache = new Cache;
$Cache_key = sprintf(self::$Product_Real_Info_Cache_Key, $product_id);
$data = unserialize($Cache->get_obj_cache( $Cache_key));
if ($data === false) {
$productInfo = self::alias('p')
->field('p.id,p.product_sn,p.product_name,p.product_money,p.product_price,p.score,p.product_stock,
p.product_image,p.product_param,p.product_desc,p.product_main,p.category_id,p.merchant_id')
->where(['p.id' => $product_id, 'p.is_delete' => ,'is_virtual' => ,'is_shelves' => ])
->find();
$data = $productInfo ? $productInfo->toArray() : [];
if (is_not_empty_array($data)) {
$Cache->cache_item($Cache_key, serialize($data), self::$Cache_time);
}
}
// echo memory_get_usage();
return $data;
}

一个简单清晰的Redis操作类的更多相关文章

  1. 一个简单清晰的Redis操作类-php

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

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

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

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

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

  4. 2.NetDh框架之简单高效的日志操作类(附源码和示例代码)

    前言 NetDh框架适用于C/S.B/S的服务端框架,可用于项目开发和学习.目前包含以下四个模块 1.数据库操作层封装Dapper,支持多种数据库类型.多库实例,简单强大: 此部分具体说明可参考博客: ...

  5. 实现一个简单的http请求工具类

    OC自带的http请求用起来不直观,asihttprequest库又太大了,依赖也多,下面实现一个简单的http请求工具类 四个文件源码大致如下,还有优化空间 MYHttpRequest.h(类定义, ...

  6. Qt5.9一个简单的多线程实例(类QThread)(第一种方法)

    Qt开启多线程,主要用到类QThread.有两种方法,第一种用一个类继承QThread,然后重新改写虚函数run().当要开启新线程时,只需要实例该类,然后调用函数start(),就可以开启一条多线程 ...

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

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

  8. 用php实现一个简单的链式操作

    最近在读<php核心技术与最佳实践>这本书,书中第一章提到用__call()方法可以实现一个简单的字符串链式操作,比如,下面这个过滤字符串然后再求长度的操作,一般要这么写: strlen( ...

  9. 简单的php数据库操作类代码(增,删,改,查)

    这几天准备重新学习,梳理一下知识体系,同时按照功能模块划分做一些东西.所以.mysql的操作成为第一个要点.我写了一个简单的mysql操作类,实现数据的简单的增删改查功能. 数据库操纵基本流程为: 1 ...

随机推荐

  1. 频繁更换ip会影响SEO优化吗?

    网站更换ip会不影响SEO的效果,其实网站更换ip是正常的(但不能频繁更换),搜索引擎抓取是根据网站的域名进行的,不是根据ip来抓取你的网站.在短时间内更换IP对SEO的效果并没有很大的影响. 如果是 ...

  2. JavaScript数组的22种方法

    原文:http://www.cnblogs.com/xiaohuochai/p/5682621.html javascript中数组的22种方法   前面的话 数组总共有22种方法,本文将其分为对象继 ...

  3. python 爬取B站视频弹幕信息

    获取B站视频弹幕,相对来说很简单,需要用到的知识点有requests.re两个库.requests用来获得网页信息,re正则匹配获取你需要的信息,当然还有其他的方法,例如Xpath.进入你所观看的视频 ...

  4. LCA(最近公共祖先)之倍增算法

    概述 对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. 如图,3和5的最近公共祖先是1,5和2的最近公共祖先是4 在本篇中我们先介 ...

  5. nginx加权轮询和ip_hash

    nginx为后端web服务器(apache,nginx,tomcat,weblogic)等做反向代理 几台后端web服务器需要考虑文件共享,数据库共享,session共享问题.文件共享可以使用nfs, ...

  6. SpringMVC解决跨域问题

    有个朋友在写扇贝插件的时候遇到了跨域问题. 于是我对解决跨域问题的方式进行了一番探讨. 问题 API:查询单词 URL: https://api.shanbay.com/bdc/search/?wor ...

  7. 使用H2数据库进行单元测试

    背景 H2 数据库是一个开源的嵌入型内存数据库,采用纯Java语言实现: 程序非常小巧轻便,整个完整的Jar包也只有1.5M左右,很容易集成到项目中. 官网地址 http://www.h2databa ...

  8. Jmeter_打印当前时间戳&打印偏移时间戳

    Jmeter中提供了一种函数,可以打印时间戳,如下图 年: yyyy 月:MM 日:dd 时: HH 分: mm 秒:ss 关于时间戳的格式,可以自由组合定义,这里我写成这样 yyyy-MM-dd H ...

  9. Redis服务启动失败,提示:redis-server:command not found

    今天我开始做主从复制的集群模式的测试,所以需要再装一个Linux操作系统,我在虚拟机里已经安装了一个Linux操作系统,Redis也已经配置好了.今天打算再安装一个Linux操作系统,Linux系统的 ...

  10. asp.net 文件上传 Uploadify HTML5 带进度条

    参考的https://www.cnblogs.com/lvdabao/p/3452858.html这位,在此基础上略有修改: 1.根据Layer,将上传附件做成弹窗显示,引入frame弹窗,在项目当中 ...