首页 > 解决方案 > 读取hashkey时如何避免多次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;
 }

我试图避免我必须对 redis 进行两次调用。请就如何解决这个问题提出建议,并在一个 redis 调用中做到这一点。

标签: performancecachingoptimizationredisstackexchange.redis

解决方案


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


推荐阅读