2019-12-05 17:01:14 +08:00
|
|
|
|
package cache
|
|
|
|
|
|
|
2019-12-09 15:20:02 +08:00
|
|
|
|
import (
|
2023-04-16 09:17:06 +08:00
|
|
|
|
"encoding/gob"
|
2019-12-09 15:20:02 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-04-16 09:17:06 +08:00
|
|
|
|
func init() {
|
|
|
|
|
|
gob.Register(map[string]itemWithTTL{})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-12-05 17:01:14 +08:00
|
|
|
|
// Driver 键值缓存存储容器
|
|
|
|
|
|
type Driver interface {
|
2019-12-23 11:22:46 +08:00
|
|
|
|
// 设置值,ttl为过期时间,单位为秒
|
2025-04-20 17:31:25 +08:00
|
|
|
|
Set(key string, value any, ttl int) error
|
2019-12-23 11:22:46 +08:00
|
|
|
|
|
|
|
|
|
|
// 取值,并返回是否成功
|
2025-04-20 17:31:25 +08:00
|
|
|
|
Get(key string) (any, bool)
|
2019-12-23 11:22:46 +08:00
|
|
|
|
|
2019-12-09 15:20:02 +08:00
|
|
|
|
// 批量取值,返回成功取值的map即不存在的值
|
2025-04-20 17:31:25 +08:00
|
|
|
|
Gets(keys []string, prefix string) (map[string]any, []string)
|
2019-12-23 11:22:46 +08:00
|
|
|
|
|
|
|
|
|
|
// 批量设置值,所有的key都会加上prefix前缀
|
2025-04-20 17:31:25 +08:00
|
|
|
|
Sets(values map[string]any, prefix string) error
|
2019-12-23 11:22:46 +08:00
|
|
|
|
|
2025-04-20 17:31:25 +08:00
|
|
|
|
// Delete values by [Prefix + key]. If no ket is presented, all keys with given prefix will be deleted.
|
|
|
|
|
|
Delete(prefix string, keys ...string) error
|
2023-04-16 09:17:06 +08:00
|
|
|
|
|
|
|
|
|
|
// Save in-memory cache to disk
|
|
|
|
|
|
Persist(path string) error
|
|
|
|
|
|
|
|
|
|
|
|
// Restore cache from disk
|
|
|
|
|
|
Restore(path string) error
|
2019-12-09 15:20:02 +08:00
|
|
|
|
|
2025-04-20 17:31:25 +08:00
|
|
|
|
// Remove all entries
|
|
|
|
|
|
DeleteAll() error
|
2019-12-05 17:01:14 +08:00
|
|
|
|
}
|