封装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 其 ...
随机推荐
- 关于MySql的坑
今天弄hibernate的时候数据库死活连不上 估计mybatis也够呛 搞了好几个小时也不行,最后问题是mysql驱动版本过低,需要换高一些版本的驱动 另一个就是一个ssl方面的错误 连接的时候要把 ...
- Opencv 初探 常用API
一.介绍 OpenCV是计算机视觉领域应用最广泛的开源工具包,基于C/C++,支持Linux/Windows/MacOS/Android/iOS,并提供了Python,Matlab和Java等语言的接 ...
- UiAutomatorViewer无法获取手机截图进行元素定位的解决办法
问题描述 本来想使用UIAutomatorView定位app页面元素的,最开始我使用的是夜神模拟器,打开UIAutomatorView连接模拟器没有问题,但是后来我使用真机时发现无法连接到真机获取真机 ...
- Chrome插件安装和用法
XPath Helper 下载插件,拖入chrome://extensions/ 使用方法:ctrl+shift+x呼出 JSONView的使用: 安装JSONView插件 下载插件,拖入chrome ...
- [Algorithm] 387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- 【C/C++】指针
指针定义 指针是一个变量,其值为另一个变量的地址,即,内存位置的直接地址.就像其他变量或常量一样,必须在使用指针存储其他变量地址之前,对其进行声明.指针变量声明的一般形式为: type *var-na ...
- MVC框架与增强
一.什么是MVC MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示 ...
- 59: loj #10215
$des$ https://loj.ac/problem/10215 $sol$ exgcd检查 $code$ #include <iostream> #include <cstdl ...
- 在Linux下配置git并设置远程仓库
自己常在云服务器上进行代码的编写,为了更方便的保存工作和管理,便使用了git这个版本管理工具来管理.下面介绍整个服务的配置过程. git的下载安装: 使用以下命令,回车即可,中间过程会有一个按y回车的 ...
- 【luoguP1858】多人背包
链接 对于每个状态\(f[j]\)多记录一个维度,转移的时候利用类似于归并排序的方法合并,以保证时间复杂度可以承受 注意事项:前\(K\)大可以有重复的价值 #include<iostream& ...