gocache

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2021 License: MIT Imports: 12 Imported by: 1

README

gocache

Build Status codecov Go Report Card

一款简易的内存缓存实现,支持容量控制,TTL和数据落盘。

特性

  1. Store接口使用 sync.Map 和 读写锁+MAP 两种实现
  2. 采用 sync.Map 实现, 在多核,大量读取,锁竞争多的情况下存在优势,缺点是内存占用高,空间换时间。
  3. 支持容量限制,超过容量报错
  4. 支持重启程序加载缓存内容,简单防止因重启导致的缓存击穿。
  5. 支持 TTL Key

接口方法

package gocache

type Cache interface {
	Get(key string) (value interface{}, exists bool)                      //
	GetWithExpire(key string) (value interface{}, ttl int64, exists bool) // 返回值和剩余时间
	Set(key string, value interface{}) error                              //
	SetWithExpire(key string, value interface{}, ttl int64) error         // ttl 秒级别
	Keys(prefix string) Keys                                              // prefix - 前缀查询,"" 查询所有, 只返回当前有效的key
	Delete(key string)                                                    //
	Size() int64                                                          // 当前存储的数据量
	FlushAll()                                                            // 删除所有 key
	GobRegister(v ...interface{})                                         // 注册自定义结构体
	WriteToDisk() error                                                   // 写入数据到磁盘, 如果存在自定义结构类型,在使用时 一定要先注册结构
	LoadFromDisk() error                                                  // 从磁盘加载数据
	Close()                                                               //
}

type Keys interface {
	Size() int64
	Value() []string
}

type Store interface {
	Load(key string) (value interface{}, ok bool)
	Store(key string, value interface{})
	Delete(key string)
	LoadOrStore(key string, value interface{}) (actual interface{}, loaded bool)
	Exists(key string) bool
	Range(f func(k string, v interface{}) bool)
	Size() int64
	Flush()
}

Usage

go get github.com/bbdshow/gocache

请查看 example 目录

Notice

  • 选择 sync.Map 的实现方式需要 golang 1.11^
  • master 分支为开发分支,最新版本

FAQ

选择 sync.Map 实现

利用 sync.map 达到读取性能相对更高,sync.Map 并不太适应大量写入的缓存操作, 且因为计数使用了 LoadOrStrore 对 key 计数。 sync.Map 在内存空间上并不占优势,约 rwMutex + map 的2倍。

在 4 核以内的机器上锁竞争不明显, 所以 RwMutex map 在性能上更占优势,但是当 cpu 核数 往上时, 锁竞争变大, sync.Map 的优势就体现出来了。 性能测试 引用 https://medium.com/@deckarep/the-new-kid-in-town-gos-sync-map-de24a6bf7c2c

选择 rwMutex + map 实现

读写都比较均衡,同时内存占用比 sync.Map 约小1倍,如果读读取要求不强烈。建议选择此实现方式。

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeysOverLimitSize = errors.New("keys over limit size")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Get(key string) (value interface{}, exists bool)                      //
	GetWithExpire(key string) (value interface{}, ttl int64, exists bool) // 返回值和剩余时间
	Set(key string, value interface{}) error                              //
	SetWithExpire(key string, value interface{}, ttl int64) error         // ttl 秒级别
	Keys(prefix string) Keys                                              // prefix - 前缀查询,"" 查询所有, 只返回当前有效的key
	Delete(key string)                                                    //
	Size() int64                                                          // 当前存储的数据量
	FlushAll()                                                            // 删除所有 key
	GobRegister(v ...interface{})                                         // 注册自定义结构体
	WriteToDisk() error                                                   // 写入数据到磁盘, 如果存在自定义结构类型,在使用时 一定要先注册结构
	LoadFromDisk() error                                                  // 从磁盘加载数据
	Close()                                                               //
}

type Config

type Config struct {
	// 缓存容量, -1 - 不限制
	LimitSize int64
	// 保存文件位置, 不设置,默认当前执行路径
	Filename string
}

type Disk added in v0.3.0

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

func NewDisk added in v0.3.0

func NewDisk(filename string) *Disk

NewDisk

func (*Disk) ReadFromFile added in v0.3.0

func (d *Disk) ReadFromFile() ([]byte, error)

ReadFromFile 从文件读取数据

func (*Disk) WriteToFile added in v0.3.0

func (d *Disk) WriteToFile(data []byte) error

WriteToFile 写数据到文件 如果之前文件存在,则删除

type Keys added in v0.3.0

type Keys interface {
	Size() int64
	Value() []string
}

type MemCache added in v0.3.0

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

MemCache

func NewMemCache added in v0.3.0

func NewMemCache(store Store) *MemCache

func NewMemCacheWithConfig added in v0.3.0

func NewMemCacheWithConfig(store Store, config Config) *MemCache

func NewRWMapCache added in v0.3.0

func NewRWMapCache() *MemCache

func NewRWMapCacheWithConfig added in v0.3.0

