cache

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2021 License: MIT Imports: 5 Imported by: 0

README

Cache缓存器

适用于内存缓存的简单工具类

Usage

缓存对象并取出

c := cache.New()

// 保存两个对象
c.Set("num", 123)
c.Set("str", "test")

// 取出对象
if value, ok := c.Get("num"); ok {
    num := value.(int)
    println(num)
}
if value, ok := c.Get("str"); ok {
    str := value.(string)
    println(str)
}

// 遍历对象
c.Range(func(key string, value interface{})bool{
    if key == "num" {
        num := value.(int)
        println(num)
    }
    return true  // 返回true表示继续遍历
})

// 移除一个缓存对象
c.Delete("num")
// 获取缓存数量
count := c.Len()
// 清空缓存
c.Flush()

全局缓存

// 初始化全局缓存
options := &cache.Options{}
cache.Init(options)

// 保存一个对象
cache.Set("num", 123)

// 取出对象
if value, ok := cache.Get("num"); ok {
    num := value.(int)
    println(num)
}

// 移除一个缓存对象
cache.Delete("num")

// 获取全局缓存
global := cache.Global()

保存一个对象并设置过期时间

c := cache.New()

// 保存一个3秒过期的对象
expiration := time.Second * 3
c.SetWithExpiration("num", 123, expiration)

设置默认过期时间,和自动清理时长

options := &cache.Options{
    DefaultExpiration: time.Second * 3,  // 默认3秒过期
    CleanInterval:     time.Minute,      // 每分钟清理一次
}

c := cache.NewWithOptions(options)

同时支持LRU缓存机制

options := &cache.Options{
    Capacity: 2,  // Capacity不为0时自动开启LRU,LRU缓存的容量为2
}

c := cache.NewWithOptions(options)
已知问题

使用LRU Cache时,如果设置了自动清理(options.CleanInterval不为0),可能有潜在的性能问题

Cache的自动清理会调用 ClearExpired 方法,该方法执行时会打开写锁,直到清理完成

跑性能测试结果如下

待删除的对象数量 一次CleanUp耗时
1k 697.929µs
1w 4.749823ms
10w 53.20541ms
100w 806.008161ms

待清除的对象数量超过1w时,一次CleanUp阻塞可能会对性能有较大影响,所以在代码中做了限制,一次最多清理1w个

Documentation

Index

Constants

View Source
const (
	// NoExpiration 永不过期
	NoExpiration time.Duration = 0
	// DefaultCleanInterval 默认的清空缓存时长
	DefaultCleanInterval time.Duration = time.Minute
)

Variables

This section is empty.

Functions

func Delete

func Delete(key string)

Delete 删除一个缓存对象

func Get

func Get(key string) (value interface{}, found bool)

Get 获取一个缓存对象

func Init

func Init(options *Options)

Init 初始化全局缓存

func Set

func Set(key string, val interface{})

Set 缓存一个对象

func SetWithExpiration

func SetWithExpiration(key string, val interface{}, expiration time.Duration)

SetWithExpiration 缓存一个对象,并设置过期时间

Types

type Cache

type Cache interface {
	// Set 缓存一个对象
	Set(key string, val interface{})
	// SetWithExpiration 缓存一个对象,并设置过期时间
	SetWithExpiration(key string, val interface{}, expiration time.Duration)
	// Get 获取一个缓存对象
	Get(key string) (value interface{}, found bool)
	// Delete 删除一个缓存对象
	Delete(key string)
	// 实现ItemMap接口的所有方法
	ItemMap
}

Cache 缓存器

func Global

func Global() Cache

Global 获取全局缓存

func New

func New() Cache

New 新建缓存器

func NewWithOptions

func NewWithOptions(options *Options) Cache

NewWithOptions 新建缓存器

type DeletedCallback

type DeletedCallback func(string, interface{})

DeletedCallback 缓存对象被删除时的回调函数

type Item

type Item struct {
	Value       interface{}
	ExpiredTime *time.Time
}

Item 缓存项

func NewItem

func NewItem(val interface{}, expiration time.Duration) *Item

func (*Item) IsExpired

func (i *Item) IsExpired() bool

IsExpired 对象是否过期

type ItemMap

type ItemMap interface {
	// GetItem 获取缓存项
	GetItem(key string) (*Item, bool)
	// AddItem 添加缓存项
	AddItem(key string, val *Item)
	// RemoveItem 移除缓存项
	RemoveItem(key string)
	// Flush 清空缓存
	Flush()
	// Len 返回缓存对象数量
	Len() int
	// Range 遍历缓存对象,接受一个op函数,函数参数分别是key/value
	// 返回true表示继续遍历,返回false表示停止遍历
	Range(op func(string, interface{}) bool)
	// ClearExpired 清空过期对象
	ClearExpired()
}

ItemMap

type Options

type Options struct {
	DefaultExpiration time.Duration
	CleanInterval     time.Duration
	Capacity          int
	DeletedCallback   DeletedCallback
}

Options 缓存选项 @DefaultExpiration 默认的过期时长 @CleanInterval 自动清理时间间隔 @Capacity 容量,设置后将启用LRU @DeletedCallback 缓存对象被删除时的回调函数

Jump to

Keyboard shortcuts

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