fix: lru cache(get nil err)
This commit is contained in:
parent
fe519cb595
commit
5c852fe559
9
internal/database/cache/cache_memory.go
vendored
9
internal/database/cache/cache_memory.go
vendored
@ -2,6 +2,7 @@ package cache
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
"ultone/internal/interfaces"
|
"ultone/internal/interfaces"
|
||||||
@ -18,6 +19,10 @@ type _mem struct {
|
|||||||
func (m *_mem) Get(ctx context.Context, key string) ([]byte, error) {
|
func (m *_mem) Get(ctx context.Context, key string) ([]byte, error) {
|
||||||
v, err := m.client.Get(key)
|
v, err := m.client.Get(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, gredis.ErrKeyNotFound) {
|
||||||
|
return nil, ErrorKeyNotFound
|
||||||
|
}
|
||||||
|
|
||||||
return nil, err
|
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) {
|
func (m *_mem) GetEx(ctx context.Context, key string, duration time.Duration) ([]byte, error) {
|
||||||
v, err := m.client.GetEx(key, duration)
|
v, err := m.client.GetEx(key, duration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, gredis.ErrKeyNotFound) {
|
||||||
|
return nil, ErrorKeyNotFound
|
||||||
|
}
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
9
internal/database/cache/cache_redis.go
vendored
9
internal/database/cache/cache_redis.go
vendored
@ -2,6 +2,7 @@ package cache
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -13,6 +14,10 @@ type _redis struct {
|
|||||||
func (r *_redis) Get(ctx context.Context, key string) ([]byte, error) {
|
func (r *_redis) Get(ctx context.Context, key string) ([]byte, error) {
|
||||||
result, err := r.client.Get(ctx, key).Result()
|
result, err := r.client.Get(ctx, key).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, redis.Nil) {
|
||||||
|
return nil, ErrorKeyNotFound
|
||||||
|
}
|
||||||
|
|
||||||
return nil, err
|
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) {
|
func (r *_redis) GetEx(ctx context.Context, key string, duration time.Duration) ([]byte, error) {
|
||||||
result, err := r.client.GetEx(ctx, key, duration).Result()
|
result, err := r.client.GetEx(ctx, key, duration).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, redis.Nil) {
|
||||||
|
return nil, ErrorKeyNotFound
|
||||||
|
}
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,12 +2,11 @@ package auth
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"gitea.com/taozitaozi/gredis"
|
|
||||||
"github.com/go-redis/redis/v8"
|
|
||||||
"github.com/loveuer/nf"
|
"github.com/loveuer/nf"
|
||||||
"github.com/loveuer/nf/nft/resp"
|
"github.com/loveuer/nf/nft/resp"
|
||||||
"strings"
|
"strings"
|
||||||
"ultone/internal/controller"
|
"ultone/internal/controller"
|
||||||
|
"ultone/internal/database/cache"
|
||||||
"ultone/internal/log"
|
"ultone/internal/log"
|
||||||
"ultone/internal/opt"
|
"ultone/internal/opt"
|
||||||
)
|
)
|
||||||
@ -36,7 +35,7 @@ func NewAuth() nf.HandlerFunc {
|
|||||||
target, err := controller.UserController.GetUserByToken(c.Context(), token)
|
target, err := controller.UserController.GetUserByToken(c.Context(), token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(c.Context(), "middleware.NewAuth: get user by token=%s err=%v", token, err)
|
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)
|
return resp.Resp401(c, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user