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. elasticsearch 处理index 一直INITIALIZING状态

    elasticsearch一个节点异常重启后有一个index恢复的过程中状态一直INITIALIZING 处理方法 PUT index_name/_settings { "index&quo ...

  2. js图的数据结构处理----普里姆算法

    //求加权无向连通图的MST的贪心算法 //最小树,最小路径联通各个点 function PRIM(){ var graph = [ [], [undefined,Infinity, 23 ,Infi ...

  3. JavaScriptSerializer的日期转换方案

    1.转换后过滤替换(通用) /// <summary> /// 日期转换 /// </summary> /// <param name="str"&g ...

  4. reveal破解

    ~/Library/Preferences/com.ittybittyapps.Reveal.plist 把IBAApplicationPersistenceData这一项删除就是了.”

  5. unity3d-游戏实战突出重围,第四天 添加角色

    1:添加unity自带的第一人称角色控制器,命名为hreo 2:添加第三人称角色控制器.这里是添加源文件Sources下面的.如箭头指示:而不是“3rd Person Controller”.并命名为 ...

  6. 记录一则expdp任务异常处理案例

    环境:AIX 6.1 + Oracle 10.2.0.4 现象:在XTTS迁移测试阶段,遇到执行几个expdp的导出任务,迟迟没有返回任何信息,对应日志无任何输出,查看任务状态: SQL> se ...

  7. php开启pdo扩展

    在Windows环境下php 5.1以上版本中,pdo和主要数据库的驱动同php一起作为扩展发布,要激活它们只需要简单地编辑php.ini文件. 打开php.ini配置文件,找到extension=p ...

  8. python爬虫-基础入门-爬取整个网站《2》

    python爬虫-基础入门-爬取整个网站<2> 描述: 开场白已在<python爬虫-基础入门-爬取整个网站<1>>中描述过了,这里不在描述,只附上 python3 ...

  9. Ubuntu中使用pip3报错

    使用pip3 出现以下错误: Traceback (most recent call last): File “/usr/bin/pip3”, line 9, in from pip import m ...

  10. Subway (树中心 + 树hash)

    首先找到树的中心或者中心,我这里是找中心,因为我们需要找一个相同的起点,然后最多2个中心就是树的宽度为偶数时,奇数时为1个. 找到之后需要对树进行hash,使得每个点都具备独特性,使之树的形态能够保证 ...