cache

package
v0.0.0-...-d69d334 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2021 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FileCachePath           = "runtime/cache" // 缓存目录
	FileCacheFileSuffix     = ".gob"          // 缓存文件后缀
	FileCacheDirectoryLevel = 1               // 缓存目录层级
	FileCacheExpire         time.Duration     // 缓存过期时间
)
View Source
var (
	// 默认回收过期缓存时间,默认一分钟
	DefaultEvery int = 60
)
View Source
var (
	DefaultKey = "redisCache"
)

Functions

func FileGetContents

func FileGetContents(filename string) (data []byte, e error)

获取文件内容

func FilePutContents

func FilePutContents(filename string, content []byte) error

写文件,不存在创建

func GetBool

func GetBool(v interface{}) bool

获取Bool类型

func GetFloat64

func GetFloat64(v interface{}) float64

获取Float64类型

func GetInt

func GetInt(v interface{}) int

获取Int类型

func GetInt64

func GetInt64(v interface{}) int64

获取Int64类型

func GetString

func GetString(v interface{}) string

获取字符串类型

func GobDecode

func GobDecode(data []byte, to *FileItem) error

GobDecode解码

func GobEncode

func GobEncode(data interface{}) ([]byte, error)

GobEncode编码

func Register

func Register(name string, adapter Instance)

注册一个新的适配器

Types

type Cache

type Cache interface {
	// 获取缓存
	Get(key string) interface{}
	// 获取多个缓存
	GetMulti(keys []string) []interface{}
	// 设置缓存和有效期
	Put(key string, val interface{}, timeout time.Duration) error
	// 删除一个缓存
	Delete(key string) error
	// 自增一个值
	Incr(key string) error
	// 自减一个值
	Decr(key string) error
	// 检查key是否存在
	IsExist(key string) bool
	// 清除所有缓存
	ClearAll() error
	// 启动并回收
	StartAndGC(config string) error
}

缓存接口

func NewCache

func NewCache(adapterName, config string) (adapter Cache, err error)

通过适配器名称创建一个新的缓存驱动,配置通过json字符串格式传入,并启动gc

func NewFileCache

func NewFileCache() Cache

返回新的文件缓存驱动

func NewMemoryCache

func NewMemoryCache() Cache

返回新的缓存

func NewRedisCache

func NewRedisCache() Cache

type FileCache

type FileCache struct {
	CachePath      string // 缓存目录
	FileSuffix     string // 缓存文件后缀
	DirectoryLevel int    // 缓存目录层级
	CacheExpire    int    // 缓存过期时间
}

func (*FileCache) ClearAll

func (fc *FileCache) ClearAll() error

清除所有缓存

func (*FileCache) Decr

func (fc *FileCache) Decr(key string) error

自减一个值

func (*FileCache) Delete

func (fc *FileCache) Delete(key string) error

删除一个缓存

func (*FileCache) Get

func (fc *FileCache) Get(key string) interface{}

获取一个缓存

func (*FileCache) GetMulti

func (fc *FileCache) GetMulti(keys []string) []interface{}

获取多个缓存

func (*FileCache) Incr

func (fc *FileCache) Incr(key string) error

自增一个值

func (*FileCache) IsExist

func (fc *FileCache) IsExist(key string) bool

检查缓存是否存在

func (*FileCache) Put

func (fc *FileCache) Put(key string, val interface{}, timeout time.Duration) error

设置一个缓存

func (*FileCache) StartAndGC

func (fc *FileCache) StartAndGC(config string) error

启动

type FileItem

type FileItem struct {
	Val        interface{} // 缓存内容
	LastAccess time.Time   // 最后访问时间
	Expire     time.Time   // 缓存有效期
}

type Instance

type Instance func() Cache

实例是一个函数,创建一个新的缓存实例

type MemoryCache

type MemoryCache struct {
	sync.RWMutex //读写锁

	Every int
	// contains filtered or unexported fields
}

缓冲驱动结构

func (*MemoryCache) ClearAll

func (bc *MemoryCache) ClearAll() error

清除所有缓存

func (*MemoryCache) Decr

func (bc *MemoryCache) Decr(key string) error

func (*MemoryCache) Delete

func (bc *MemoryCache) Delete(name string) error

删除一个缓存

func (*MemoryCache) Get

func (bc *MemoryCache) Get(name string) interface{}

获取一个缓存

func (*MemoryCache) GetMulti

func (bc *MemoryCache) GetMulti(names []string) []interface{}

获取多个缓存

func (*MemoryCache) Incr

func (bc *MemoryCache) Incr(key string) error

自增 支持int int32 int66 uint uint32 unit64

func (*MemoryCache) IsExist

func (bc *MemoryCache) IsExist(name string) bool

检查是否存在缓存

func (*MemoryCache) Put

func (bc *MemoryCache) Put(name string, value interface{}, ttr time.Duration) error

设置一个缓存 如果ttr = 0 永久缓存

func (*MemoryCache) StartAndGC

func (bc *MemoryCache) StartAndGC(config string) error

启动

type MemoryItem

type MemoryItem struct {
	// contains filtered or unexported fields
}

缓存item结构

type RedisCache

type RedisCache struct {
	// contains filtered or unexported fields
}

func (*RedisCache) ClearAll

func (rc *RedisCache) ClearAll() error

func (*RedisCache) Decr

func (rc *RedisCache) Decr(key string) error

func (*RedisCache) Delete

func (rc *RedisCache) Delete(key string) error

func (*RedisCache) Get

func (rc *RedisCache) Get(key string) interface{}

func (*RedisCache) GetMulti

func (rc *RedisCache) GetMulti(keys []string) []interface{}

func (*RedisCache) Incr

func (rc *RedisCache) Incr(key string) error

func (*RedisCache) IsExist

func (rc *RedisCache) IsExist(key string) bool

func (*RedisCache) Put

func (rc *RedisCache) Put(key string, val interface{}, timeout time.Duration) error

func (*RedisCache) StartAndGC

func (rc *RedisCache) StartAndGC(config string) error

Jump to

Keyboard shortcuts

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