Redis 缓存保存某段时间累加的数值,加入最大id防止同一秒并发过大,导致只统计了执行时同一秒的部分数据,而同一秒另一部分数据在下次累加时没有统计到缓存中

   //coin总数
public function get_stat_coin_total($key = '',$now = 0){
$redisTotal = 0;
$redisMaxId = 0;
$redisData = null;
$effective_time = Kv::getStatEffectiveTime(); //有效时间
if($key == 'gift_log_coin_today'){
$time1 = strtotime(date('Y-m-d',strtotime('0 day')));
$time2 = $now; //统计到当前时间的数据
}elseif($key == 'gift_log_coin_yesterday'){
$time1 = strtotime(date('Y-m-d',strtotime('-1 day')));
$time2 = strtotime(date('Y-m-d',strtotime('0 day')));
}elseif($key == 'gift_log_coin_yesterday_before'){
$time1 = 0; //最开始时间
$time2 = strtotime(date('Y-m-d',strtotime('0 day')));
}elseif($key == 'gift_log_coin_total'){
$time1 = 0; //最开始时间
$time2 = $now; //统计到当前时间的数据
}else{
return 0;
}
$redis = self::getRedis();
$userStatListKey = UserStat::USER_STAT_LIST_KEY . $key;
$redisData = $redis->hGetAll($userStatListKey);
if($redisData && $redisData['effective_time'] > $now){
$time1 = $redisData['end_time'];
$redisTotal = $redisData['total'];
$redisMaxId = $redisData['max_id'];
$effective_time = $redisData['effective_time'];
} $sql = "SELECT sum(send_coin) as total,max(id) as max_id FROM `gift_log` WHERE add_time >= {$time1} AND add_time <= {$time2} AND id > {$redisMaxId}; ";
$connection = $this->getReadConnection();
$data = $connection->fetchOne($sql);
$total = empty($data['total'])?0:$data['total'];
$max_id = empty($data['max_id'])?$redisMaxId:$data['max_id'];
$result = [
'start_time'=>$time1,
'end_time'=>$time2,
'total'=>$total+$redisTotal,
'max_id'=>$max_id,
'effective_time'=>$effective_time,
];
$redis->hMset($userStatListKey,$result); return $result['total']; }

一条语句判断数据是否异常例子(保存统计过的最大自增id)

