从存储层Redis Golang中发生了EntityNotFound错误。

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

Error EntityNotFound from store layer Redis Golang

问题

我有一个使用redis作为后端存储的Golang项目。在存储层的Get方法中,我想检查实体是否存在。如果实体存在,我将返回它;否则,我有两种情况:

  • 错误EntityNotFound
  • 数据库错误

我只能返回数据库错误。如何返回实体未找到的错误?在redis中,检查的条件是什么?

请帮忙解答。

英文:

I have a golang Project that implements redis as it's backend store. In the Get method of the store layer, I want to check if an entity is existing or not. If the entity is existing i will return it else i have two cases :

  • Error EntityNotFound
  • DB Error

I am only able to return the DB Error. How can i return the entity not found error. Like what's the condition for checking in redis.

Help Please

答案1

得分: 1

当进行键查找时,如果键不存在,Redis客户端将返回一个错误。您可以检查该错误是否等于redis.Nil,这表示未找到该键。

value, err = client.Get(ctx, key)
if errors.Is(err, redis.Nil) {
  return EntityNotFound
} else if err != nil {
  // 其他错误
  return err
}
英文:

When doing a key lookup the Redis client will return an error if the key does not exist. You can check if that error is equal to redis.Nil, which indicates that the key was not found.

value, err = client.Get(ctx, key)
if errors.Is(err, redis.Nil) {
  return EntityNotFound
} else if err != nil {
  // Some other error
  return err
}

huangapple
  • 本文由 发表于 2022年6月3日 13:43:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/72485179.html
匿名

发表评论

匿名网友

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

确定