<?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. 原生 JS 实现一个瀑布流插件

    更好的阅读体验,点击 原文地址 瀑布流布局中的图片有一个核心特点 -- 等宽不定等高,瀑布流布局在国内网网站都有一定规模的使用,比如pinterest.花瓣网等等.那么接下来就基于这个特点开始瀑布流探 ...

  2. python_如何统计序列中元素

    问题1: 随机数列[12,5,8,7,8,9,4,8,5,...] 中出现次数最高的3个元素,他们出现的次数 问题2: 对某英文文章的单词,进行词频统计,找出出现次数最搞得10个单词,他们出现的次数是 ...

  3. Linux make nginx 的时候报错

    报错如下: `conf/koi-win' and `/usr/local/nginx/conf/koi-win' are the same file   原因: 可能在编译 nginx 的时候步骤不对 ...

  4. hibernate 常用主键生成策略与配置

    <id name="id" column="id"> <generator class="assigned" /> ...

  5. <%@include file="a.jsp"%> jsp引用jsp文件时候注意

    被包含的a.jsp文件:    a.jsp文件的page指令要么不要写.要么与主jsp文件的page指令一样.

  6. 解决IE中placeholder的兼容问题

    定义和用法 placeholder 属性提供可描述输入字段预期值的提示信息(hint). 该提示会在输入字段为空时显示,并会在字段获得焦点时消失. 注释:placeholder 属性适用于以下的 &l ...

  7. IOS 疯狂基础之 页面间跳转

    常用的就两种 一种通过导航,一种直接跳 第一种 直接跳转 思路大致就是new一个目的页面,然后设置下页面跳转动画 中间还可以做点目的页面的数据初始化: ValueInputView *valueVie ...

  8. PHPUnit使用教程——PHP环境变量+x-debug+composer+phpunit配置安装(超详细!)

    注意:Windows系统 一.提前入坑点:要求php5.6,7.0,7.1,不论使用集成版还是非集成版的小伙伴都要好好查看自己的php版本,个人的版本居然是5.5.X的,哭唧唧.不过别担心,爸爸教你升 ...

  9. NodeJs实现他人项目实例

    1.简单实例,参考 https://github.com/alsotang/node-lessons/tree/master/lesson2 2.express一个新项目 ,但出现警告 发现少了nod ...

  10. 《.NET 设计规范》第 7 章:异常

    第 7 章:异常 异常与各种面向对象语言集成得非常好. 异常增强了 API 的一致性. 在用返回值来报告错误时,错误处理的代码与可能会发生错误的代码距离总是很近. 更容易使错误处理的带码全局化. 错误 ...