cache

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 12, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// 没有过期时间标志 -不过期
	NoExpiration time.Duration = -1

	// 默认的过期时间
	DefaultExpiration time.Duration = 0
)

Variables

This section is empty.

Functions

func Add

func Add(k string, v interface{}, second int) (err error)

func Get

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

获取缓存数据

func Init

func Init()

初始化缓存

func Set

func Set(k string, v interface{}, second int)

设置数据

Types

type Cache

type Cache struct {
	Items map[string]Item // 缓存数据项存储在 map 中

	File string
	// contains filtered or unexported fields
}
var Obj Cache

func NewCache

func NewCache(defaultExpiration, gcInterval time.Duration, file string) *Cache

创建一个缓存系统

func (*Cache) Add

func (c *Cache) Add(k string, v interface{}, d time.Duration) error

添加数据项,如果数据项已经存在,则返回错误

func (*Cache) Count

func (c *Cache) Count() int

返回缓存数据项的数量

func (*Cache) Delete

func (c *Cache) Delete(k string)

删除一个数据项

func (*Cache) DeleteExpired

func (c *Cache) DeleteExpired()

遍历删除过期数据项

func (*Cache) Flush

func (c *Cache) Flush()

清空缓存

func (*Cache) Get

func (c *Cache) Get(k string) (interface{}, bool)

获取数据项

func (*Cache) Load

func (c *Cache) Load(r io.Reader) error

从 io.Reader 中读取数据项

func (*Cache) LoadFile

func (c *Cache) LoadFile(file string) error

从文件中加载缓存数据项

func (*Cache) Replace

func (c *Cache) Replace(k string, v interface{}, d time.Duration) error

替换一个存在的数据项

func (*Cache) Save

func (c *Cache) Save(w io.Writer) (err error)

将缓存数据项写入到 io.Writer 中

func (*Cache) SaveToFile

func (c *Cache) SaveToFile(file string) error

保存数据项到文件中

func (*Cache) Set

func (c *Cache) Set(k string, v interface{}, d time.Duration)

设置缓存数据项,如果数据项存在则覆盖

func (*Cache) StopGc

func (c *Cache) StopGc()

停止过期缓存清理

type Config

type Config struct {
	//是否启用
	Enable bool `toml:"enable"`
	//缓存默认过期时间(秒) ,传递-1时取此值
	Expiration int32 `toml:"expiration"`
	//是否实时输出到缓存文件
	RealtimeSave bool `toml:"realtime_save"`
	//缓存文件保存周期(秒),realtime_save=false 才会生效
	SavefileCycle int32 `toml:"savefile_cycle"`
	//缓存文件保存路径
	SavefilePath string `toml:"savefile_path"`
}

type Item

type Item struct {
	Object     interface{} // 真正的数据项
	Expiration int64       // 生存时间
}

func (Item) Expired

func (item Item) Expired() bool

判断数据项是否已经过期

Jump to

Keyboard shortcuts

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