错误的EntityNotFound来自Store Layer Redis Golang

发布于 2025-02-04 13:37:17 字数 257 浏览 2 评论 0原文

我有一个Golang项目,该项目将REDIS实现为后端商店。在存储层的GET方法中,我想检查实体是否存在。如果实体存在现有,我将返回否则,我有两种情况:

  • 错误entitynotfound
  • db错误,

我只能返回DB错误。我如何返回未找到错误的实体。就像在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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只涨不跌 2025-02-11 13:37:17

执行密钥查找时,REDIS客户端将返回错误,如果键不存在。您可以检查该错误是否等于redis.nil,这表明找不到键。

value, err = client.Get(ctx, key)
if errors.Is(err, redis.Nil) {
  return EntityNotFound
} else if err != nil {
  // Some other error
  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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文