fix: lru cache(get nil err)

This commit is contained in:
loveuer 2024-07-19 15:42:26 +08:00
parent fe519cb595
commit 5c852fe559
3 changed files with 20 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package cache
import (
"context"
"errors"
"fmt"
"time"
"ultone/internal/interfaces"
@ -18,6 +19,10 @@ type _mem struct {
func (m *_mem) Get(ctx context.Context, key string) ([]byte, error) {
v, err := m.client.Get(key)
if err != nil {
if errors.Is(err, gredis.ErrKeyNotFound) {
return nil, ErrorKeyNotFound
}
return nil, err
}
@ -32,6 +37,10 @@ func (m *_mem) Get(ctx context.Context, key string) ([]byte, error) {
func (m *_mem) GetEx(ctx context.Context, key string, duration time.Duration) ([]byte, error) {
v, err := m.client.GetEx(key, duration)
if err != nil {
if errors.Is(err, gredis.ErrKeyNotFound) {
return nil, ErrorKeyNotFound
}
return nil, err
}

View File

@ -2,6 +2,7 @@ package cache
import (
"context"
"errors"
"github.com/go-redis/redis/v8"
"time"
)
@ -13,6 +14,10 @@ type _redis struct {
func (r *_redis) Get(ctx context.Context, key string) ([]byte, error) {
result, err := r.client.Get(ctx, key).Result()
if err != nil {
if errors.Is(err, redis.Nil) {
return nil, ErrorKeyNotFound
}
return nil, err
}
@ -22,6 +27,10 @@ func (r *_redis) Get(ctx context.Context, key string) ([]byte, error) {
func (r *_redis) GetEx(ctx context.Context, key string, duration time.Duration) ([]byte, error) {
result, err := r.client.GetEx(ctx, key, duration).Result()
if err != nil {
if errors.Is(err, redis.Nil) {
return nil, ErrorKeyNotFound
}
return nil, err
}

View File

@ -2,12 +2,11 @@ package auth
import (
"errors"
"gitea.com/taozitaozi/gredis"
"github.com/go-redis/redis/v8"
"github.com/loveuer/nf"
"github.com/loveuer/nf/nft/resp"
"strings"
"ultone/internal/controller"
"ultone/internal/database/cache"
"ultone/internal/log"
"ultone/internal/opt"
)
@ -36,7 +35,7 @@ func NewAuth() nf.HandlerFunc {
target, err := controller.UserController.GetUserByToken(c.Context(), token)
if err != nil {
log.Error(c.Context(), "middleware.NewAuth: get user by token=%s err=%v", token, err)
if errors.Is(err, redis.Nil) || errors.Is(err, gredis.ErrKeyNotFound) {
if errors.Is(err, cache.ErrorKeyNotFound) {
return resp.Resp401(c, err)
}