Skip to content

Commit 4c6d248

Browse files
committed
client-cache invalidation message parsing code
1 parent 2d8fa02 commit 4c6d248

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

pubsub.go

+14
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,20 @@ func (c *PubSub) newMessage(reply interface{}) (interface{}, error) {
409409
return &Pong{
410410
Payload: reply[1].(string),
411411
}, nil
412+
case "invalidate":
413+
switch payload := reply[1].(type) {
414+
case []interface{}:
415+
s := make([]string, len(payload))
416+
for idx := range payload {
417+
s[idx] = payload[idx].(string)
418+
}
419+
return &Message{
420+
Channel: "invalidate",
421+
PayloadSlice: s,
422+
}, nil
423+
default:
424+
return nil, fmt.Errorf("redis: unsupported invalidate message payload: %#v", payload)
425+
}
412426
default:
413427
return nil, fmt.Errorf("redis: unsupported pubsub message: %q", kind)
414428
}

pubsub_test.go

+97
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package redis_test
22

33
import (
4+
"context"
5+
"fmt"
46
"io"
57
"net"
68
"sync"
@@ -567,4 +569,99 @@ var _ = Describe("PubSub", func() {
567569
Expect(msg.Channel).To(Equal("mychannel"))
568570
Expect(msg.Payload).To(Equal(text))
569571
})
572+
573+
It("supports client-cache invalidation messages", func() {
574+
ch := make(chan []string, 2)
575+
defer close(ch)
576+
client := redis.NewClient(getOptsWithTracking(redisOptions(), func(keys []string) error {
577+
ch <- keys
578+
return nil
579+
}))
580+
defer client.Close()
581+
582+
v1 := client.Get(context.Background(), "foo")
583+
Expect(v1.Val()).To(Equal(""))
584+
s1 := client.Set(context.Background(), "foo", "bar", time.Duration(time.Minute))
585+
Expect(s1.Val()).To(Equal("OK"))
586+
v2 := client.Get(context.Background(), "foo")
587+
Expect(v2.Val()).To(Equal("bar"))
588+
// sleep a little to allow time for the first invalidation message to come through
589+
time.Sleep(time.Second)
590+
s2 := client.Set(context.Background(), "foo", "foobar", time.Duration(time.Minute))
591+
Expect(s2.Val()).To(Equal("OK"))
592+
593+
for i := 0; i < 2; i++ {
594+
select {
595+
case keys := <-ch:
596+
Expect(keys).ToNot(BeEmpty())
597+
Expect(keys[0]).To(Equal("foo"))
598+
case <-time.After(10 * time.Second):
599+
// fail on timeouts
600+
Fail("invalidation message wait timed out")
601+
}
602+
}
603+
})
604+
570605
})
606+
607+
func getOptsWithTracking(opt *redis.Options, processInvalidKeysFunc func([]string) error) *redis.Options {
608+
var mu sync.Mutex
609+
invalidateClientID := int64(-1)
610+
invalidateOpts := *opt
611+
invalidateOpts.OnConnect = func(ctx context.Context, conn *redis.Conn) (err error) {
612+
invalidateClientID, err = conn.ClientID(ctx).Result()
613+
return
614+
}
615+
616+
startBackgroundInvalidationSubscription := func(ctx context.Context) int64 {
617+
mu.Lock()
618+
defer mu.Unlock()
619+
620+
if invalidateClientID != -1 {
621+
return invalidateClientID
622+
}
623+
624+
invalidateClient := redis.NewClient(&invalidateOpts)
625+
invalidations := invalidateClient.Subscribe(ctx, "__redis__:invalidate")
626+
627+
go func() {
628+
defer func() {
629+
invalidations.Close()
630+
invalidateClient.Close()
631+
632+
mu.Lock()
633+
invalidateClientID = -1
634+
mu.Unlock()
635+
}()
636+
637+
for {
638+
msg, err := invalidations.ReceiveMessage(context.Background())
639+
if err == io.EOF || err == context.Canceled {
640+
return
641+
} else if err != nil {
642+
fmt.Printf("warning: subscription on key invalidations aborted: %s\n", err.Error())
643+
// send back empty []string to fail the test
644+
processInvalidKeysFunc([]string{})
645+
return
646+
}
647+
648+
processInvalidKeysFunc(msg.PayloadSlice)
649+
}
650+
}()
651+
652+
return invalidateClientID
653+
}
654+
655+
opt.OnConnect = func(ctx context.Context, conn *redis.Conn) error {
656+
invalidateClientID := startBackgroundInvalidationSubscription(ctx)
657+
return conn.Process(
658+
ctx,
659+
redis.NewBoolCmd(
660+
ctx,
661+
"CLIENT", "TRACKING", "on",
662+
"REDIRECT", fmt.Sprintf("%d", invalidateClientID),
663+
),
664+
)
665+
}
666+
return opt
667+
}

0 commit comments

Comments
 (0)