cache

package
v0.0.0-...-1493fe1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2019 License: Apache-2.0 Imports: 14 Imported by: 0

README

cache

cache是一个go的缓存管理,使用适配器adapter适配各种模式,目前支持memcache,redis,file,memory

how to use

Documentation

Overview

*

*Created by Xie Jian on 2019/10/14 9:42

*

*Created by Xie Jian on 2019/10/14 10:07

Index

Constants

This section is empty.

Variables

View Source
var (
	// 缓存目录
	FileCachePath = "cache"
	// 缓存文件后缀
	FileCacheFileSuffix = ".bin"
	// 如果自动生成缓存文件,缓存文件的目录深度
	FileCacheDirectoryLevel = 2
	// 缓存文件的过期时间,默认不过期
	FileCacheEmbedExpiry time.Duration
)

文件缓存

View Source
var DefaultValidTimeout = 60 // 1分钟

默认过期校验时间

Functions

func GetBool

func GetBool(v interface{}) bool

func GetFloat64

func GetFloat64(v interface{}) float64

func GetInt

func GetInt(v interface{}) int

func GetInt64

func GetInt64(v interface{}) int64

func GetString

func GetString(v interface{}) string

func GobDecode

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

解码数据

func GobEncode

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

编码数据

func Register

func Register(adapterName CacheAdapterName, adapter CacheInstance)

注册函数,不支持重复注册

Types

type Cache

type Cache interface {
	// 通过key获取cache存储的值
	Get(key string) interface{}
	// 获取所有值
	GetMulti(keys []string) []interface{}
	// 存储key并设置过期时间
	Put(key string, val interface{}, timeout time.Duration) error
	// 删除key
	Delete(key string) error
	// 校验key是否存在
	IsExist(key string) bool
	// 清空
	ClearAll() error
	// 注册绑定方式
	StartCache(config string) error
}

func NewCache

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

NewCache新建cache

func NewFileCache

func NewFileCache() Cache

NewFileCache 返回空配置的接口

func NewMemoryCache

func NewMemoryCache() Cache

NewMemoryCache 返回接口

type CacheAdapterName

type CacheAdapterName string
const (
	CacheRedisAdapter    CacheAdapterName = "redis"
	CacheMemcacheAdapter CacheAdapterName = "memcache"
	CacheFileAdapter     CacheAdapterName = "file"
	CacheMemoryAdapter   CacheAdapterName = "memory"
)

type CacheInstance

type CacheInstance func() Cache

定义一个Cache的方程

type FileCache

type FileCache struct {
	CachePath      string `json:"cachePath"`
	FileSuffix     string `json:"fileSuffix"`
	DirectoryLevel int    `json:"directoryLevel"`
	EmbedExpired   int64  `json:"embedExpired"` // 过期时间
}

FileCache 文件缓存适配器

func (*FileCache) ClearAll

func (fc *FileCache) ClearAll() error

ClearAll 从根目录清空

func (*FileCache) Delete

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

Delete 删除文件缓存

func (*FileCache) Get

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

Get 从文件获取值,如果不存在或过期返回""

func (*FileCache) GetMulti

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

GetAll 返回根路径下所有的文件数据

func (*FileCache) IsExist

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

IsExist 文件缓存是否存在

func (*FileCache) Put

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

Put 存放文件缓存,timeout 单位ms,过期时间默认将来(100年)

func (*FileCache) StartCache

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

StartCache 调用文件缓存 config:{"cachePath":"/cache","fileSuffix":".log","directoryLevel":2,"embedExpired":0}

type FileCacheItem

type FileCacheItem struct {
	Data       interface{} `json:"data"`
	LastAccess time.Time   `json:"lastAccess"`
	Expired    time.Time   `json:"expired"`
}

FileCacheItem 存储元

type MemoryCache

type MemoryCache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

MemoryCache 内存适配器,用读写锁保证map的读写安全

func (*MemoryCache) ClearAll

func (mc *MemoryCache) ClearAll() error

ClearAll 清空所有key

func (*MemoryCache) Delete

func (mc *MemoryCache) Delete(key string) error

Delete 删除key

func (*MemoryCache) Get

func (mc *MemoryCache) Get(key string) interface{}

Get 从内存获取值,如果不存在或过期返回nil

func (*MemoryCache) GetMulti

func (mc *MemoryCache) GetMulti(keys []string) []interface{}

GetAll 返回所有key:value,不包含其他内容

func (*MemoryCache) IsExist

func (mc *MemoryCache) IsExist(key string) bool

IsExist 是否存在

func (*MemoryCache) Put

func (mc *MemoryCache) Put(key string, value interface{}, lifeSpan time.Duration) error

Put 存放key:value,lifeSpan设置过期时间,如果设为0,表示永远存在不会被删除

func (*MemoryCache) StartCache

func (mc *MemoryCache) StartCache(config string) error

StartCache 调用memory config: {"interval":60}

type MemoryItem

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

MemoryItem 存储元

Directories

Path Synopsis
* *Created by Xie Jian on 2019/10/25 9:28
* *Created by Xie Jian on 2019/10/25 9:28
* *Created by Xie Jian on 2019/10/24 16:01
* *Created by Xie Jian on 2019/10/24 16:01

Jump to

Keyboard shortcuts

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