跳到主内容
版本:valkey_v0.x.x

Ristretto

Release Discord Test Security Linter

一个使用 dgraph-io/ristretto 的内存限制存储驱动程序。

注意:需要 Go 1.19 及以上版本

目录

函数签名

func New(config ...Config) Storage
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) Reset() error
func (s *Storage) Close() error
func (s *Storage) Conn() *ristretto.Cache

安装

Ristretto 在支持模块的最后 2 个 Go 版本上进行了测试。因此,如果你还没有这样做,请确保先初始化一个模块。

go mod init github.com/<user>/<repo>

然后安装 ristretto 实现

go get github.com/gofiber/storage/ristretto/v2

示例

导入存储包。

import "github.com/gofiber/storage/ristretto/v2"

你可以使用以下方式创建存储

// Initialize default config
store := ristretto.New()

// Initialize custom config
store := ristretto.New(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.
})

配置

type Config struct {
// NumCounters number of keys to track frequency of (10M).
NumCounters int64

// MaxCost maximum cost of cache (1GB).
MaxCost int64

// BufferItems number of keys per Get buffer.
BufferItems int64
}

默认配置

var ConfigDefault = Config{
NumCounters: 1e7,
MaxCost: 1 << 30,
BufferItems: 64,
DefaultCost: 1,
}