-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
84 lines (69 loc) · 1.98 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
package redis
import (
"github.com/go-redis/redis"
"time"
)
type RedisTemplate struct {
Client *redis.Client
}
//Create new Redis client
func NewRedisClient(url string, password string, db int) *RedisTemplate {
return &RedisTemplate{
Client: redis.NewClient(&redis.Options{
Addr: url,
Password: password,
DB: db,
}),
}
}
// Set sets the value of a key
func (s *RedisTemplate) Set(key string, value string) error {
err := s.Client.Set(key, value, 0).Err()
return err
}
func (s *RedisTemplate) Get(key string) (string, error) {
val, err := s.Client.Get(key).Result()
return val, err
}
// Del deletes one or more keys.
func (s *RedisTemplate) Delete(key ...string) error {
err := s.Client.Del(key...).Err()
return err
}
// Exists determines if a key exists.
func (s *RedisTemplate) Exists(key string) (int64, error) {
return s.Client.Exists(key).Result()
}
// Expire sets a timeout on a key.
func (s *RedisTemplate) Expire(key string, duration int64) error {
return s.Client.Expire(key, time.Duration(duration)).Err()
}
// TTL gets the remaining time to live of a key.
func (s *RedisTemplate) TTL(key string) (time.Duration, error) {
return s.Client.TTL(key).Result()
}
// Incr increments the value of a key.
func (s *RedisTemplate) Incr(key string) (int64, error) {
return s.Client.Incr(key).Result()
}
// Decr decrements the value of a key.
func (s *RedisTemplate) Decr(key string) (int64, error) {
return s.Client.Decr(key).Result()
}
// HGet gets the value of a field in a hash.
func (s *RedisTemplate) HGet(key string, field string) (string, error) {
return s.Client.HGet(key, field).Result()
}
// HSet sets the value of a field in a hash.
func (s *RedisTemplate) HSet(key string, field string, value string) error {
return s.Client.HSet(key, field, value).Err()
}
//ping function
func (s *RedisTemplate) Ping() (string, error) {
return s.Client.Ping().Result()
}
//close connection
func (s *RedisTemplate) CloseConnection() error {
err := s.Client.Close()
return err
}