<?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版本的更多相关文章

  1. openresty开发系列27--openresty中封装redis操作

    openresty开发系列27--openresty中封装redis操作 在关于web+lua+openresty开发中,项目中会大量操作redis, 重复创建连接-->数据操作-->关闭 ...

  2. java封装 redis 操作 对象,list集合 ,json串

    /** * 功能说明: * 功能作者: * 创建日期: * 版权归属:每特教育|蚂蚁课堂所有 www.itmayiedu.com */package com.redis.service; import ...

  3. redis操作封装整理

    <?php /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防止缓存穿透 * */ cla ...

  4. Redis操作Set工具类封装,Java Redis Set命令封装

    Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...

  5. Redis操作List工具类封装,Java Redis List命令封装

    Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...

  6. Redis操作Hash工具类封装,Redis工具类封装

    Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...

  7. Redis操作字符串工具类封装,Redis工具类封装

    Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...

  8. python 全栈开发,Day101(redis操作,购物车,DRF解析器)

    昨日内容回顾 1. django请求生命周期? - 当用户在浏览器中输入url时,浏览器会生成请求头和请求体发给服务端 请求头和请求体中会包含浏览器的动作(action),这个动作通常为get或者po ...

  9. 使用poco再次封装redis

    为方便程序对redis操作,我对poco的redis进行了再次封装,主要是针对自己应用需要的部分. 开发工具:netbean 系统环境:centos7 poco版本: poco-1.9.0-all 其 ...

随机推荐

  1. sql的date和string转换

    涉及的函数 date_format(date, format) 函数,MySQL日期格式化函数date_format() unix_timestamp() 函数 str_to_date(str, fo ...

  2. javascript学习4、Function函数、伪数组arguments

    一.Function函数基础 函数:就是将一些语句进行封装,然后通过调用的形式,执行这些语句. 1.函数的作用: 将大量重复的语句写在函数里,以后需要这些语句的时候,可以直接调用函数,避免重复劳动. ...

  3. 男上加男 BETA冲刺博客汇总

    项目BETA冲刺(团队) --总结 1.团队信息 团队名 :男上加男 成员信息 : 队员学号 队员姓名 个人博客地址 备注 221600427 Alicesft https://www.cnblogs ...

  4. spring Boot + MyBatis + Maven 项目,日志开启打印 sql

    在 spring Boot + MyBatis + Maven 项目中,日志开启打印 sql 的最简单方法,就是在文件 application.properties 中新增: logging.leve ...

  5. 关于Python文件读写

    Python中文件操作可以通过open函数,这的确很像C语言中的fopen.通过open函数获取一个file object,然后调用read(),write()等方法对文件进行读写操作. 1.open ...

  6. NameNode和SecondaryNameNode

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/weixin_37838429/artic ...

  7. Word Squares

    Description Given a set of words without duplicates, find all word squares you can build from them. ...

  8. 同余方程组(EXCRT)(luogu4777)

    #include<cstdio> #include<algorithm> #define ll long long using namespace std; ll k; ll ...

  9. KVM系统镜像制作

    使用virt-install创建虚拟机并安装GuestOS virt-install是一个命令行工具,它能够为KVM.Xen或其它支持libvirt API的hypervisor创建虚拟机并完成Gue ...

  10. JavaScript 箭头函数

    ES6新标准增加了一种新的函数,箭头函数. x=>x*x 相当于: function (x){ return x*x; } 如果参数不是一个,就需要用括号()括起来: // 两个参数:var t ...