解决超出发放礼物问题-巡乐会-日志加缓存
This commit is contained in:
@@ -7,7 +7,8 @@ use think\Log;
|
||||
use think\Model;
|
||||
use think\Db;
|
||||
use think\Session;
|
||||
use think\cache\driver\Redis;
|
||||
//use think\cache\driver\Redis;
|
||||
use Redis;
|
||||
|
||||
/*
|
||||
* 盲盒转盘优化后方法
|
||||
@@ -20,7 +21,16 @@ class BlindBoxTurntableGiftDraw extends Model
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->redis = new Redis(['database' => 1]);
|
||||
try {
|
||||
$this->redis = new Redis();
|
||||
// 连接到Redis服务器
|
||||
$this->redis->connect(config('redis.host'), config('redis.port')); // 根据实际配置调整主机和端口
|
||||
// 选择数据库1
|
||||
$this->redis->select(1);
|
||||
} catch (\Exception $e) {
|
||||
Log::record('Redis连接失败: ' . $e->getMessage(), 'error');
|
||||
$this->redis = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,7 +70,6 @@ class BlindBoxTurntableGiftDraw extends Model
|
||||
$expectedCount = count(explode(',', $gift_user_ids)) * $num;
|
||||
|
||||
if(count($precomputedResults) != $expectedCount){
|
||||
Log::record('抽奖结果数量与预期数量不一致,请重试!预期: '.$expectedCount.', 实际: '.count($precomputedResults).': ' . $room_id."【数据】".var_export($precomputedResults, true),"infos-mhzp");
|
||||
// 记录错误到Redis
|
||||
$this->recordDrawErrorToRedis($expectedCount, count($precomputedResults), $room_id, $user_id, $gift_bag_id, $num, $gift_user_ids, $precomputedResults);
|
||||
return ['code' => 0, 'msg' => '抽奖结果数量与预期数量不一致,请重试! ', 'data' => null];
|
||||
@@ -1566,6 +1575,13 @@ class BlindBoxTurntableGiftDraw extends Model
|
||||
*/
|
||||
private function recordDrawErrorToRedis($expectedCount, $actualCount, $room_id, $user_id, $gift_bag_id, $num, $gift_user_ids, $precomputedResults)
|
||||
{
|
||||
// 检查Redis连接是否可用
|
||||
if (!$this->redis) {
|
||||
Log::record('Redis连接不可用,无法记录错误日志', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$errorData = [
|
||||
'timestamp' => time(),
|
||||
'expected_count' => $expectedCount,
|
||||
@@ -1580,7 +1596,11 @@ class BlindBoxTurntableGiftDraw extends Model
|
||||
'error_type' => 'draw_count_mismatch'
|
||||
];
|
||||
|
||||
// 将错误信息推送到Redis列表中
|
||||
$this->redis->set('blind_box_draw_errors', json_encode($errorData),86400 * 7);
|
||||
// 使用正确的Redis方法存储数据
|
||||
$key = 'blind_box_draw_errors_' . date('Y-m-d-H-i-s');
|
||||
$this->redis->setex($key, 86400 * 7, json_encode($errorData));
|
||||
} catch (\Exception $e) {
|
||||
Log::record('Redis操作失败: ' . $e->getMessage(), 'error');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user