英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论