1 Star 0 Fork 0

iThings/core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
slot.go 1009 Bytes
一键复制 编辑 原始数据 按行查看 历史
杨磊 提交于 2024-04-19 00:16 . feat: 完善项目
package cache
import (
"context"
"gitee.com/i-Things/core/service/syssvr/internal/repo/relationDB"
"gitee.com/i-Things/share/domain/slot"
"github.com/dgraph-io/ristretto"
"time"
)
type Slot struct {
cache *ristretto.Cache
}
const (
expireTime = time.Minute * 10
)
func NewSlot() *Slot {
cache, _ := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, // number of keys to track frequency of (10M).
MaxCost: 1 << 30, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
})
return &Slot{
cache: cache,
}
}
func (c *Slot) Get(ctx context.Context, code string, subCode string) slot.Infos {
key := code + ":" + subCode
v, ok := c.cache.Get(key)
if ok {
return v.(slot.Infos)
}
list, err := relationDB.NewSlotInfoRepo(ctx).FindByFilter(ctx, relationDB.SlotInfoFilter{Code: code, SubCode: subCode}, nil)
if err != nil {
return nil
}
slots := relationDB.ToSlotsDo(list)
c.cache.SetWithTTL(key, slots, 1, expireTime)
return slots
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/i-Things/core.git
git@gitee.com:i-Things/core.git
i-Things
core
core
v0.1.6

搜索帮助

0d507c66 1850385 C8b1a773 1850385