1 Star 0 Fork 0

kzangv / gsf-ai-agent

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cache.go 775 Bytes
一键复制 编辑 原始数据 按行查看 历史
kzangv 提交于 2024-01-13 12:43 . fixed
package utils
import (
"sync"
"time"
)
type KeyValueCache interface {
Set(k, v string, tm *time.Time) error
Get(k string) (string, bool)
Del(k string) error
}
type MapCacheItem struct {
Value string
Expire *time.Time
}
type MapCache struct {
val sync.Map
}
func (m *MapCache) Set(k, v string, tm *time.Time) error {
m.val.Store(k, &MapCacheItem{
Value: v,
Expire: tm,
})
return nil
}
func (m *MapCache) Get(k string) (string, bool) {
if vr, ok := m.val.Load(k); ok {
if v, ok := vr.(*MapCacheItem); ok {
if v.Expire == nil || v.Expire.After(time.Now()) {
return v.Value, true
}
}
}
return "", false
}
func (m *MapCache) Del(k string) error {
m.val.LoadAndDelete(k)
return nil
}
func NewMapCache() *MapCache {
return &MapCache{}
}
Go
1
https://gitee.com/kzangv/gsf-ai-agent.git
git@gitee.com:kzangv/gsf-ai-agent.git
kzangv
gsf-ai-agent
gsf-ai-agent
v0.0.7

搜索帮助