[优化] 优化缓存

This commit is contained in:
PandaX
2023-10-17 14:39:01 +08:00
parent 5db1db8289
commit 10b98df33c
17 changed files with 158 additions and 85 deletions

33
pkg/cache/device_etoken.go vendored Normal file
View File

@@ -0,0 +1,33 @@
package cache
import (
"context"
"github.com/PandaXGO/PandaKit/rediscli"
"time"
)
var RedisDb *rediscli.RedisDB
// SetDeviceEtoken key 是设备的时候为token 是子设备的时候为设备编码
func SetDeviceEtoken(key string, value any, duration time.Duration) error {
return RedisDb.Set(key, value, duration)
}
// GetDeviceEtoken value 是参数指针
func GetDeviceEtoken(key string, value interface{}) error {
return RedisDb.Get(key, value)
}
// DelDeviceEtoken 删除指定的key
func DelDeviceEtoken(key string) error {
return RedisDb.Del(context.Background(), key).Err()
}
func ExistsDeviceEtoken(key string) bool {
exists, _ := RedisDb.Exists(RedisDb.Context(), key).Result()
if exists == 1 {
return true
} else {
return false
}
}

8
pkg/cache/panel.go vendored Normal file
View File

@@ -0,0 +1,8 @@
package cache
import (
"github.com/PandaXGO/PandaKit/cache"
"time"
)
var PanelCache = cache.NewTimedCache(cache.NoExpiration, 600*time.Second)

20
pkg/cache/product_rule.go vendored Normal file
View File

@@ -0,0 +1,20 @@
package cache
import (
"github.com/PandaXGO/PandaKit/cache"
"time"
)
var ProductCache = cache.NewTimedCache(cache.NoExpiration, 24*time.Hour)
func ComputeIfAbsentProductRule(key string, fun func(any) (any, error)) (any, error) {
return ProductCache.ComputeIfAbsent(key, fun)
}
func DelProductRule(key string) {
ProductCache.Delete(key)
}
func PutProductRule(key string, data any) {
ProductCache.Put(key, data)
}