<?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. C#各版本新增功能

    本系列文章主要整理并介绍 C# 各版本的新增功能. C# 8.0 C#8.0 于 2019年4月 随 .NET Framework 4.8 与 Visual Studio 2019 一同发布,但是当前 ...

  2. live555 基本框架

    (转) 从程序的结构来看,live项目包括了四个基本库.程序入口类(在mediaServer中)和一些测试代码(在testProgs中).四个基本库是UsageEnvironment,BasicUsa ...

  3. 滚动加载|页面滑到底部加载数据|jquery.endless-scroll插件|使用demo

    <html> <head> <link rel="dns-prefetch" href="http://i.tq121.com.cn&quo ...

  4. Goland在go mod vendor模式下无法识别某些库

    症状:go build可以正常编译,但代码编辑器里面提示找不到相关lib,后来发现是因为go.mod中没有用require这个库,补上库地址和版本.因为项目的mod vendor模式,版本一般不需要写 ...

  5. SpringBoot Ajax跨域问题(session共享问题)

    ajax 发送post请求至springBoot出现跨域问题 需要在springBoot加上注解  @CrossOrigin 就能解决 报错信息 前端js代码 如果用上面的方式,session不能共享 ...

  6. c+多态的本质:编译器维护了类型信息同时插入了解释执行机制

    Calling a virtual function is slower than calling a non-virtual function for a couple of reasons: Fi ...

  7. SuperSocket

    1.目前稳定版是 v1.6: 2.轻量级.可扩展.Socket应用程序框架: 3.你可以用来开发Socket服务端应用,不用关心如何使用Socket.如何维护Socket连接和Socket如何工作. ...

  8. [Git] Remove Files from Staging Before Committing

    If you've added files to the staging area (the Index) accidentally - you can remove them using git r ...

  9. JavaScript基础10——正则

    什么是正则?     正则表达式(regular expression)是一个描述字符规则的对象.可以用来检查一个字符串是否含有某个字符,将匹配的字符做替换或者从某个字符串中取出某个条件的子串等.   ...

  10. MongoDB 几种查询嵌套数据(Embedded)的方式(转载)

    前言 MongoDB 推荐使用「内嵌文档(Embedded)」,所以带来一个问题,如何查询嵌入文档内的数据? 假如我们有一个 storage 的 Collection,包含一条数据: // `stor ...