Skip to content

Commit 7b4f30c

Browse files
committed
Add client tracking and client trackinginfo commands
1 parent 531f068 commit 7b4f30c

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

commands.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ type StatefulCmdable interface {
236236
SwapDB(ctx context.Context, index1, index2 int) *StatusCmd
237237
ClientSetName(ctx context.Context, name string) *BoolCmd
238238
ClientSetInfo(ctx context.Context, info LibraryInfo) *StatusCmd
239+
ClientTracking(ctx context.Context, on bool, options *ClientTrackingOptions) *StatusCmd
240+
ClientTrackingOn(ctx context.Context) *StatusCmd
241+
ClientTrackingOff(ctx context.Context) *StatusCmd
242+
ClientTrackingOnWithArgs(ctx context.Context, options *ClientTrackingOptions) *StatusCmd
243+
ClientTrackingOffWithArgs(ctx context.Context, options *ClientTrackingOptions) *StatusCmd
244+
ClientTrackingInfo(ctx context.Context) *MapStringInterfaceCmd
239245
Hello(ctx context.Context, ver int, username, password, clientName string) *MapStringInterfaceCmd
240246
}
241247

@@ -319,6 +325,91 @@ func (c statefulCmdable) ClientSetInfo(ctx context.Context, info LibraryInfo) *S
319325
return cmd
320326
}
321327

328+
type ClientTrackingOptions struct {
329+
ClientID int
330+
Prefixes []interface{}
331+
BCast bool
332+
OptIn bool
333+
OptOut bool
334+
NoLoop bool
335+
}
336+
337+
// Enables the tracking feature of the Redis server, that is used
338+
// for server assisted client side caching.
339+
// “on“ indicate for tracking on or tracking off. The dafualt is on.
340+
// “clientid“ send invalidation messages to the connection with
341+
// the specified ID.
342+
// “bcast“ enable tracking in broadcasting mode. In this mode
343+
// invalidation messages are reported for all the prefixes
344+
// specified, regardless of the keys requested by the connection.
345+
// “optin“ when broadcasting is NOT active, normally don't track
346+
// keys in read only commands, unless they are called immediately
347+
// after a CLIENT CACHING yes command.
348+
// “optout“ when broadcasting is NOT active, normally track keys in
349+
// read only commands, unless they are called immediately after a
350+
// CLIENT CACHING no command.
351+
// “noloop“ don't send notifications about keys modified by this
352+
// connection itself.
353+
// “prefixes“ for broadcasting, register a given key prefix, so that
354+
// notifications will be provided only for keys starting with this string.
355+
// For more innformation - https://redis.io/commands/client-tracking
356+
func (c statefulCmdable) ClientTracking(ctx context.Context, on bool, options *ClientTrackingOptions) *StatusCmd {
357+
args := []interface{}{"CLIENT", "TRACKING"}
358+
if on {
359+
args = append(args, "ON")
360+
} else {
361+
args = append(args, "OFF")
362+
}
363+
if options != nil {
364+
if options.Prefixes != nil && !options.BCast {
365+
panic("prefixes can only be used with BCast")
366+
}
367+
if options.ClientID != 0 {
368+
args = append(args, "REDIRECT", options.ClientID)
369+
}
370+
for _, prefix := range options.Prefixes {
371+
args = append(args, "PREFIX", prefix)
372+
}
373+
if options.BCast {
374+
args = append(args, "BCAST")
375+
}
376+
if options.OptIn {
377+
args = append(args, "OPTIN")
378+
}
379+
if options.OptOut {
380+
args = append(args, "OPTOUT")
381+
}
382+
if options.NoLoop {
383+
args = append(args, "NOLOOP")
384+
}
385+
}
386+
cmd := NewStatusCmd(ctx, args...)
387+
_ = c(ctx, cmd)
388+
return cmd
389+
}
390+
391+
func (c statefulCmdable) ClientTrackingOn(ctx context.Context) *StatusCmd {
392+
return c.ClientTracking(ctx, true, nil)
393+
}
394+
395+
func (c statefulCmdable) ClientTrackingOff(ctx context.Context) *StatusCmd {
396+
return c.ClientTracking(ctx, false, nil)
397+
}
398+
399+
func (c statefulCmdable) ClientTrackingOnWithArgs(ctx context.Context, options *ClientTrackingOptions) *StatusCmd {
400+
return c.ClientTracking(ctx, true, options)
401+
}
402+
403+
func (c statefulCmdable) ClientTrackingOffWithArgs(ctx context.Context, options *ClientTrackingOptions) *StatusCmd {
404+
return c.ClientTracking(ctx, false, options)
405+
}
406+
407+
func (c statefulCmdable) ClientTrackingInfo(ctx context.Context) *MapStringInterfaceCmd {
408+
cmd := NewMapStringInterfaceCmd(ctx, "CLIENT", "TRACKINGINFO")
409+
_ = c(ctx, cmd)
410+
return cmd
411+
}
412+
322413
// Validate checks if only one field in the struct is non-nil.
323414
func (info LibraryInfo) Validate() error {
324415
if info.LibName != nil && info.LibVer != nil {

commands_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ var _ = Describe("Commands", func() {
243243
Expect(get.Val()).To(Equal("theclientname"))
244244
})
245245

246+
It("should ClientTrackingInfo", func() {
247+
clientTrackingInfo, err := client.Conn().ClientTrackingInfo(ctx).Result()
248+
Expect(err).NotTo(HaveOccurred())
249+
Expect(clientTrackingInfo["prefixes"]).To(Equal([]interface{}{}))
250+
})
251+
246252
It("should ClientSetInfo", func() {
247253
pipe := client.Pipeline()
248254

0 commit comments

Comments
 (0)