<?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. httprunner学习11-辅助函数debugtalk.py

    前言 在httprunner里面,每个 YAML / JSON 文件的脚本都是独立运行的,有时候我们希望能跨文件使用公用的参数. 比如登录生成一个token,后面的用例都可以去引用这个token值,或 ...

  2. Redux的图文模型

    Also these are really nice (from http://slides.com/jenyaterpil/redux-from-twitter-hype-to-production ...

  3. 伯克利套接字(BSD Socket)

    http://blog.csdn.net/blueman2012/article/details/6693605#socket.28.29 伯克利套接字(Berkeley sockets),也称为BS ...

  4. foreach中的collection

    foreach中collection的三种用法 https://www.cnblogs.com/xiemingjun/p/9800999.html foreach的主要用在构建in条件中,它可以在SQ ...

  5. 爬虫 -- UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe6 in position 301: unexpected end of data

     errors参数有3个值:strict,  ignore,  replace html.decode("utf-8"),这种形式有时会报错,那么修改为下面形式,将decode函数 ...

  6. centos7 安装 bugfree3

    . 安装apache yum install httpd . 安装mysql wget -i -c http://dev.mysql.com/get/mysql57-community-release ...

  7. vant checkBox 批量删除

    有两种实现方式,当然不止两种 一:使用 filter 将我们需要的过滤出来,也就是哪个没有选中就过滤哪个 二:使用splice数组方法,将我们选择需要删除的 index 放到一个数组里面,然后进行删除 ...

  8. mod_spatialite.so.7

  9. CSRF的防御

    声明 本文转自:跨站请求伪造漏洞

  10. 理解Spring定时任务@Scheduled的两个属性fixedRate和fixedDelay

    fixedRate和fixedDelay都是表示任务执行的间隔时间 fixedRate和fixedDelay的区别:fixedDelay非常好理解,它的间隔时间是根据上次的任务结束的时候开始计时的.比 ...