-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.go
71 lines (55 loc) · 1.56 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
package core
import (
"github.com/blizztrack/owmods/system"
"os"
"strconv"
"time"
"github.com/go-redis/redis"
)
type RedisClient struct {
Client *redis.Client
Addr string
Password string
Database int
}
var RedisManager *RedisClient
func NewRedis() {
db, err := strconv.Atoi(system.Redis().Database)
if err != nil {
db = 0
}
client := redis.NewClient(&redis.Options{
Addr: system.Redis().Host,
Password: system.Redis().Password, // no password set
DB: db, // use default DB
})
_, err = client.Ping().Result()
if err != nil {
os.Exit(0)
}
RedisManager = &RedisClient{client, client.Options().Addr, client.Options().Password, client.Options().DB}
}
func (r *RedisClient) Set(key string, value interface{}, expiration time.Duration) *redis.StatusCmd {
return r.Client.Set(key, value, expiration)
}
func (r *RedisClient) Get(key string) (string, error) {
return r.Client.Get(key).Result()
}
func (r *RedisClient) Keys(pattern string) ([]string, error) {
return r.Client.Keys(pattern).Result()
}
func (r *RedisClient) Delete(keys ...string) (int64, error) {
return r.Client.Del(keys...).Result()
}
/*
Your keys count must match the exist amount or not all keys exist
Exists in redis doesn't return what keys actually existed so this function is pretty basic
*/
func (r *RedisClient) Exist(keys ...string) bool {
test, _ := r.Client.Exists(keys...).Result()
keyCount := len(keys)
return test == int64(keyCount)
}
func (r *RedisClient) TTL(key string) (time.Duration, error) {
return r.Client.TTL(key).Result()
}