forked from redis-go/redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
179 lines (151 loc) · 4.33 KB
/
redis.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package redis
import (
"github.com/redis-go/redcon"
"strings"
"sync"
)
const (
SyntaxErr = "ERR syntax error"
InvalidIntErr = "ERR value is not an integer or out of range"
WrongTypeErr = "WRONGTYPE Operation against a key holding the wrong kind of value"
WrongNumOfArgsErr = "ERR wrong number of arguments for '%s' command"
)
// This is the redis server.
type Redis struct {
// databases/keyspaces
redisDbs RedisDbs
// Locking is important, share this mutex around to provide state.
mu *sync.RWMutex
commands Commands
unknownCommand UnknownCommand
handler Handler
accept Accept
onClose OnClose
// TODO version
// TODO log writer
// TODO modules
// TODO redis options type
keyExpirer KeyExpirer
clients Clients
nextClientId ClientId
}
// A Handler is called when a request is received and after Accept
// (if Accept allowed the connection by returning true).
//
// For implementing an own handler see the default handler
// as a perfect example in the createDefault() function.
type Handler func(c *Client, cmd redcon.Command)
// Accept is called when a Client tries to connect and before everything else,
// the Client connection will be closed instantaneously if the function returns false.
type Accept func(c *Client) bool
// OnClose is called when a Client connection is closed.
type OnClose func(c *Client, err error)
// Client map
type Clients map[ClientId]*Client
// Client id
type ClientId uint64
// Gets the handler func.
func (r *Redis) HandlerFn() Handler {
r.Mu().RLock()
defer r.Mu().RUnlock()
return r.handler
}
// Sets the handler func.
// Live updates (while redis is running) works.
func (r *Redis) SetHandlerFn(new Handler) {
r.Mu().Lock()
defer r.Mu().Unlock()
r.handler = new
}
// Gets the accept func.
func (r *Redis) AcceptFn() Accept {
r.Mu().RLock()
defer r.Mu().RUnlock()
return r.accept
}
// Sets the accept func.
// Live updates (while redis is running) works.
func (r *Redis) SetAcceptFn(new Accept) {
r.Mu().Lock()
defer r.Mu().Unlock()
r.accept = new
}
// Gets the onclose func.
func (r *Redis) OnCloseFn() OnClose {
r.Mu().RLock()
defer r.Mu().RUnlock()
return r.onClose
}
// Sets the onclose func.
// Live updates (while redis is running) works.
func (r *Redis) SetOnCloseFn(new OnClose) {
r.Mu().Lock()
defer r.Mu().Unlock()
r.onClose = new
}
// The mutex of the redis.
func (r *Redis) Mu() *sync.RWMutex {
return r.mu
}
func (r *Redis) KeyExpirer() KeyExpirer {
r.Mu().RLock()
defer r.Mu().RUnlock()
return r.keyExpirer
}
func (r *Redis) SetKeyExpirer(ke KeyExpirer) {
r.Mu().Lock()
defer r.Mu().Unlock()
r.keyExpirer = ke
}
var defaultRedis *Redis
// Default redis server.
// Initializes the default redis if not already.
// You can change the fields or value behind the pointer
// of the returned redis pointer to extend/change the default.
func Default() *Redis {
if defaultRedis != nil {
return defaultRedis
}
defaultRedis = createDefault()
return defaultRedis
}
// createDefault creates a new default redis.
func createDefault() *Redis {
// initialize default redis server
r := &Redis{
mu: new(sync.RWMutex),
accept: func(c *Client) bool {
return true
},
onClose: func(c *Client, err error) {
},
handler: func(c *Client, cmd redcon.Command) {
cmdl := strings.ToLower(string(cmd.Args[0]))
if c.Redis().CommandExists(cmdl) {
c.Redis().CommandHandlerFn(cmdl)(c, cmd)
} else {
c.Redis().UnknownCommandFn()(c, cmd)
}
},
unknownCommand: func(c *Client, cmd redcon.Command) {
c.Conn().WriteError("ERR unknown command '" + string(cmd.Args[0]) + "'")
},
commands: make(Commands, 0),
}
r.redisDbs = make(RedisDbs, redisDbMapSizeDefault)
r.RedisDb(0) // initializes default db 0
r.keyExpirer = KeyExpirer(NewKeyExpirer(r))
r.RegisterCommands([]*Command{
NewCommand("ping", PingCommand, CMD_STALE, CMD_FAST),
NewCommand("set", SetCommand, CMD_WRITE, CMD_DENYOOM),
NewCommand("get", GetCommand, CMD_READONLY, CMD_FAST),
NewCommand("del", DelCommand, CMD_WRITE),
NewCommand("ttl", TtlCommand, CMD_READONLY, CMD_FAST),
NewCommand("lpush", LPushCommand, CMD_WRITE, CMD_FAST, CMD_DENYOOM),
NewCommand("rpush", RPushCommand, CMD_WRITE, CMD_FAST, CMD_DENYOOM),
NewCommand("lpop", LPopCommand, CMD_WRITE, CMD_FAST),
NewCommand("rpop", RPopCommand, CMD_WRITE, CMD_FAST),
NewCommand("lrange", LRangeCommand, CMD_READONLY),
})
return r
}