donglv7097 2019-07-28 17:54
浏览 78

需要使用Redis来计算48小时的分数变化

I need to use Redis to calculate the actual score received compared to the one received in the same time but 48 hours ago.

Doing that using MySQL is easy, I have just to do this:

SELECT 
    `score` 
FROM game_history 
WHERE 
  user_id='$user_id' AND date <= DATE_SUB(NOW(), INTERVAL 2 DAY) 
ORDER BY date DESC 
LIMIT 1

And then:

$last_24_score = $game_history_row["score"];
$score_change = ($actual_score - $last_24_score / $last_24_score) * 100;

I want to use Redis to get faster result, and better latency compared to the actual result ! I integrated Redis for some functionalities of my game and now I am getting better performances.

To retrieve 48 hours score changes to my users, I need to achieve this goal and do it from Redis.

So, my approach is like this:

1- Store new scores to Redis (Need to set expiration for each sub key to let each entry expire after 48 hours of time):

$redis->HSET('score-'.$user_id, $actual_timestamp, serialize($new_score));                  
$redis->expire('score-'.$user_id, 60*60*24*2);

Note: This is an incorrect code since the expiration is not for each inserted value but the whole key.

2- Receive event with the value when expired (I am stucking on this too - Need solution for this)

3- Store the new event to Redis

$redis->set('48-score-'.$user_id, serialize($48_score));                  
//Without expiration

The goal here in order to get quickly the 48 hours change, I need only to get the key: '48-score-'.$user_id :

$redis->get('48-score-'.$user_id); 

So, my big barrier to achieve it is to get a way to automatically get the actual expired key (from the step 1) and store it in a new key that do not expire in a step 3.

If you have any solution you are welcomed or even better approach :)

Thank you in advance.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大