/**
* 检测 今日 coin 是否正常 统一查询
* 保存统计过的最大自增id
*/
public function checkCoin() {
$model = new UserStat();
$connection = $model->getReadConnection();
$redis = self::getRedis();
$userStatListKey = UserStat::USER_STAT_LIST_KEY;
$redisData = $redis->hGetAll($userStatListKey);
if ($redisData) {
$sql = <<<SQL
SELECT (SELECT concat(IFNULL(sum(coin),0),'_',ifnull(max(id),:max_recharge_id)) FROM wallet_recharge_log WHERE pay_status = 1 AND id > :max_recharge_id ) AS recharge
,(SELECT concat(IFNULL(sum(-send_coin),0),'_',ifnull(max(id),:max_send_gift_id)) AS send_total FROM gift_log WHERE id > :max_send_gift_id) AS send_gift
,(SELECT concat(IFNULL(sum(CASE type WHEN 1 THEN coin ELSE -coin END),0),'_',ifnull(max(id),:max_wallet_order_id)) FROM wallet_order_log WHERE id > :max_wallet_order_id ) AS wallet_order
,(SELECT IFNULL(sum(-coin),0) AS has_total_coin FROM user_wallet) AS has_coin
,(SELECT IFNULL(sum(-pay_coin),0) AS total FROM live_pay_log WHERE `status` =2) AS live_pay
,(SELECT concat(IFNULL(sum(-coin),0),'_',ifnull(max(id),:max_barrage_pay_id)) FROM user_barrage_log WHERE id > :max_barrage_pay_id) AS barrage_pay
,(SELECT concat(IFNULL(sum(-coin),0),'_',ifnull(max(id),:max_vip_pay_id)) FROM user_vip_log WHERE id > :max_vip_pay_id) AS vip_pay
,(SELECT concat(IFNULL(sum(coin),0),'_',ifnull(max(id),:max_exchange_get_id)) FROM wallet_exchange_log WHERE id > :max_exchange_get_id) AS exchange_get;
SQL;
$bind_arr = [
'max_recharge_id' => $redisData['max_recharge_id'],
'max_send_gift_id' => $redisData['max_send_gift_id'],
'max_wallet_order_id' => $redisData['max_wallet_order_id'],
'max_barrage_pay_id' => $redisData['max_barrage_pay_id'],
'max_vip_pay_id' => $redisData['max_vip_pay_id'],
'max_exchange_get_id' => $redisData['max_exchange_get_id'],
];
$result = $connection->query($sql, $bind_arr)->fetch(); $change_coin = $redisData['has_coin'] - $result['has_coin']; $result['recharge'] = explode('_',$result['recharge']);
$result['send_gift'] = explode('_',$result['send_gift']);
$result['wallet_order'] = explode('_',$result['wallet_order']);
$result['barrage_pay'] = explode('_',$result['barrage_pay']);
$result['vip_pay'] = explode('_',$result['vip_pay']);
$result['exchange_get'] = explode('_',$result['exchange_get']);
$redis_arr = [
'has_coin' => $result['has_coin'],
'live_pay' => $result['live_pay'],
'recharge' => $redisData['recharge'] + $result['recharge'][0],
'send_gift' => $redisData['send_gift'] + $result['send_gift'][0],
'wallet_order' => $redisData['wallet_order'] + $result['wallet_order'][0],
'barrage_pay' => $redisData['barrage_pay'] + $result['barrage_pay'][0],
'vip_pay' => $redisData['vip_pay'] + $result['vip_pay'][0],
'exchange_get' => $redisData['exchange_get'] + $result['exchange_get'][0],
];
$max_id_arr = [
'max_recharge_id' => $result['recharge'][1],
'max_send_gift_id' => $result['send_gift'][1],
'max_wallet_order_id' => $result['wallet_order'][1],
'max_barrage_pay_id' => $result['barrage_pay'][1],
'max_vip_pay_id' => $result['vip_pay'][1],
'max_exchange_get_id' => $result['exchange_get'][1],
];
echo "send_gift : [{$result['send_gift'][0]}];change_coin [$change_coin]']\n\n";
} else {
$sql = <<<SQL
SELECT (SELECT concat(IFNULL(sum(coin),0),'_',ifnull(max(id),0)) FROM wallet_recharge_log WHERE pay_status = 1 ) AS recharge
,(SELECT concat(IFNULL(sum(-send_coin),0),'_',ifnull(max(id),0)) AS send_total FROM gift_log) AS send_gift
,(SELECT concat(IFNULL(sum(CASE type WHEN 1 THEN coin ELSE -coin END),0),'_',ifnull(max(id),0)) FROM wallet_order_log ) AS wallet_order
,(SELECT IFNULL(sum(-coin),0) AS has_total_coin FROM user_wallet) AS has_coin
,(SELECT IFNULL(sum(-pay_coin),0) AS total FROM live_pay_log WHERE `status` =2) AS live_pay
,(SELECT concat(IFNULL(sum(-coin),0),'_',ifnull(max(id),0)) FROM user_barrage_log ) AS barrage_pay
,(SELECT concat(IFNULL(sum(-coin),0),'_',ifnull(max(id),0)) FROM user_vip_log) AS vip_pay
,(SELECT concat(IFNULL(sum(coin),0),'_',ifnull(max(id),0)) FROM wallet_exchange_log) AS exchange_get;
SQL;
$result = $connection->query($sql)->fetch();
$result['recharge'] = explode('_',$result['recharge']);
$result['send_gift'] = explode('_',$result['send_gift']);
$result['wallet_order'] = explode('_',$result['wallet_order']);
$result['barrage_pay'] = explode('_',$result['barrage_pay']);
$result['vip_pay'] = explode('_',$result['vip_pay']);
$result['exchange_get'] = explode('_',$result['exchange_get']);
$redis_arr = [
'has_coin' => $result['has_coin'],
'live_pay' => $result['live_pay'],
'recharge' => $result['recharge'][0],
'send_gift' => $result['send_gift'][0],
'wallet_order' => $result['wallet_order'][0],
'barrage_pay' => $result['barrage_pay'][0],
'vip_pay' => $result['vip_pay'][0],
'exchange_get' => $result['exchange_get'][0],
];
$max_id_arr = [
'max_recharge_id' => $result['recharge'][1],
'max_send_gift_id' => $result['send_gift'][1],
'max_wallet_order_id' => $result['wallet_order'][1],
'max_barrage_pay_id' => $result['barrage_pay'][1],
'max_vip_pay_id' => $result['vip_pay'][1],
'max_exchange_get_id' => $result['exchange_get'][1],
];
}
$coin_diff_today = array_sum($redis_arr);
$coin_limit = Kv::get('coin_limit', '0');
//coin是否正常
if (abs($coin_diff_today) < $coin_limit) {
//正常
$redis_arr['status'] = 1;
} else {
//异常
$redis_arr['status'] = 2;
}
$redis_arr = array_merge($redis_arr,$max_id_arr);
$redis_arr['coin_diff_today'] = $coin_diff_today;
$redis->hMset($userStatListKey,$redis_arr);
if(!$redisData){
$redis->expire($userStatListKey,3600 * 24);
}
return $redis_arr;
}

