-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.go
62 lines (58 loc) · 1.08 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
package main
import (
"fmt"
"menteslibres.net/gosexy/redis"
"os"
"strconv"
)
var redisCli *redis.Client
func main() {
host := os.Getenv("REDIS_HOST")
if host == "" {
host = "localhost"
}
portStr := os.Getenv("REDIS_PORT")
if portStr == "" {
portStr = "6379"
}
port, err := strconv.Atoi(portStr)
if err != nil {
os.Exit(1)
}
redisCli = redis.New()
err = redisCli.Connect(host, uint(port))
if err != nil {
os.Exit(1)
}
defer redisCli.Quit()
cmd := os.Args[1]
switch cmd {
case "get":
value, err := redisCli.Get(os.Args[2])
if err == nil {
fmt.Println(value)
}
case "set":
redisCli.Set(os.Args[2], os.Args[3])
case "setex":
args := os.Args[3]
seconds, err := strconv.Atoi(args)
if err == nil {
redisCli.SetEx(os.Args[2], int64(seconds), os.Args[4])
}
case "del":
redisCli.Del(os.Args[2])
case "incr":
value, err := redisCli.Incr(os.Args[2])
if err == nil {
fmt.Println(value)
}
case "decr":
value, err := redisCli.Decr(os.Args[2])
if err == nil {
fmt.Println(value)
}
case "publish":
redisCli.Publish(os.Args[2], os.Args[3])
}
}