util

package
Version: v0.0.0-...-b67df6e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2018 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package util is a collection of helper functions for interacting with various parts of the radix.v2 package

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LuaEval

func LuaEval(c Cmder, script string, keys int, args ...interface{}) *redis.Resp

LuaEval calls EVAL on the given Cmder for the given script, passing the key count and argument list in as well. See http://redis.io/commands/eval for more on how EVAL works and for the meaning of the keys argument.

LuaEval will automatically try to call EVALSHA first in order to preserve bandwidth, and only falls back on EVAL if the script has never been used before.

This method works with any of the Cmder's implemented in radix.v2, including Client, Pool, and Cluster.

r := util.LuaEval(c, `return redis.call('GET', KEYS[1])`, 1, "foo")

func Scan

func Scan(r Cmder, ch chan string, cmd, key, pattern string) error

Scan is DEPRECATED. It contains an inherent race-condition and shouldn't be used. Use NewScanner instead.

Scan is a helper function for performing any of the redis *SCAN functions. It takes in a channel which keys returned by redis will be written to, and returns an error should any occur. The input channel will always be closed when Scan returns, and *must* be read until it is closed.

The key argument is only needed if cmd isn't SCAN

Example SCAN command

ch := make(chan string)
var err error
go func() {
	err = util.Scan(r, ch, "SCAN", "", "*")
}()
for key := range ch {
	// do something with key
}
if err != nil {
	// handle error
}

Example HSCAN command

ch := make(chan string)
var err error
go func() {
	err = util.Scan(r, ch, "HSCAN", "somekey", "*")
}()
for key := range ch {
	// do something with key
}
if err != nil {
	// handle error
}

Types

type Cmder

type Cmder interface {
	Cmd(cmd string, args ...interface{}) *redis.Resp
}

Cmder is an interface which can be used to interchangeably work with either redis.Client (the basic, single connection redis client), pool.Pool, or cluster.Cluster. All three implement a Cmd method (although, as is the case with Cluster, sometimes with different limitations), and therefore all three are Cmders

type ScanOpts

type ScanOpts struct {
	// The scan command to do, e.g. "SCAN", "HSCAN", etc...
	Command string

	// The key to perform the scan on. Only necessary when Command isn't "SCAN"
	Key string

	// An optional pattern to filter returned keys by
	Pattern string

	// An optional count hint to send to redis to indicate number of keys to
	// return per call. This does not affect the actual results of the scan
	// command, but it may be useful for optimizing certain datasets
	Count int
}

ScanOpts are various parameters which can be passed into ScanWithOpts. Some fields are required depending on which type of scan is being done.

type Scanner

type Scanner interface {
	HasNext() bool
	Next() string
	Err() error
}

Scanner is used to iterate through the results of a SCAN call (or HSCAN, SSCAN, etc...). The Cmder may be a Client, Pool, or Cluster.

Once created, call HasNext() on it to determine if there's a waiting value, then Next() to retrieve that value, then repeat. Once HasNext() returns false, call Err() to potentially retrieve an error which stopped the iteration.

Example SCAN command

s := util.NewScanner(cmder, util.ScanOpts{Command: "SCAN"})
for s.HasNext() {
	log.Printf("next: %q", s.Next())
}
if err := s.Err(); err != nil {
	log.Fatal(err)
}

Example HSCAN command

s := util.NewScanner(cmder, util.ScanOpts{Command: "HSCAN", Key: "somekey"})
for s.HasNext() {
	log.Printf("next: %q", s.Next())
}
if err := s.Err(); err != nil {
	log.Fatal(err)
}

HasNext MUST be called before every call to Next. Err MUST be called after HasNext returns false.

func NewScanner

func NewScanner(c Cmder, o ScanOpts) Scanner

NewScanner initializes a Scanner struct with the given options and returns it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL