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


评论