wukong

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2021 License: Apache-2.0 Imports: 14 Imported by: 0

README

wukong(行者,悟空)

悟空,Golang版本的缓存框架,存在的唯一目的就是加速系统性能

为什么要叫悟空

《西游记》里记载,孙悟空一个筋斗云十万八千里,和系统缓存加载异曲同工

功能

  • 支持本地内存缓存BigCache
  • 支持Redis
  • 支持Memcache
  • 统一的缓存接口
  • 方便集成自己的缓存
  • 更友好的API设计

内置存储

  • Redis
  • Memcache
  • BigCache

内置序列化

  • Msgpack
  • Json
  • Xml

为什么要写这个缓存

最近一直在寻找一个统一的Golang版本的缓存框架,无奈于Golang的生态确实不如Java,各自为政,许久寻得一个框架基本满足要求https://github.com/eko/gocache 但是其接口设计使用使用起来特别麻烦,每次存放数据,都得传Options参数,还不能省略,十分讨厌,所以在其基础之上,增加更易使用的方法形成此框架

样例代码

简单缓存

简单缓存只提供了缓存的基本功能,不包括链式调用、监控等功能

使用Redis
store := wukong.NewMemcache(redis.NewClient(&redis.Options{
	Addr: "127.0.0.1:6379",
}))

cache := wukong.New(store)
err := cache.Set("my-key", "my-value", WithExpiration(15*time.Second))
if err != nil {
    panic(err)
}

value := cache.Get("my-key")
使用Memcache
store := wukong.NewRedis(memcache.New(
	"10.0.0.1:11211", 
	"10.0.0.2:11211", 
	"10.0.0.3:11212",
))

cache := wukong.New(store)
err := cache.Set("my-key", "my-value", WithExpiration(15*time.Second))
if err != nil {
    panic(err)
}

value := cache.Get("my-key")
使用BigCache
bigcache, _ := bigcache.NewBigCache(bigcache.DefaultConfig(5*time.Minute))
store := store.NewBigcache(bigcache)

cache := wukong.New(store)
err := cache.Set("my-key", "my-value", WithExpiration(15*time.Second))
if err != nil {
    panic(err)
}

value := cache.Get("my-key")
定制缓存
序列化器

序列化器可以定制你存放在缓存中的数据序列化方式(从结构体到二进制数据以及从二进制数据到结构体),wukong提供了方便使用的序列化器,且内置常用的序列化器

代码
store := wukong.NewRedis(redis.NewClient(&redis.Options{
	Addr: "127.0.0.1:6379",
}), WithSerializer(wukong.SerializerJson{}))

cache := wukong.New(store)
err := cache.Set("my-key", "my-value", WithExpiration()15*time.Second)
if err != nil {
    panic(err)
}

value := cache.Get("my-key")
支持的序列化器有
  • wukong.SerializerJson
  • wukong.SerializerXml
  • wukong.SerializerMsgpack
增加自己的序列化器

实现序列化接口,就可以方便的实现自己的序列化器

type Serializer interface {
	// Marshal 将结构体编码成二进制数组
	Marshal(obj interface{}) ([]byte, error)
	// Unmarshal 将二进制数据解码成结构体
	Unmarshal(data []byte, obj interface{}) error
}
缓存数据存放

可以很方便的增加自己的缓存存储方案,只需要实现一个特定的接口即可

代码
type Store interface {
	// Get 取得缓存值
	Get(key string) ([]byte, error)
	// Set 设置缓存值
	Set(key string, data []byte, options ...option) error
	// Delete 删除缓存值
	Delete(key string) error
	// Invalidate 设置缓存失效
	Invalidate(options ...invalidateOption) error
	// Clear 清除缓存
	Clear() error
	// Type 缓存类型
	Type() string
}
内置的数据存放方案
  • wukong.NewRedis
  • wukong.NewBigCache

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BigCache added in v1.2.0

func BigCache(client *bigcache.BigCache) *storeBigCache

BigCache 创建一个BigCache存储

func Expiration added in v1.2.0

func Expiration(expiration time.Duration) *optionExpiration

Expiration 配置过期时间

func Gob added in v1.2.0

func Gob() *optionSerializer

Gob Golang内置序列化器

func Json added in v1.2.0

func Json() *optionSerializer

Json Json列化器

func Memcache added in v1.2.0

func Memcache(client *memcache.Client) *storeMemcache

Memcache 创建一个Memcache存储

func Msgpack added in v1.2.0

func Msgpack() *optionSerializer

Msgpack Msgpack列化器,比Json生成的序列化数据更简短

func Redis added in v1.2.0

func Redis(client *redis.Client) *storeRedis

Redis 创建一个Redis存储

func Tags added in v1.2.0

func Tags(tags ...string) *optionTags

Tags 配置标签

Types

type Decoder

type Decoder interface {
	// Decode 将二进制数据解码成结构体
	Decode(data []byte) (interface{}, error)
}

type Encoder

type Encoder interface {
	// Encode 将结构体编码成二进制数组
	Encode(obj interface{}) ([]byte, error)
}

type Serializer

type Serializer interface {
	Encoder
	Decoder
}

Serializer 序列化接口,用于将结构体编码成二进制数组或者将二进制数据解码成结构体

type Store

type Store interface {
	// Get 取得缓存值
	Get(key string) ([]byte, error)

	// Set 设置缓存值
	Set(key string, data []byte, expiration time.Duration, tags ...string) error

	// Delete 删除缓存值
	Delete(key string) error

	// Invalidate 设置缓存失效
	Invalidate(tags ...string) error

	// Clear 清除缓存
	Clear() error

	// Type 缓存类型
	Type() Type
}

Store 描述一个缓存真正的存储

type Type added in v1.2.0

type Type string

Type 类型

const (
	// TypeBigCache 基于内存的缓存
	TypeBigCache Type = "bigCache"
	// TypeMemcache 基于Memcache分布式缓存
	TypeMemcache Type = "memcache"
	// TypeRedis 基于Redis的分布式缓存
	TypeRedis Type = "redis"
)

type Wukong added in v1.2.0

type Wukong interface {
	// Get 从缓存中取得对象
	Get(key string) (value interface{}, err error)

	// Set 设置缓存值
	Set(key string, value interface{}, options ...option) (err error)

	// Delete 从缓存中删除一个对象
	Delete(key string) (err error)

	// Invalidate 让缓存失效
	Invalidate(opts ...option) (err error)

	// Clear 清空缓存
	Clear() (err error)

	// Serializer 设置序列化器
	Serializer(serializer Serializer)

	// Type 缓存类型
	Type() Type
}

Wukong 描述一个缓存

func New

func New(store Store) Wukong

New 创建一个普通缓存

Jump to

Keyboard shortcuts

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