文档:https://pkg.go.dev/github.com/patrickmn/go-cache
Constants
const (
NoExpiration time.Duration = -1 // 不过期
DefaultExpiration time.Duration = 0 // 过期时间为cache的过期时间
)
type Cache
// 默认defaultExpiration过期,每间隔cleanupInterval开始清理的cache
func New(defaultExpiration, cleanupInterval time.Duration) *Cache
// Add 仅当key不存在时才添加
func (c Cache) Add(k string, x interface{}, d time.Duration) error
// Set 如果key存在会直接覆盖
func (c Cache) Set(k string, x interface{}, d time.Duration)
func (c Cache) SetDefault(k string, x interface{})
func (c Cache) Decrement // 自减
func (c Cache) Increment // 自增
func (c Cache) Delete(k string)
func (c Cache) DeleteExpired() // 主动清理过期的条目
func (c Cache) Flush() // 删除所有
func (c Cache) Get(k string) (interface{}, bool)
func (c Cache) GetWithExpiration(k string) (interface{}, time.Time, bool)
func (c Cache) ItemCount() int // 返回cache中的key数,可能包含过期但未清理的key
func (c Cache) Load(r io.Reader) error
func (c Cache) LoadFile(fname string) error
func (c Cache) Save(w io.Writer) (err error)
func (c Cache) SaveFile(fname string) error
// 仅当key存在,且没过期才有效
func (c Cache) Replace(k string, x interface{}, d time.Duration) error