一个简单清晰的Redis操作类-php
<?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操作类-php的更多相关文章
- 一个简单清晰的Redis操作类
<?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 ...
随机推荐
- linux 开机启动nginx
这里使用的是编写shell脚本的方式来处理 vi /etc/init.d/nginx (输入下面的代码) #!/bin/bash# nginx Startup script for the Ngin ...
- FTP新建文件夹访问
今天在远程服务器上添加了文件夹,本来还想着FTP打开看看,结果竟然发现没有这个文件夹访问 想了一下,感觉应该是FTP访问的文件设置,只有FTP设置了的文件夹才能有显示
- 设计模式之单例模式(JAVA实现)
单例模式之自我介绍 我,单例模式(Singleton Pattern)是一个比较简单的模式,我的定义如下: Ensure a class has only one instance,and provi ...
- nexus 增加代理仓库 无法搜到snapshot的jar包 解决方法
如题, nexus 私服 增加了另一个 私服, 但是无法搜到 版本中带有 snapshot字样的 jar包. 环境情况: 1.老私服: 首先版本中带有 snapshot字样的 jar包,是发布在 老 ...
- PL/SQL developer 可以连接本地数据库,但是不可以连接远程数据库的解决方法
修改Oracle_home目录下的 network\ADMIN\tnsnames.ora 文件, 在其中增加远程数据库对应的记录,类似下边这样: .2_orcl = (DESCRIPTION = (A ...
- JQuery 用法总结
1.隐藏与显示,用于div等等 //隐藏id=myTab下的所有li标签 $("#myTab li").hide(); //动画隐藏,1秒内隐藏该对象 $(); //显示class ...
- supervisor 配置篇
1,配置管理进程 进程管理配置参数,不建议全都写在supervisord.conf文件中,应该每个进程写一个配置文件放在include指定的目录下包含进supervisord.conf文件中. 1&g ...
- jenkins之升级
首先查看系统war包放置的位置 rpm -ql jenkins 下载一个war包 下载地址 https://mirrors.tuna.tsinghua.edu.cn/jenkins/war/2.61/ ...
- IIS中User-mode caching引起的Cache-Control不为public的问题
在IIS的Output caching中如果启用了User-mode caching将引起Cache-Control为no-cache,从而造成页面不能被浏览器或代理服务器缓存. web.config ...
- 一个不需要Log4Net的写日志的简单方法
有些项目写日志时会选择大名鼎鼎的Log4Net.而在我们使用它时,总会出现一些诸如版本不匹配而造成的写日志失败的情况,还要改web.config,还要改AssemblyInfo.而且,它的失败,并不是 ...