mirror of
https://gitee.com/xiangheng/x_admin.git
synced 2025-10-06 00:27:00 +08:00
68 lines
1.2 KiB
Go
68 lines
1.2 KiB
Go
package util
|
|
|
|
import (
|
|
"errors"
|
|
"strconv"
|
|
)
|
|
|
|
// var CacheUtil = toolsUtil{}
|
|
|
|
type CacheUtil struct {
|
|
Name string
|
|
}
|
|
|
|
// 设置缓存
|
|
func (c CacheUtil) SetCache(key interface{}, obj interface{}) bool {
|
|
str, e := ToolsUtil.ObjToJson(obj)
|
|
if e != nil {
|
|
return false
|
|
}
|
|
var cacheKey string
|
|
switch k := key.(type) {
|
|
case int:
|
|
cacheKey = strconv.Itoa(k)
|
|
case string:
|
|
cacheKey = k
|
|
default:
|
|
return false
|
|
}
|
|
return RedisUtil.HSet(c.Name, cacheKey, str, 3600)
|
|
}
|
|
|
|
// 获取缓存
|
|
func (c CacheUtil) GetCache(key interface{}, obj interface{}) error {
|
|
var cacheKey string
|
|
switch k := key.(type) {
|
|
case int:
|
|
cacheKey = strconv.Itoa(k)
|
|
case string:
|
|
cacheKey = k
|
|
default:
|
|
return errors.New("缓存key无效")
|
|
}
|
|
str := RedisUtil.HGet(c.Name, cacheKey)
|
|
if str == "" {
|
|
return errors.New("获取缓存失败")
|
|
}
|
|
err := ToolsUtil.JsonToObj(str, &obj)
|
|
|
|
if err != nil {
|
|
return errors.New("解析缓存失败")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 删除缓存
|
|
func (c CacheUtil) RemoveCache(key interface{}) bool {
|
|
var cacheKey string
|
|
switch k := key.(type) {
|
|
case int:
|
|
cacheKey = strconv.Itoa(k)
|
|
case string:
|
|
cacheKey = k
|
|
default:
|
|
return false
|
|
}
|
|
return RedisUtil.HDel(c.Name, cacheKey)
|
|
}
|