Should it call method hasKey before get when use redis or other DB

huangapple go评论74阅读模式
英文:

Should it call method hasKey before get when use redis or other DB

问题

以下是翻译好的内容:

当我想从Redis获取数据时,我对于是否在调用redis.get之前调用redis.hasKey方法感到困惑。

有人写成这样:

if (redis.hasKey('xxx')) {
    return redis.get('xxx');
}
return ...

而其他人写成这样:

Object value = redis.get('xxx')
if (value != null) {
    return value
}
return ...

我认为第二种方法更好,因为它只涉及一次Redis操作,而第一种涉及两次。你选择了哪种方法以及为什么?谢谢。

英文:

When I want to get data form Redis, I was confused about that whether or not call method redis.hasKey before redis.get.

Someone write like this:

if (redis.hasKey('xxx')) {
    return redis.get('xxx');
}
return ...

and the others write like this:

Object value = redis.get('xxx')
if (value != null) {
    return value
}
return ...

I think the second one was good, because it just once Redis operation, the first one has two. Which did you choose and why? Thanks.

Sorry for my poor English.

答案1

得分: 1

调用 hasKey 是阻塞的,get 也是阻塞的。只调用 get 并检查结果是否为 null 或 empty,这样更有意义。

英文:

The call to hasKey is blocking and so is get. It makes much more sense to just call get and check if the result is null or empty depending on the object.

huangapple
  • 本文由 发表于 2020年4月6日 20:01:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/61059360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定