forked from mediocregopher/radix.v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
54 lines (47 loc) · 1.54 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Package util is a collection of helper functions for interacting with various
// parts of the radix.v2 package
package util
import (
"github.com/mediocregopher/radix.v2/cluster"
"github.com/mediocregopher/radix.v2/pool"
"github.com/mediocregopher/radix.v2/redis"
)
// 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 Cmder interface {
Cmd(cmd string, args ...interface{}) *redis.Resp
}
// withClientForKey is useful for retrieving a single client which can handle
// the given key and perform one or more requests on them, especially when the
// passed in Cmder is actually a Cluster or Pool.
//
// The function given takes a Cmder and not a Client because the passed in Cmder
// may not be one implemented in radix.v2, and in that case may not actually
// have a way of mapping to a Client. In that case it is simply passed directly
// through to fn.
func withClientForKey(c Cmder, key string, fn func(c Cmder)) error {
var singleC Cmder
switch cc := c.(type) {
case *cluster.Cluster:
client, err := cc.GetForKey(key)
if err != nil {
return err
}
defer cc.Put(client)
singleC = client
case *pool.Pool:
client, err := cc.Get()
if err != nil {
return err
}
defer cc.Put(client)
singleC = client
default:
singleC = cc
}
fn(singleC)
return nil
}