caches

package
v0.0.0-...-b459b6a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NeverDie = 0 // 是一个常量,永不过期
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache 结构体:封装缓存底层结构

func NewCache

func NewCache() *Cache

NewCache 返回Cache对象

func NewCacheWith

func NewCacheWith(options Options) *Cache

NewCacheWith 返回一个指定配置的缓存对象

func (*Cache) AutoDump

func (c *Cache) AutoDump()

AutoDump 开启定时任务去持久化缓存

func (*Cache) AutoGc

func (c *Cache) AutoGc()

AutoGc 会开启一个定时GC异步任务

func (*Cache) Delete

func (c *Cache) Delete(key string)

Delete 删除指定key的键值对数据

func (*Cache) Get

func (c *Cache) Get(key string) ([]byte, bool)

Get 返回指定key的value,找不到则返回false

func (*Cache) Set

func (c *Cache) Set(key string, value []byte) error

Set 添加一个键值对到缓存中,不设定ttl,也就意味着数据不会过期 返回error是nil说明添加成功,否则就是添加失败,可能是触发了写满保护机制,拒绝写入数据

func (Cache) SetWithTTL

func (c Cache) SetWithTTL(key string, value []byte, ttl int64) error

SetWithTTL 添加一个键值对到缓存中,使用给定的ttl去设定过期时间 返回error是nil说明添加成功,否则就是添加失败,可能是触发了写满保护机制,拒绝写入数据

func (*Cache) Status

func (c *Cache) Status() Status

Status 返回缓存信息

type Options

type Options struct {
	MaxEntrySize int64  // 单位GB,写满保护阈值,当缓存中的键值对占用空间达到该值就触发写满保护
	MaxGcCount   int    // 自动淘汰机制阈值,当清理的数据达到该值后就会停止清理
	GcDuration   int64  // 自动淘汰机制时间间隔,每隔固定的GcDuraton时间就会进行一次自动淘汰
	DumpFile     string // 持久化文件的路径
	DumpDuration int64  // 持久化时间间隔,单位是分钟
}

Options 是一些选项的结构体

func DefaultOptions

func DefaultOptions() Options

DefaultOptions 返回一个默认的选项设置对象

type Status

type Status struct {
	Count     int   `json:"count"`     // 记录缓存中的数据项个数
	KeySize   int64 `json:"keySize"`   // 记录key占用的空间大小
	ValueSize int64 `json:"valueSize"` // 记录value占用的空间大小
}

Status 是一个代表缓存信息的结构体 因为需要被序列化为JSON字符串并通过网络传输,所以这里使用JSON标签

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL