一个简单清晰的Redis操作类
<?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操作类的更多相关文章
- 一个简单清晰的Redis操作类-php
<?php /** * redis处理的二次封装 * */ class Redis{ private $_redis; private $_config; public function __c ...
- php的redis 操作类,适用于单台或多台、多组redis服务器操作
redis 操作类,包括单台或多台.多组redis服务器操作,适用于业务复杂.高性能要求的 php web 应用. redis.php: <?php /* redis 操作类,适用于单台或多台. ...
- 设计模式之PHP项目应用——单例模式设计Memcache和Redis操作类
1 单例模式简单介绍 单例模式是一种经常使用的软件设计模式. 在它的核心结构中仅仅包括一个被称为单例类的特殊类. 通过单例模式能够保证系统中一个类仅仅有一个实例并且该实例易于外界訪问.从而方便对实例个 ...
- 2.NetDh框架之简单高效的日志操作类(附源码和示例代码)
前言 NetDh框架适用于C/S.B/S的服务端框架,可用于项目开发和学习.目前包含以下四个模块 1.数据库操作层封装Dapper,支持多种数据库类型.多库实例,简单强大: 此部分具体说明可参考博客: ...
- 实现一个简单的http请求工具类
OC自带的http请求用起来不直观,asihttprequest库又太大了,依赖也多,下面实现一个简单的http请求工具类 四个文件源码大致如下,还有优化空间 MYHttpRequest.h(类定义, ...
- Qt5.9一个简单的多线程实例(类QThread)(第一种方法)
Qt开启多线程,主要用到类QThread.有两种方法,第一种用一个类继承QThread,然后重新改写虚函数run().当要开启新线程时,只需要实例该类,然后调用函数start(),就可以开启一条多线程 ...
- 封装一个redis操作类来操作hash格式
最近项目要用redis,依然是基于tp3.2. 发现thinkphp3.2自带的缓存类并不好使用,就自己封装了一个 目前只支持hash格式,其他数据类型的操作后面用到的时候再补充 <?php / ...
- 用php实现一个简单的链式操作
最近在读<php核心技术与最佳实践>这本书,书中第一章提到用__call()方法可以实现一个简单的字符串链式操作,比如,下面这个过滤字符串然后再求长度的操作,一般要这么写: strlen( ...
- 简单的php数据库操作类代码(增,删,改,查)
这几天准备重新学习,梳理一下知识体系,同时按照功能模块划分做一些东西.所以.mysql的操作成为第一个要点.我写了一个简单的mysql操作类,实现数据的简单的增删改查功能. 数据库操纵基本流程为: 1 ...
随机推荐
- Intellij 设置生成serialVersionUID的方法
- Azure Powershell对ASM资源的基本操作
本文主要介绍Windows Azure Powershell对ASM资源的基本操作 1.登陆ASM模式,命令:Add-AzureAccount -Environment AzureChinaCloud ...
- python_如何使用生成器实现可迭代对象?
案例分析: 实一个可迭代对象的类,它能迭代出给定范围内所有的素数: pn = Number(1, 30) for k in pn: print(k) 结果为:2,3,5,7,11,13,17,19,2 ...
- CSS深入理解学习笔记之overflow
1.Overflow基本属性 overflow:visible(默认)/hidden/scroll/auto/inherit; visible:超出部分可见. hidden:超出部分隐藏. scrol ...
- Maven 常用配置
pom.xml基础配置: <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEn ...
- jumpserver v0.4.0 基于 CenOS7 的安装详解
标签(linux): jumpserver 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 笔者已经弃用Jumpserver,并自已开发了shell跳板机. sh ...
- 【转】GPS连续运行单参考站解决方案
GPS连续运行单参考站解决方案 一. 前言 随着国家信息化程度的提高及计算机网络和通信技术的飞速发展,电子政务.电子商务.数字城市.数字省区和数字地球的工程化和现实化,需要采集多种实时地理 空间 ...
- LINUX获取文件信息
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- Java - 二叉树递归与非递归
树的定义具有递归特性,因此用递归来遍历比较符合特性,但是用非递归方式就比较麻烦,主要是递归和栈的转换. import java.util.Stack; /** * @author 李文浩 * @ver ...
- Spring MVC的优势
Spring 框架提供了构建Web应用程序的全功能MVC模块--Spring MVC. Spring MVC框架提供了一个DispatcherServlet作为前端控制器来分派请求,同时提供灵活的配置 ...