Skip to content

Commit e2bd50d

Browse files
authored
redis: add NewWithCodec (#46)
1 parent a8a6a0e commit e2bd50d

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

store/redis/redis.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ func Register() {
4040

4141
// New creates a new Redis client given a list of endpoints and optional tls config.
4242
func New(endpoints []string, options *store.Config) (store.Store, error) {
43+
return NewWithCodec(endpoints, options, &RawCodec{})
44+
}
45+
46+
// NewWithCodec creates a new Redis client with codec config.
47+
func NewWithCodec(endpoints []string, options *store.Config, codec Codec) (store.Store, error) {
4348
if len(endpoints) > 1 {
4449
return nil, ErrMultipleEndpointsUnsupported
4550
}
@@ -52,7 +57,7 @@ func New(endpoints []string, options *store.Config) (store.Store, error) {
5257
password = options.Password
5358
}
5459

55-
return newRedis(context.Background(), endpoints, password, &RawCodec{}), nil
60+
return newRedis(context.Background(), endpoints, password, codec), nil
5661
}
5762

5863
func newRedis(ctx context.Context, endpoints []string, password string, codec Codec) *Redis {
@@ -450,9 +455,14 @@ type pusher func(interface{})
450455
func watchLoop(msgCh chan *redis.Message, _ <-chan struct{}, get getter, push pusher) error {
451456
// deliver the original data before we setup any events.
452457
pair, err := get()
453-
if err != nil {
458+
if err != nil && !errors.Is(err, store.ErrKeyNotFound) {
454459
return err
455460
}
461+
462+
if errors.Is(err, store.ErrKeyNotFound) {
463+
pair = &store.KVPair{}
464+
}
465+
456466
push(pair)
457467

458468
for m := range msgCh {
@@ -463,11 +473,11 @@ func watchLoop(msgCh chan *redis.Message, _ <-chan struct{}, get getter, push pu
463473
}
464474

465475
// in case of watching a key that has been expired or deleted return and empty KV.
466-
if errors.Is(err, store.ErrKeyNotFound) && (m.Payload == "expire" || m.Payload == "del") {
467-
push(&store.KVPair{})
468-
} else {
469-
push(pair)
476+
if errors.Is(err, store.ErrKeyNotFound) && (m.Payload == "expired" || m.Payload == "del") {
477+
pair = &store.KVPair{}
470478
}
479+
480+
push(pair)
471481
}
472482

473483
return nil

0 commit comments

Comments
 (0)