<?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. 汇编编译器(masm.exe)对jmp的相关处理

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  2. mybatis-自定义typeHandler

    场景一:有个java.util.Date在存入数据库的时候自动转换为timestamp时间戳,从数据库取值的时候把时间戳自动转换为java.util.Date 表结构 CREATE TABLE `us ...

  3. 完美解决打开github速度慢的问题

    摘抄自知乎. 修改hosts(HOSTS文件路径:C:\Windows\System32\drivers\etc\hosts) 1.打开Dns检测|Dns查询 - 站长工具 2.在检测输入栏中输入ht ...

  4. shell第二篇

    第二篇知道shell是什么,再来了解一下shell的分类及相关历史 参考百度百科:shell 1.shell概念(计算机壳层) 在计算机科学中,Shell俗称壳(用来区别于核),是指"提供使 ...

  5. Eralng的常用数据结构

    1.记录(record) 适用于小数据,并且用属性名方便查找 2.Key/Value 类型 a.属性列表 就是类似[{Key, Value}]的列表,可以通过proplists模块来处理这样的列表 当 ...

  6. python监控微信报警

    微信接口调用代码: #coding=utf8 import itchat from flask import Flask, request itchat.auto_login(enableCmdQR= ...

  7. 我的java之路week2类的无参、带参方法

    2.1语法 public 返回值类型 方法名(){ //方法体 } 2.2方法的调用语法 对象名.方法名 计算平均分和总成绩 public class Score { /** * 创建类 ScoreC ...

  8. 在线生成PDF的网站-HTML 转 PDF 在线

    http://pdf.df5d.com/   (服务器问题,演示暂停了,但是 下面介绍的组件还是可以使用的) 将前面用到的wkhtmltopdf用一个服务器程序集成在一起,接受一个URL参数,在生成一 ...

  9. 钉钉自定义机器人配合SVN钩子事件进行消息的推送实践

    目前很多公司还是使用SVN(TortoiseSVN)进行版本控制,使用简单,适合管理一般项目.协同办公软件目前钉钉比较成熟,阿里也一直在宣传推广,这两年公司也在使用,主要用于信息的沟通,其它的绩效.考 ...

  10. 6.Ray-消息订阅器编写

    消息订阅器: Ray是基于Event Sourcing设计的ES/Actor框架,消息发布后需要订阅处理,订阅器主要有以下两类: CoreHandler消息订阅器=RabbitSub+SubHandl ...