From 0285eb1e5329654dfd79a674d8d964245e64a5e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flc=E3=82=9B?= Date: Tue, 31 Oct 2023 18:21:11 +0800 Subject: [PATCH] feat(errors): added `New` --- cache/redis/store.go | 32 ++++++++++++++++++++++---------- contract/cache/repository.go | 4 ++-- contract/cache/store.go | 8 ++++---- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/cache/redis/store.go b/cache/redis/store.go index 490496d7..59e531e1 100644 --- a/cache/redis/store.go +++ b/cache/redis/store.go @@ -88,11 +88,13 @@ func (s *Store) Get(ctx context.Context, key string, dest interface{}) error { return s.opt.serializer.Unserialize([]byte(result.Val()), dest) } -func (s *Store) Put(ctx context.Context, key string, value interface{}, ttl time.Duration) error { +func (s *Store) Put(ctx context.Context, key string, value interface{}, ttl time.Duration) (bool, error) { if data, err := s.opt.serializer.Serialize(value); err != nil { - return err + return false, err + } else if ok, err := s.opt.redis.SetEx(ctx, s.opt.prefix+key, data, ttl).Result(); err != nil { + return false, err } else { - return s.opt.redis.SetEx(ctx, s.opt.prefix+key, data, ttl).Err() + return ok == "OK", nil } } @@ -112,20 +114,30 @@ func (s *Store) Decrement(ctx context.Context, key string, value int) (int, erro } } -func (s *Store) Forever(ctx context.Context, key string, value interface{}) error { +func (s *Store) Forever(ctx context.Context, key string, value interface{}) (bool, error) { if data, err := s.opt.serializer.Serialize(value); err != nil { - return err + return false, err + } else if ok, err := s.opt.redis.Set(ctx, s.opt.prefix+key, data, redis.KeepTTL).Result(); err != nil { + return false, err } else { - return s.opt.redis.Set(ctx, s.opt.prefix+key, data, redis.KeepTTL).Err() + return ok == "OK", nil } } -func (s *Store) Forget(ctx context.Context, key string) error { - return s.opt.redis.Del(ctx, s.opt.prefix+key).Err() +func (s *Store) Forget(ctx context.Context, key string) (bool, error) { + if deleted, err := s.opt.redis.Del(ctx, s.opt.prefix+key).Result(); err != nil { + return false, err + } else { + return deleted == 1, nil + } } -func (s *Store) Flush(ctx context.Context) error { - return s.opt.redis.FlushAll(ctx).Err() +func (s *Store) Flush(ctx context.Context) (bool, error) { + if ok, err := s.opt.redis.FlushAll(ctx).Result(); err != nil { + return false, err + } else { + return ok == "OK", nil + } } func (s *Store) GetPrefix() string { diff --git a/contract/cache/repository.go b/contract/cache/repository.go index c53bd396..3f511674 100644 --- a/contract/cache/repository.go +++ b/contract/cache/repository.go @@ -15,7 +15,7 @@ type Repository interface { Addable Missing(ctx context.Context, key string) (bool, error) - Delete(ctx context.Context, key string) error - Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error + Delete(ctx context.Context, key string) (bool, error) + Set(ctx context.Context, key string, value interface{}, ttl time.Duration) (bool, error) Remember(ctx context.Context, key string, dest interface{}, value func() interface{}, ttl time.Duration) error } diff --git a/contract/cache/store.go b/contract/cache/store.go index 12db2529..6bc54636 100644 --- a/contract/cache/store.go +++ b/contract/cache/store.go @@ -10,17 +10,17 @@ type Store interface { Get(ctx context.Context, key string, dest interface{}) error - Put(ctx context.Context, key string, value interface{}, ttl time.Duration) error + Put(ctx context.Context, key string, value interface{}, ttl time.Duration) (bool, error) Increment(ctx context.Context, key string, value int) (int, error) Decrement(ctx context.Context, key string, value int) (int, error) - Forever(ctx context.Context, key string, value interface{}) error + Forever(ctx context.Context, key string, value interface{}) (bool, error) - Forget(ctx context.Context, key string) error + Forget(ctx context.Context, key string) (bool, error) - Flush(ctx context.Context) error + Flush(ctx context.Context) (bool, error) GetPrefix() string }