I've some leaderboard stats for my players. Leaderboards have daily,weekly and monthly stats. So I using redis with its expire feature for leaderboards but there's a thing that I'd want to know. Is there any method that make key expired but if the key has expire already, it will not touch it. If the method doesn't exist, what is the more performance friendly method to check and set it? I've a lot of keys to check and set I don't want to slow the process.
Code
try (Jedis resource = redisManager.getResource()) {
Pipeline pipeline = resource.pipelined();
pipeline.multi();
for (PlayerData playerData : playerDataList) {
if (playerData.getKill() > 0) {
pipeline.zincrby("game.kill.total", playerData.getKill(), playerData.getId());
pipeline.zincrby("game.kill.monthly", playerData.getKill(), playerData.getId());
pipeline.zincrby("game.kill.weekly", playerData.getKill(), playerData.getId());
}
}
//if the key doesn't have expire date, add expire date to key.
pipeline.exec();
}
If you key in the Redis is expired, Redis will handle it internally, by deleting them periodically. check official document
Redis exist
API can help to check if the key exists. doc In Java Spring RedisTemplate hasKey API can help.
Best practice to handle Redis key is
If you're using Redis in multithread, it's another story. You have to use Redis Lock.