Redis查询缓存与延时双删的实际应用(Golang)
有个小项目,需要弄个树型单词分类,虽然客户那边访问量不大,崩是不可能崩的。但是数据量大了以后,每次打开这个页面都要等个一两秒,属实有点难受。就想着用缓存来装这颗树。
Json数据的结构
"title": "大学",
"key": 1,
"children": [
{
"title": "四级阅读",
"key": 2,
"children": [
{
"title": "8901A",
"key": 3,
"children": null
Golang结构体
type CategoryTree struct {
Title string `json:"title"`
Key int64 `json:"key"`
Children []CategoryTree `json:"children"`
}
查询时使用缓存,加快访问速度
func GetCategorys() ([]models.CategoryTree, bool) {
var tree []models.CategoryTree
_, jsonStr := redis.GetValueByKey("CategoryTree")
if len(jsonStr) > 0 {
err := json.Unmarshal([]byte(jsonStr), &tree)
if err == nil {
return tree, true
}
}
jsonTree, _ := json.Marshal(tree)
redis.SetValueByKey("CategoryTree", string(jsonTree), 60*60*720)
}
更新数据时,使用延时双删,清除旧的缓存
func SetCategory(categoryName string, fatherId int64, lv int) (string, bool) {
redis.SetValueByKey("CategoryTree", "", 1)
defer func() {
go func() {
time.Sleep(time.Second * 3)
redis.SetValueByKey("CategoryTree", "", 1)
}()
}()
if err != nil {
utils.Loger.Println(err)
return "插入失败", false
}
return "插入成功", true
}
Redis数据插入函数
func SetValueByKey(key string, value string, time int) bool {
_, err := conn.Do("SET", key, value, "EX", time)
if err != nil {
utils.Loger.Println(err)
return false
}
return true
}