Skip to content

Commit

Permalink
feat(errors): added New
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 committed Oct 31, 2023
1 parent d733dd7 commit 0285eb1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
32 changes: 22 additions & 10 deletions cache/redis/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions contract/cache/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions contract/cache/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 0285eb1

Please sign in to comment.