memutil

package
v0.0.0-...-9c21ab8 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	KeyError = errors.New("key not exist")
)

Functions

This section is empty.

Types

type LRU

type LRU struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewLRUCache

func NewLRUCache(capacity int) *LRU

maxSize: 缓存的最大数量

func (*LRU) Get

func (lru *LRU) Get(key string) (interface{}, error)

func (*LRU) Set

func (lru *LRU) Set(key string, value interface{})

type TTL

type TTL struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewTTLCache

func NewTTLCache(aliveSeconds int64, capacity int) *TTL

aliveSeconds: 缓存有效期,capacity: 缓存容量,注意合理估计capacity为未过期元素的最大数量

func (*TTL) Get

func (ttl *TTL) Get(key string) (interface{}, error)

func (*TTL) Set

func (ttl *TTL) Set(key string, value interface{})

当缓存容量超出指定值时,会主动扫描一遍过期键 但是这并不能保证缓存容量减少到指定范围之内

Jump to

Keyboard shortcuts

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