封装redis操作 php版本
<?php namespace App\Service; use App\Service\BaseService;
use Illuminate\Support\Facades\Redis; /*
* redis操作相关方法
*/ class RedisService extends BaseService { /**
*
* @param string $key reids的get的key
* @param string $class 待执行的方法的class App\Service\AdminService
* @param string $function 待执行的方法的function getSupplyNum
* @param array $parameters 待执行的方法的parameters 参数 ...$parameters [] 数组自动绑定
* @param int $survivalTime redis存储时间
* $returnData 调用方法是否必须有数据
* @return type
* @throws \Exception
*/
public static function get(string $key, string $class = '', string $function = '', array $parameters = [], int $survivalTime = 3600, $returnData = true) {
if (empty($key)) {
throw new \Exception('key不能为空');
} $keyValue = Redis::get($key);
if (empty($keyValue)) { if (!method_exists($class, $function)) {
throw new \Exception($class . '此类的方法' . $function . '不存在');
}
/*
* 参数自动绑定,参数位置不能错
* $value 最好返回是数组,
*/
$value = $class::$function(...$parameters);
// p($value);
if (empty($value) && $returnData) {
throw new \Exception($class . '此类的方法' . $function . '返回数据为空,请检查调用方法');
}
Redis::set($key, json_encode($value));
Redis::expire($key, $survivalTime);
return json_encode($value);
}
return $keyValue;
} }
demo AdminService
//近一个月数据
public static function getMonthSaleOrderData($shop_id) { //近一个月订单总金额
$saleOrderKey = 'MonthSaleOrder' . $shop_id; $data = RedisService::get($saleOrderKey, 'App\Service\AdminService', 'MonthSaleOrderData', [$shop_id]); return json_decode($data, true);
} public static function MonthSaleOrderData($shop_id) {
$time = get_lately_month_start_to_end(); $query = SaleOrder::where('shop_id', $shop_id)->where('is_delete', 10)
->where('create_time', '>=', $time['start_time'])
->where('create_time', '<=', $time['end_time']);
$saleOrderAmount = $query->sum('order_amount');
$saleOrderNumber = $query->sum('order_number');
$saleOrderCount = $query->count();
$data = [
'saleOrderAmount' => $saleOrderAmount,
'saleOrderNumber' => $saleOrderNumber,
'saleOrderCount' => $saleOrderCount,
];
return $data;
}
封装redis操作 php版本的更多相关文章
- openresty开发系列27--openresty中封装redis操作
openresty开发系列27--openresty中封装redis操作 在关于web+lua+openresty开发中,项目中会大量操作redis, 重复创建连接-->数据操作-->关闭 ...
- java封装 redis 操作 对象,list集合 ,json串
/** * 功能说明: * 功能作者: * 创建日期: * 版权归属:每特教育|蚂蚁课堂所有 www.itmayiedu.com */package com.redis.service; import ...
- redis操作封装整理
<?php /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防止缓存穿透 * */ cla ...
- Redis操作Set工具类封装,Java Redis Set命令封装
Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...
- Redis操作List工具类封装,Java Redis List命令封装
Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...
- Redis操作Hash工具类封装,Redis工具类封装
Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...
- Redis操作字符串工具类封装,Redis工具类封装
Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...
- python 全栈开发,Day101(redis操作,购物车,DRF解析器)
昨日内容回顾 1. django请求生命周期? - 当用户在浏览器中输入url时,浏览器会生成请求头和请求体发给服务端 请求头和请求体中会包含浏览器的动作(action),这个动作通常为get或者po ...
- 使用poco再次封装redis
为方便程序对redis操作,我对poco的redis进行了再次封装,主要是针对自己应用需要的部分. 开发工具:netbean 系统环境:centos7 poco版本: poco-1.9.0-all 其 ...
随机推荐
- Python库的使用之-Sys模块
Python标准库之sys模块使用详解,讲解了使用sys模块获得脚本的参数.处理模块. 使用sys模块操作 模块搜索路径.使用sys模块查找内建模块.使用sys模块查找已导入的模块,重定向输出以及重定 ...
- Easyui combobox 源码修改模糊查询v=1.34
原来的匹配方式: $.fn.combobox.defaults=$.extend({},$.fn.combo.defaults,{valueField:"value",textFi ...
- hive中时间操作(二)
转:https://blog.csdn.net/qq646748739/article/details/77997276 --Hive中日期函数总结:--1.时间戳函数--日期转时间戳:从1970-0 ...
- Virtual DOM--react
Consider a DOM made of thousands of divs. Remember, we are modern web developers, our app is very SP ...
- BZOJ 4903: [Ctsc2017]吉夫特 数论+dp
思路很巧妙的一道题 ~ 这个应该不完全是正解,复杂度约为 $O(3\times 10^8)$,有时间再研究研究正解. 首先,最裸的暴力是按照权值从小到大枚举每一个数,然后枚举后面的数来更新方案数,是 ...
- Numpy | 09 高级索引
NumPy 比一般的 Python 序列提供更多的索引方式.除了之前看到的用整数和切片的索引外,数组可以由整数数组索引.布尔索引及花式索引. 整数数组索引 实例1:获取数组中(0,0),(1,1)和( ...
- WinDbg常用命令系列---!findstack
简介 !findstack扩展查找所有包含指定的符号或模块的堆栈.此命令搜索线程调用堆栈中的特定符号,并显示匹配的线程. 使用形式 !findstack Symbol[DisplayLevel] !f ...
- minio gataway 模式快速提供s3 兼容的文件服务
实际很多场景我们已经有了遗留系统的文件存储方式(ftp,或者共享目录),但是这个方式可能不是很好,对于web 不是很友好 实际上minio 也提供了gateway 的模式,可以方便快速的将遗留系统的存 ...
- HNOI做题记录
算是--咕完了? 2013.2014的就咕了吧,年代太久远了,并且要做的题还有那么多-- LOJ #2112. 「HNOI2015」亚瑟王 发现打出的概率只和被经过几次有关. 于是\(dp_{i,j} ...
- 内置函数— — eval、exec、compile
字符串类型代码:eval.exec.compile eval() 执⾏字符串类型的代码,并返回最终结果 print(eval("2+2")) # 4 n=8 def func() ...