/
redis.go
87 lines (74 loc) · 1.92 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
/*
* @Author: youlingdada youlingdada@163.com
* @Date: 2022-07-08 11:03:14
* @LastEditors: youlingdada youlingdada@163.com
* @LastEditTime: 2022-07-12 09:29:26
* @FilePath: \street-stall\utils\redis.go
* @Description: redis 工具类
* QQ:3367758294
* Copyright (c) 2022 by Youlingdada, All Rights Reserved.
*/
package redis_utils
import (
"fmt"
"log"
"github.com/gomodule/redigo/redis"
"github.com/youlingdada/street-stall/config"
)
var Conn redis.Conn
const (
ReplyOk = "OK"
)
func init() {
app := config.GlobalConfig
address := fmt.Sprintf("%s:%d", app.Redis.Host, app.Redis.Port)
conn, err := redis.Dial("tcp", address, redis.DialDatabase(int(app.Redis.Db)), redis.DialUsername(app.Redis.Username), redis.DialPassword(app.Redis.Password))
if err != nil {
log.Printf("redis conn failure,err: %s", err)
log.Panic("Application stop")
}
Conn = conn
}
type redisUtil struct{}
var RedisUtils redisUtil
/**
* @description: redis设置值
* @param {string} key 键
* @param {interface{}} value 值
* @return {*}
*/
func (ru redisUtil) Set(key string, value interface{}) error {
_, err := Conn.Do("Set", key, value)
return err
}
/**
* @description: 设置值,并设置过期时间
* @param {string} key 键
* @param {interface{}} value 值
* @param {int} expTime 过期时间 (s)
* @return {*}
*/
func (ru redisUtil) Setex(key string, value interface{}, exp int) error {
_, err := Conn.Do("Setex", key, exp, value)
return err
}
/**
* @description: redis 获取值
* @param {string} key 键
* @return {*} 返回的值
*/
func (ru redisUtil) Get(key string) (interface{}, error) {
return Conn.Do("Get", key)
}
/**
* @description: 获取指定键的过期时间
* @param {string} key 键
* @return {*} 过期时间
*/
func (ru redisUtil) Ttl(key string) (int64, error) {
return redis.Int64(Conn.Do("Ttl", key))
}
// 删除一个指定的键值对
func (ru redisUtil) Del(key string) error {
return nil
}