文档:https://pkg.go.dev/github.com/patrickmn/go-cache

Constants

  1. const (
  2. NoExpiration time.Duration = -1 // 不过期
  3. DefaultExpiration time.Duration = 0 // 过期时间为cache的过期时间
  4. )

type Cache

  1. // 默认defaultExpiration过期,每间隔cleanupInterval开始清理的cache
  2. func New(defaultExpiration, cleanupInterval time.Duration) *Cache
  3. // Add 仅当key不存在时才添加
  4. func (c Cache) Add(k string, x interface{}, d time.Duration) error
  5. // Set 如果key存在会直接覆盖
  6. func (c Cache) Set(k string, x interface{}, d time.Duration)
  7. func (c Cache) SetDefault(k string, x interface{})
  8. func (c Cache) Decrement // 自减
  9. func (c Cache) Increment // 自增
  10. func (c Cache) Delete(k string)
  11. func (c Cache) DeleteExpired() // 主动清理过期的条目
  12. func (c Cache) Flush() // 删除所有
  13. func (c Cache) Get(k string) (interface{}, bool)
  14. func (c Cache) GetWithExpiration(k string) (interface{}, time.Time, bool)
  15. func (c Cache) ItemCount() int // 返回cache中的key数,可能包含过期但未清理的key
  16. func (c Cache) Load(r io.Reader) error
  17. func (c Cache) LoadFile(fname string) error
  18. func (c Cache) Save(w io.Writer) (err error)
  19. func (c Cache) SaveFile(fname string) error
  20. // 仅当key存在,且没过期才有效
  21. func (c Cache) Replace(k string, x interface{}, d time.Duration) error