func NewRWMapCacheWithConfig(config Config) *MemCache

func NewSyncMapCache added in v0.3.0

func NewSyncMapCache() *MemCache

func NewSyncMapCacheWithConfig added in v0.3.0

func NewSyncMapCacheWithConfig(config Config) *MemCache

func (*MemCache) AutoCleanExpireKey added in v0.3.0

func (mem *MemCache) AutoCleanExpireKey(interval time.Duration)

AutoCleanExpireKey 自动在一定时间内清理过期 key 当设置了大量的 expire key 且通常只读取一次的情况下再建议使用。 interval 建议设置大一点,否则可能影响写入性能,建议设置 5-10 minute

func (*MemCache) Close added in v0.3.0

func (mem *MemCache) Close()

Close

func (*MemCache) Delete added in v0.3.0

func (mem *MemCache) Delete(key string)

func (*MemCache) FlushAll added in v0.3.0

func (mem *MemCache) FlushAll()

FlushAll 清空所有数据

func (*MemCache) Get added in v0.3.0

func (mem *MemCache) Get(key string) (value interface{}, ok bool)

func (*MemCache) GetWithExpire added in v0.3.0

func (mem *MemCache) GetWithExpire(key string) (value interface{}, ttl int64, ok bool)

func (*MemCache) GobRegister added in v0.3.0

func (mem *MemCache) GobRegister(v ...interface{})

GobRegister 注册自定义结构

func (*MemCache) Keys added in v0.3.0

func (mem *MemCache) Keys(prefix string) Keys

func (*MemCache) LoadFromDisk added in v0.3.0

func (mem *MemCache) LoadFromDisk() error

LoadFromDisk 从磁盘中读取缓存内容,过过滤掉已经过期的内容

func (*MemCache) Set added in v0.3.0

func (mem *MemCache) Set(key string, value interface{}) error

func (*MemCache) SetWithExpire added in v0.3.0

func (mem *MemCache) SetWithExpire(key string, value interface{}, ttl int64) error

SetWithExpire ttl - 过期时间秒级别, -1 永久有效

func (*MemCache) Size added in v0.3.0

func (mem *MemCache) Size() int64

Size

func (*MemCache) WriteToDisk added in v0.3.0

func (mem *MemCache) WriteToDisk() error

WriteToDisk 缓存内容写入磁盘,当缓存内容比较大时,不建议写入磁盘,比较耗费时间

type RWMap added in v0.3.0

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

RWMap 读写Map 当数据竞争不强,或读取多时。使用节省空间更快

func NewRWMap added in v0.3.0

func NewRWMap() *RWMap

func (*RWMap) Delete added in v0.3.0

func (s *RWMap) Delete(key string)

func (*RWMap) Exists added in v0.3.0

func (s *RWMap) Exists(key string) bool

func (*RWMap) Flush added in v0.3.0

func (s *RWMap) Flush()

func (*RWMap) Load added in v0.3.0

func (s *RWMap) Load(key string) (value interface{}, ok bool)

func (*RWMap) LoadOrStore added in v0.3.0

func (s *RWMap) LoadOrStore(key string, value interface{}) (actual interface{}, loaded bool)

func (*RWMap) Range added in v0.3.0

func (s *RWMap) Range(f func(k string, v interface{}) bool)

func (*RWMap) Size added in v0.3.0

func (s *RWMap) Size() int64

func (*RWMap) Store added in v0.3.0

func (s *RWMap) Store(key string, value interface{})

type Store added in v0.3.0

type Store interface {
	Load(key string) (value interface{}, ok bool)
	Store(key string, value interface{})
	Delete(key string)
	LoadOrStore(key string, value interface{}) (actual interface{}, loaded bool)
	Exists(key string) bool
	Range(f func(k string, v interface{}) bool)
	Size() int64
	Flush()
}

type SyncMap added in v0.3.0

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

SyncMap 并发Map 当数据竞争大时,多核CPU时,使用比RwMap性能好,缺点空间占用会多点

func NewSyncMap added in v0.3.0

func NewSyncMap() *SyncMap

func (*SyncMap) Delete added in v0.3.0

func (s *SyncMap) Delete(key string)

func (*SyncMap) Exists added in v0.3.0

func (s *SyncMap) Exists(key string) bool

func (*SyncMap) Flush added in v0.3.0

func (s *SyncMap) Flush()

func (*SyncMap) Load added in v0.3.0

func (s *SyncMap) Load(key string) (value interface{}, ok bool)

func (*SyncMap) LoadOrStore added in v0.3.0

func (s *SyncMap) LoadOrStore(key string, value interface{}) (actual interface{}, loaded bool)

func (*SyncMap) Range added in v0.3.0

func (s *SyncMap) Range(fn func(k string, v interface{}) bool)

func (*SyncMap) Size added in v0.3.0

func (s *SyncMap) Size() int64

func (*SyncMap) Store added in v0.3.0

func (s *SyncMap) Store(key string, value interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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