forked from neeker/sqlrestful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
150 lines (131 loc) · 3.74 KB
/
db.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
/*********************************************
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑 永无BUG
心外无法 法外无心
三宝弟子 飞猪宏愿
*********************************************/
package main
import (
"encoding/json"
"fmt"
"github.com/go-redis/redis"
"log"
)
// DatabaseConfig - 数据库配置
type DatabaseConfig struct {
Driver string // 驱动名
Dsn string //连接方式
Redis string //Redis连接
redisClient *redis.Client //Redis客户端连接
}
// ConnectToRedis - 连接到Redis服务器
func (c *DatabaseConfig) ConnectToRedis() error {
redisOpts, err := redis.ParseURL(c.Redis)
if err != nil {
return fmt.Errorf("invalid redis url %s", c.Redis)
}
c.redisClient = redis.NewClient(redisOpts)
return nil
}
// IsDatabaseEnabled - 数据库是否已启用
func (c *DatabaseConfig) IsDatabaseEnabled() bool {
return c.Driver != "" && c.Dsn != ""
}
// IsRedisEnabled - 是否已启用Redis缓存
func (c *DatabaseConfig) IsRedisEnabled() bool {
return c.Redis != "" && c.redisClient != nil
}
// BuildCacheKey - 获取缓存键值
func (c *DatabaseConfig) BuildCacheKey(input map[string]interface{}) string {
if len(input) == 0 {
return ""
}
return fmt.Sprintf("%v", input)
}
// PutCacheData - Put cache data
func (c *DatabaseConfig) PutCacheData(cacheNames []string, cacheKey string, val interface{}) (bool, error) {
if !c.IsRedisEnabled() {
if *flagDebug > 0 {
fmt.Printf("put %s=%v to cache %v error: redis is disabled\n", cacheKey, val, cacheNames)
}
return false, nil
}
if len(cacheNames) == 0 || len(cacheKey) == 0 {
return false, nil
}
var (
ret bool
err error
)
ret = false
for _, k := range cacheNames {
jsonData, _ := json.Marshal(val)
ret, err = c.redisClient.HSet(k, cacheKey, string(jsonData)).Result()
if err != nil {
if *flagDebug > 1 {
log.Printf("put %s to cache %s error: %v\n", cacheKey, k, err)
}
return false, err
}
if *flagDebug > 2 {
log.Printf("put %s to cache %s: %s\n", cacheKey, k, jsonData)
}
}
return ret, nil
}
// GetCacheData Get cache data
func (c *DatabaseConfig) GetCacheData(cacheNames []string, cacheKey string) (interface{}, error) {
if !c.IsRedisEnabled() {
if *flagDebug > 0 {
fmt.Printf("get %s from cache %v error: redis is disabled\n", cacheKey, cacheNames)
}
return nil, nil
}
if len(cacheNames) == 0 || len(cacheKey) == 0 {
return nil, nil
}
for _, k := range cacheNames {
if c.redisClient.HExists(k, cacheKey).Val() {
jsonData, _ := c.redisClient.HGet(k, cacheKey).Result()
var outData interface{}
err := json.Unmarshal([]byte(jsonData), &outData)
if err != nil {
if *flagDebug > 1 {
log.Printf("get %s from cache %s error: %v\n", cacheKey, k, err)
}
return nil, err
}
if *flagDebug > 2 {
log.Printf("get %s from %s cache data: %s\n", cacheKey, k, jsonData)
}
return outData, nil
}
}
return nil, nil
}
// ClearCaches - clear caches
func (c *DatabaseConfig) ClearCaches(cacheNames []string) {
if len(cacheNames) > 0 {
for _, k := range cacheNames {
c.redisClient.Del(k)
}
}
}