redis 数据统计(用自增id防止同一秒并发过大没统计成功)的更多相关文章

  1. mysql插入数据后返回自增ID的方法,last_insert_id(),selectkey

    mysql插入数据后返回自增ID的方法 mysql和oracle插入的时候有一个很大的区别是,oracle支持序列做id,mysql本身有一个列可以做自增长字段,mysql在插入一条数据后,如何能获得 ...

  2. mysql函数之六:mysql插入数据后返回自增ID的方法,last_insert_id(),selectkey

    mysql插入数据后返回自增ID的方法 mysql和oracle插入的时候有一个很大的区别是,oracle支持序列做id,mysql本身有一个列可以做自增长字段,mysql在插入一条数据后,如何能获得 ...

  3. mysql清空表数据并重置自增ID

    mysql清空表数据并重置自增ID: ## 查看mysql> select * from work_order_company;mysql> show create table work_ ...

  4. mysql插入数据后返回自增ID的方法

    mysql和oracle插入的时候有一个很大的区别是,oracle支持序列做id,mysql本身有一个列可以做自增长字段,mysql在插入一条数据后,如何能获得到这个自增id的值呢? 方法一是使用la ...

  5. mybatis插入数据并返回自增Id

    上图mybatis的写法,在xxxMapper.xml中: 加入:useGeneratedKeys="true" keyProperty="applyId" k ...

  6. Mybatis使用generatedKey在插入数据时返回自增id始终为1,自增id实际返回到原对象当中的问题排查

    今天在使用数据库的时候,遇到一个场景,即在插入数据完成后需要返回此数据对应的自增主键id,但是在使用Mybatis中的generatedKey且确认各项配置均正确无误的情况下,每次插入成功后,返回的都 ...

  7. 如何在MySQl数据库中给已有的数据表添加自增ID?

    由于使用MySQL数据库还没有多久的缘故,在搭建后台往数据库导入数据的时候发现新增的表单是没有自增id的,因次就有了上面这个问题. 解决方法 1.给某一张表先增加一个字段,这里我们就以node_tab ...

  8. Sql Server插入数据并返回自增ID,@@IDENTITY,SCOPE_IDENTITY和IDENT_CURRENT的区别

    预备知识:SQLServer的IDENTITY关键字IDENTITY关键字代表的是一个函数,而不是identity属性.在access里边没有这个函数,所以在access不能用这个语句.语法:iden ...

  9. Sql Server插入数据并返回自增ID,@@IDENTITY,SCOPE_IDENTITY和IDENT_CURRENT的区别(转载)

    预备知识:SQL Server的IDENTITY关键字IDENTITY关键字代表的是一个函数,而不是identity属性.在access里边没有这个函数,所以在access不能用这个语句.语法:ide ...

随机推荐

  1. centos7部署fabric-ca错误解决

    1.fabric-ca 编译错误:ltdl.h: no such file 在fabric-ca目录中使用make编译时,会出现如下错误: 解决方案: 如果在ubunt操作系统中,只需安装:apt i ...

  2. Elasticsearch集群管理工具head插件安装

    Elasticsearch-head是一个elasticsearch的集群管理工具,它是完全由html5编写的独立网页程序,你可以通过插件把它集成到es.或直接下载源码,在本地打开index.html ...

  3. pd.concat/merge/join

    pandas的拼接分为两种: 级联:pd.concat, pd.append 合并:pd.merge, pd.join 一.回顾numpy.concatenate 生成1个6*3的矩阵,一个2*3的矩 ...

  4. C++编译变更stlport到使用g++的stl经验总结

    . 花了几天时间,需要把经验给记下来. 1. 需要支持C++11的编译器,最方便的方式就是 yum -y install devtoolset-6 在/usr/local/bin中增加gcc6.sh ...

  5. python class 1

    //test.py class Employee: 'all employee' empCount = 0 def __init__(self, name, salary): self.name = ...

  6. Go linux 实践2

    今天,看看GO的高级语言特性-方法和接口 废话不多说,直接上代码 ************************************************* 1 package main 2 ...

  7. EasyUI扩展——自定义列排序匹配字段

    一些特殊情况下希望实现:单击某些列,但是排序要按照自定义指定另外的列排序 easyui扩展: 如果不写sort属性则按照默认该列的field排序 $.fn.datagrid.defaults.onBe ...

  8. iOS 上传自己的库到cocoapod

    最近自己写了个库,传到github上,想让自己的库支持cocoapod,这里我看了很多相关文章.下面我就写下详细步骤以及会遇到的问题. 我们会使用trunk的方式提交到cocoa pod 这是2014 ...

  9. Mac为python2.7.10安装pip

    首先下载get-pip.py https://bootstrap.pypa.io/get-pip.py alias python="/usr/bin/python2.7" pyth ...

  10. Xshell连接不上虚拟机提示ssh服务器拒绝了密码,请再试一次

    1. 设置ubuntu的管理员root的密码 hughes@hughes-virtual:~$ sudo passwd (供xshell连接时使用) 2. 确保源文件和系统已更新 hughes@hug ...