You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gocache/ristretto.go

40 lines
1020 B

2 years ago
package gocache
import (
"github.com/dgraph-io/ristretto"
)
// Ristretto https://github.com/dgraph-io/ristretto
type Ristretto struct {
2 years ago
db *ristretto.Cache // 驱动
2 years ago
numCounters int64 // 跟踪频率的键数 (10M)
maxCost int64 // 缓存的最大成本1GB
bufferItems int64 // 每个Get缓冲区的密钥数
}
// NewRistretto 实例化
func NewRistretto() *Ristretto {
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.
})
2 years ago
return &Ristretto{db: cache}
2 years ago
}
// Set 插入数据
func (r *Ristretto) Set(key string, value interface{}, cost int64) {
2 years ago
r.db.Set(key, value, cost)
r.db.Wait()
}
// Get 获取单个数据
func (r *Ristretto) Get(key string) (interface{}, bool) {
return r.db.Get(key)
2 years ago
}
// Del 删除数据
func (r *Ristretto) Del(key string) {
2 years ago
r.db.Del(key)
2 years ago
}