2021-12-06 16:31:44 +08:00
|
|
|
|
# rds_cache_go
|
2021-05-07 09:05:20 +08:00
|
|
|
|
|
2021-12-06 16:31:13 +08:00
|
|
|
|
## 说明
|
2021-05-07 09:05:20 +08:00
|
|
|
|
rds_cache_go是基于go-redis的缓存工具包
|
|
|
|
|
|
2021-12-06 16:31:13 +08:00
|
|
|
|
## 安装
|
2021-05-07 09:05:20 +08:00
|
|
|
|
go get -u github.com/cowardmrx/rds_cache_go
|
|
|
|
|
|
|
|
|
|
## 使用
|
|
|
|
|
```go
|
2021-12-06 16:20:44 +08:00
|
|
|
|
var caches = NewCache(WithHost("192.168.0.151"), WithPort("6379"), WithDB(13))
|
2021-05-07 09:05:20 +08:00
|
|
|
|
|
|
|
|
|
// PUT
|
|
|
|
|
err := caches.Put("key1", "va", 1*time.Minute)
|
|
|
|
|
|
|
|
|
|
// PUT json
|
|
|
|
|
amap := map[string]interface{}{
|
|
|
|
|
"data" : "data1"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
amapJson,_ := json.Marshal(amap)
|
|
|
|
|
|
|
|
|
|
err := caches.Put("key2",amapJson,1 * time.Minute)
|
|
|
|
|
|
|
|
|
|
// Exist
|
|
|
|
|
exist := caches.Exist("key2")
|
|
|
|
|
|
2021-12-06 16:32:51 +08:00
|
|
|
|
// Get
|
|
|
|
|
result := caches.Get("key2")
|
|
|
|
|
|
2021-05-07 09:05:20 +08:00
|
|
|
|
// Delete
|
|
|
|
|
result,err := caches.Delete("key2")
|
|
|
|
|
|
|
|
|
|
// Delete More
|
|
|
|
|
result,err = caches.Delete("key2","key3","key4")
|
2021-12-14 17:01:43 +08:00
|
|
|
|
|
|
|
|
|
// already redis connect client
|
|
|
|
|
rdsClient := redis.NewClient(&redis.Options{
|
|
|
|
|
Addr: fmt.Sprintf("%s:%s", "192.168.0.151", "6379"),
|
|
|
|
|
Password: "",
|
|
|
|
|
DB: 15,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// WithRedisClient() 使用已有的redis链接客户端
|
|
|
|
|
// WithOriginDB() 是否使用已经的客户端的数据库 true - 使用已有的客户端链接数据库 | false - 使用WithDB()中的数据库,如果为空默认使用0库
|
|
|
|
|
// WithDB() 指定数据库,如果 WithOriginDB() 为false,WithDB()未指定的话默认使用0号库
|
|
|
|
|
|
|
|
|
|
caches := NewCache(WithRedisClient(rdsClient), WithOriginDB(true), WithDB(11))
|
|
|
|
|
|
|
|
|
|
|
2021-05-07 09:05:20 +08:00
|
|
|
|
```
|