如何在读取哈希键时避免多次Redis调用

时间:2019-02-11 15:45:51

标签: performance caching optimization redis stackexchange.redis

我正在将hashmap缓存到redis,并以以下方式读取特定的hashkey:

var hashValue = redis.HashGet(rediskey, hashkey)   // RedisCall#1
 if (hashValue == null && !redis.KeyExist(rediskey))  // RedisCall#2
 {
   // load from sql and cache it to redis
 }
 else
 {
   return hashValue;
 }

我试图避免必须打2次来给redis打电话。请提出建议,以解决您将如何解决此问题的问题,并只需一个redis调用即可完成。

1 个答案:

答案 0 :(得分:0)

您可以改用pipeline在一个请求中执行两个命令。