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.