cache

package
v0.0.0-...-2c17daf Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2021 License: MIT, MIT Imports: 11 Imported by: 1

README

Cache 缓存组件

将数据存储于各种缓存驱动中的组件

关于缓存的约定

  • 过期的数据一定不可被访问
  • 未过期的数据不保证在所有情况下的可用性
  • 所有的数据通过[]byte保存,通用可序列化结构的数据可以直接序列化保存
  • 所有缓存取出的原始[]byte数据不应该被修改。需要修改请自行复制或者反序列化
  • 缓存的Flush方法是否支持取决与具体驱动的实现

特性

  • 通用的缓存驱动接口,便于引入更多驱动
  • 支持并发
  • 引入Load方法,一定程度避免缓存雪崩
  • 提供cacheable接口,并提供collection和node两个额外实现,方便缓存的复用
  • 支持自定义数据的序列化方式

可用驱动

  • dummpycache:空缓存。不缓存任何数据
  • syncmapcache: 基于sync.Map的本地缓存驱动
  • freecache: 基于 github.com/coocood/freecache 的本地缓存驱动
  • sqlcache 基于sql的缓存
  • rediscache 基于redis的缓存,需要独占db
  • redisluacache 基于redis和lua的缓存。多个缓存可以共用db
  • versioncache 利用本地、远程缓存和版本控制,兼顾访问效率和多机可用的缓存接口

配置说明

#TOML版本,其他版本可以根据对应格式配置
#驱动名,具体值取决于需要使用的驱动
Driver="syncmapcache"
#缓存默认有效时间,单位为秒。
TTL=60   
#Config部分为具体驱动设置,参考各个驱动的文档
[Config]
Size=50000000

使用缓存

创建缓存对象
c:=cache.New()
config:=&cache.OptionConfig{}
err:=config.ApplyTo(c)
操作二进制数据([]byte)
 //根据主键获取数据
bs,err:=c.GetBytesValue("name")

//根据数据设置数据.第三个参数为缓存最大有效时间
err=c.SetBytesValue("name",bs,60*time.Second)

//根据数据更新数据.只有本身缓存中就有数据才会更新。不然不储存数据。也不会返回错误。
err=c.UpdateBytesValue("name",bs,60*time.Second)

//删除缓存
err=c.Del("name")

//重设缓存过期时间
err=c.Expire("name",60*time.Second)

//批量获取缓存数据。返回的结果为map[string][]byte形式
data,err=c.MGetBytesValue(keys ...string) (map[string][]byte, error)

//批量设置map[string][]byte形式的数据
err=c.MSetBytesValue(data,60*time.Second) 
使用预设的序列化器直接存取结构
//根据主键获取缓存值.必须传入指针
var v string
err=c.Get("name",&v)

//根据主键设置缓存
err=c.Set("name","value",60*time.Second)

//根据主键更新缓存
err=c.Set("name","value",60*time.Second)
通过Load方法和loader函数加载数据
使用计数器

同名的计数器和二进制/结构数据是独立额,互相不影响

//计数器递增值
value,err=c.IncrCounter("name", 1, 60 * time.Second)

//设置计数器的值
err=SetCounter("name", 10, 60 * time.Second)

//获取计数器的值
v,err=GetCounter("name")

//删除计数器的值
err=DelCounter("name")

//刷新计数器的过期时间
err=ExpireCounter("name", 10*time.Second) error
其他杂项操作
//清除所有数据。不是所有驱动都能支持
err=c.Flush()

//关闭缓存
err=c.Close()

//获取绝对主键
key=c.FinalKey(string)

//序列化数据 bs,err=c.Marshal(v)

//反序列化数据 var v string err=c.Unmarshal(bs,&v)

缓存操作错误

  • ErrNotFound: 指定主键的数据未找到
  • ErrNotCacheable :数据无法储存
  • ErrEntryTooLarge:数据过大
  • ErrKeyTooLarge:主键过长
  • ErrKeyUnavailable:主键无效
  • ErrFeatureNotSupported:驱动不支持该功能
  • ErrTTLNotAvaliable:TTL无效

缓存复用

缓存复用指将创建好的缓存划分成多个cacheable的组件,便于在不同的莫快中进行使用。目前支持的复用组件为Collection和Node

Collection

Collection可以通过cacheable.Collection(Name)的方式创建。 Collection支持flush数据。通过利用两个储存当前实际主键和实际数据的字段来实现。对于访问速度和内存占用有较大影响。请仅在需要对一系列数据进行flush操作时使用。

Node

Node可以通过cacheable.Node(Name)的方式创建。 Node不支持flush数据。通过给主键加上固定的前缀实现,对于访问速度和内存占用影响较小,推荐一般情况下使用。

Documentation

Overview

Package cache provide a key-value data store with ttl interface.

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrNotFound raised when the given data not found in cache.
	ErrNotFound = errors.New("Entry not found")
	//ErrNotCacheable raised if the data cannot be cached.
	ErrNotCacheable = errors.New("Not Cachable")
	//ErrEntryTooLarge raised when data is too large to store.
	ErrEntryTooLarge = errors.New("Entry too large to cache")
	//ErrKeyTooLarge raised when key is too large to store.
	ErrKeyTooLarge = errors.New("Key too large to cache")
	//ErrKeyUnavailable raised when the key is not available.For example,empty key.
	ErrKeyUnavailable = errors.New("Key Unavailable")
	//ErrFeatureNotSupported raised when calling feature on unsupported driver.
	ErrFeatureNotSupported = errors.New("Feature is not supported")
	//ErrTTLNotAvaliable raised when ttl not avaliable.
	ErrTTLNotAvaliable = errors.New("TTL not avaliable")
)
View Source
var CollectionTTLMultiple = 10

CollectionTTLMultiple default collection ttl multiple

View Source
var DefaultMarshaler = "msgpack"

DefaultMarshaler default marshaler name

View Source
var DefaultTTL = time.Duration(0)

DefaultTTL means use cache default ttl setting.

View Source
var (
	//KeyPrefix default key prefix
	KeyPrefix = string([]byte{0})
)
View Source
var TokenMask = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.")

TokenMask The []bytes of alphabet and number to generate token.

Functions

func Factories

func Factories() []string

Factories returns a sorted list of the names of the registered factories.

func Key

func Key(key string) string

Key return cache key

func MarshalerFactories

func MarshalerFactories() []string

MarshalerFactories returns a sorted list of the names of the registered marshaler factories.

func NewRandMaskedBytes

func NewRandMaskedBytes(mask []byte, length int, origin []byte) ([]byte, error)

NewRandMaskedBytes Generate a give length random []byte which different from origin bytes. All bytes in the random []byte is select from given mask. Return the random [] byte and any error raised.

func NewRandomBytes

func NewRandomBytes(length int, origin []byte) ([]byte, error)

NewRandomBytes Generate a give length random []byte which different from origin bytes. Return the random [] byte and any error raised.

func RandMaskedBytes

func RandMaskedBytes(mask []byte, length int) ([]byte, error)

RandMaskedBytes Generate a give length random []byte. All bytes in the random []byte is select from given mask. Return the random [] byte and any error raised.

func RandomBytes

func RandomBytes(length int) ([]byte, error)

RandomBytes Generate a give length random []byte. Return the random [] byte and any error raised.

func Register

func Register(name string, f Factory)

Register makes a driver creator available by the provided name. If Register is called twice with the same name or if driver is nil, it panics.

func RegisterMarshaler

func RegisterMarshaler(name string, f MarshalerFactory)

RegisterMarshaler makes a marshaler creator available by the provided name. If Register is called twice with the same name or if driver is nil, it panics.

func UnregisterAll

func UnregisterAll()

UnregisterAll all factorys

func UnregisterAllMarshalers

func UnregisterAllMarshalers()

UnregisterAllMarshalers Unregister all marshalers

Types

type Cache

type Cache struct {
	Driver
	TTL time.Duration
	// contains filtered or unexported fields
}

Cache Cache stores the cache Driver and default ttl.

func New

func New() *Cache

New :Create a empty cache.

func NewSubCache

func NewSubCache(conf *OptionConfig) (*Cache, error)

NewSubCache create subcache with given loader. Return cache created and any error if raised.

func (*Cache) DefaultTTL

func (c *Cache) DefaultTTL() time.Duration

DefaultTTL return cache default ttl

func (*Cache) Del

func (c *Cache) Del(key string) error

Del Delete data in cache by given name. Return any error raised.

func (*Cache) DelCounter

func (c *Cache) DelCounter(key string) error

DelCounter Delete int val in cache by given name.Count cache and data cache are in two independent namespace. Return any error raised.

func (*Cache) Expire

func (c *Cache) Expire(key string, ttl time.Duration) error

Expire set cache value expire duration by given key and ttl

func (*Cache) ExpireCounter

func (c *Cache) ExpireCounter(key string, ttl time.Duration) error

ExpireCounter set cache counter expire duration by given key and ttl

func (*Cache) Field

func (c *Cache) Field(fieldname string) *Field

Field retuan a cache field with given field name

func (*Cache) FinalKey

func (c *Cache) FinalKey(key string) string

FinalKey get final key which passed to cache driver .

func (*Cache) Get

func (c *Cache) Get(key string, v interface{}) error

Get Get data model from cache by given key. Parameter v should be pointer to empty data model which data filled in. Return any error raised.

func (*Cache) GetBytesValue

func (c *Cache) GetBytesValue(key string) ([]byte, error)

GetBytesValue Get bytes data from cache by given key. Return data bytes and any error raised.

func (*Cache) GetCounter

func (c *Cache) GetCounter(key string) (int64, error)

GetCounter Get int val from cache by given key.Count cache and data cache are in two independent namespace. Return int data value and any error raised.

func (*Cache) Hit

func (c *Cache) Hit() int64

Hit return cache hit count

func (*Cache) IncrCounter

func (c *Cache) IncrCounter(key string, increment int64, ttl time.Duration) (int64, error)

IncrCounter Increase int val in cache by given key.Count cache and data cache are in two independent namespace. If ttl is DefaultTTL(0),use default ttl in config instead. Return int data value and any error raised.

func (*Cache) Init

func (c *Cache) Init(option Option) error

Init init cache with option

func (*Cache) Load

func (c *Cache) Load(key string, v interface{}, ttl time.Duration, loader Loader) error

Load Get data model from cache by given key.If data not found,call loader to get current data value and save to cache. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Cache) Locker

func (c *Cache) Locker(key string) (*Locker, bool)

Locker create new locker with given key. return locker and if locker aleady locked.

func (*Cache) MGetBytesValue

func (c *Cache) MGetBytesValue(keys ...string) (map[string][]byte, error)

MGetBytesValue get multiple bytes data from cache by given keys. Return data bytes map and any error if raised.

func (*Cache) MSetBytesValue

func (c *Cache) MSetBytesValue(data map[string][]byte, ttl time.Duration) error

MSetBytesValue set multiple bytes data to cache with given key-value map. Return any error if raised.

func (*Cache) Marshal

func (c *Cache) Marshal(v interface{}) ([]byte, error)

Marshal Marshal data model to bytes. Return marshaled bytes and any error rasied.

func (*Cache) Miss

func (c *Cache) Miss() int64

Miss return cache miss count

func (*Cache) Proxy

func (c *Cache) Proxy(prefix string) *Proxy

Proxy get a cache proxy with given prefix

func (*Cache) Set

func (c *Cache) Set(key string, v interface{}, ttl time.Duration) error

Set Set data model to cache by given key. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Cache) SetBytesValue

func (c *Cache) SetBytesValue(key string, bytes []byte, ttl time.Duration) error

SetBytesValue Set bytes data to cache by given key. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Cache) SetCounter

func (c *Cache) SetCounter(key string, v int64, ttl time.Duration) error

SetCounter Set int val in cache by given key.Count cache and data cache are in two independent namespace. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Cache) Unmarshal

func (c *Cache) Unmarshal(bytes []byte, v interface{}) error

Unmarshal Unmarshal bytes to data model. Parameter v should be pointer to empty data model which data filled in. Return any error raseid.

func (*Cache) Update

func (c *Cache) Update(key string, v interface{}, ttl time.Duration) error

Update Update data model to cache by given key only if the cache exist. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Cache) UpdateBytesValue

func (c *Cache) UpdateBytesValue(key string, bytes []byte, ttl time.Duration) error

UpdateBytesValue Update bytes data to cache by given key only if the cache exist. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

type Cacheable

type Cacheable interface {
	MinimumOperation
	//Set Set data model to cache by given key.
	//If ttl is DefaultTTL(0),use default ttl in config instead.
	//Return any error raised.
	Set(key string, v interface{}, ttl time.Duration) error
	//Get Get data model from cache by given key.
	//Parameter v should be pointer to empty data model which data filled in.
	//Return any error raised.
	Get(key string, v interface{}) error
	//Update Update data model to cache by given key only if the cache exist.
	//If ttl is DefaultTTL(0),use default ttl in config instead.
	//Return any error raised.
	Update(key string, v interface{}, ttl time.Duration) error
	//Load Get data model from cache by given key.If data not found,call loader to get current data value and save to cache.
	//If ttl is DefaultTTL(0),use default ttl in config instead.
	//Return any error raised.
	Load(key string, v interface{}, ttl time.Duration, loader Loader) error
	//FinalKey get final key which passed to cache driver .
	FinalKey(string) string
	//DefaultTTL return cache default ttl
	DefaultTTL() time.Duration
	Hit() int64
	Miss() int64
}

Cacheable interface which can be used as cache.

func Dummy

func Dummy() Cacheable

func NewNestedCollection

func NewNestedCollection(cache Cacheable, path ...string) Cacheable

NewNestedCollection create new nested cache collection with given cache,prefix. Return collection created.

type Collection

type Collection struct {
	//Cache raw cache
	Cache Cacheable
	//Prefix cache key prefix
	Prefix string
	// default ttl
	TTL time.Duration
}

Collection cache Collection Collection is flushable sub cache create from other cacheable.

func NewCollection

func NewCollection(cache Cacheable, prefix string, TTL time.Duration) *Collection

NewCollection create new cache collection with given cache,prefix and ttl. Return collection created.

func (*Collection) Close

func (c *Collection) Close() error

Close cache.

func (*Collection) DefaultTTL

func (c *Collection) DefaultTTL() time.Duration

DefaultTTL return cache default ttl

func (*Collection) Del

func (c *Collection) Del(key string) error

Del Delete data in cache by given name. Return any error raised.

func (*Collection) DelCounter

func (c *Collection) DelCounter(key string) error

DelCounter Delete int val in cache by given name.Count cache and data cache are in two independent namespace. Return any error raised.

func (*Collection) Expire

func (c *Collection) Expire(key string, TTL time.Duration) error

Expire set cache value expire duration by given key and ttl

func (*Collection) ExpireCounter

func (c *Collection) ExpireCounter(key string, TTL time.Duration) error

ExpireCounter set cache counter expire duration by given key and ttl

func (*Collection) Field

func (c *Collection) Field(fieldname string) *Field

Field retuan a cache field with given field name

func (*Collection) FinalKey

func (c *Collection) FinalKey(key string) string

FinalKey get final key which passed to cache driver .

func (*Collection) Flush

func (c *Collection) Flush() error

Flush Delete all data in cache.

func (*Collection) Get

func (c *Collection) Get(key string, v interface{}) error

Get Get data model from cache by given key. Parameter v should be pointer to empty data model which data filled in. Return any error raised.

func (*Collection) GetBytesValue

func (c *Collection) GetBytesValue(key string) ([]byte, error)

GetBytesValue Get bytes data from cache by given key. Return data bytes and any error raised.

func (*Collection) GetCacheKey

func (c *Collection) GetCacheKey(key string) (string, error)

GetCacheKey return raw cache key by given key. Return key and any error if raised.

func (*Collection) GetCounter

func (c *Collection) GetCounter(key string) (int64, error)

GetCounter Get int val from cache by given key.Count cache and data cache are in two independent namespace. Return int data value and any error raised.

func (*Collection) Hit

func (c *Collection) Hit() int64

Hit return cache hit count

func (*Collection) IncrCounter

func (c *Collection) IncrCounter(key string, increment int64, TTL time.Duration) (int64, error)

IncrCounter Increase int val in cache by given key.Count cache and data cache are in two independent namespace. If ttl is DefaultTTL(0),use default ttl in config instead. Return int data value and any error raised.

func (*Collection) Load

func (c *Collection) Load(key string, v interface{}, TTL time.Duration, loader Loader) error

Load Get data model from cache by given key.If data not found,call loader to get current data value and save to cache. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Collection) MGetBytesValue

func (c *Collection) MGetBytesValue(keys ...string) (map[string][]byte, error)

MGetBytesValue get multiple bytes data from cache by given keys. Return data bytes map and any error if raised.

func (*Collection) MSetBytesValue

func (c *Collection) MSetBytesValue(data map[string][]byte, ttl time.Duration) error

MSetBytesValue set multiple bytes data to cache with given key-value map. Return any error if raised.

func (*Collection) Miss

func (c *Collection) Miss() int64

Miss return cache miss count

func (*Collection) Proxy

func (c *Collection) Proxy(prefix string) *Proxy

Proxy get a cache proxy with given prefix

func (*Collection) Set

func (c *Collection) Set(key string, v interface{}, TTL time.Duration) error

Set Set data model to cache by given key. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Collection) SetBytesValue

func (c *Collection) SetBytesValue(key string, bytes []byte, TTL time.Duration) error

SetBytesValue Set bytes data to cache by given key. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Collection) SetCounter

func (c *Collection) SetCounter(key string, v int64, TTL time.Duration) error

SetCounter Set int val in cache by given key.Count cache and data cache are in two independent namespace. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Collection) SetUtil

func (c *Collection) SetUtil(u *Util)

func (*Collection) Update

func (c *Collection) Update(key string, v interface{}, TTL time.Duration) error

Update Update data model to cache by given key only if the cache exist. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Collection) UpdateBytesValue

func (c *Collection) UpdateBytesValue(key string, bytes []byte, TTL time.Duration) error

UpdateBytesValue Update bytes data to cache by given key only if the cache exist. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Collection) Util

func (c *Collection) Util() *Util

type Driver

type Driver interface {
	MinimumOperation
	//Set callback to handler error raised when gc.
	SetGCErrHandler(f func(err error))
}

Driver : Cache driver interface.Should Never used directly

func MustNewDriver

func MustNewDriver(name string, loader func(v interface{}) error) Driver

MustNewDriver create new dirver with given driver name and loader. Return driver created. Painc is any error raised.

func NewDriver

func NewDriver(name string, loader func(v interface{}) error) (Driver, error)

NewDriver create new dirver with given driver name and loader. Return driver created and any error if raised.

type DriverUtil

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

DriverUtil drive util struct.

func (*DriverUtil) SetUtil

func (u *DriverUtil) SetUtil(util *Util)

SetUtil set util to cache

func (*DriverUtil) Util

func (u *DriverUtil) Util() *Util

Util return cache util

type DummyCache

type DummyCache struct {
	DriverUtil
}

DummyCache DummyCache dont store any data. Usually used in develop environment or testing

func (*DummyCache) Close

func (c *DummyCache) Close() error

Close Close cache. Return any error if raised

func (*DummyCache) Del

func (c *DummyCache) Del(key string) error

Del Delete data in cache by given key. Return any error raised.

func (*DummyCache) DelCounter

func (c *DummyCache) DelCounter(key string) error

DelCounter Delete int val in cache by given key.Count cache and data cache are in two independent namespace. Return any error raised.

func (*DummyCache) Expire

func (c *DummyCache) Expire(key string, ttl time.Duration) error

Expire set cache value expire duration by given key and ttl

func (*DummyCache) ExpireCounter

func (c *DummyCache) ExpireCounter(key string, ttl time.Duration) error

ExpireCounter set cache counter expire duration by given key and ttl

func (*DummyCache) Flush

func (c *DummyCache) Flush() error

Flush Delete all data in cache. Return any error if raised

func (*DummyCache) GetBytesValue

func (c *DummyCache) GetBytesValue(key string) ([]byte, error)

GetBytesValue Get bytes data from cache by given key. Return data bytes and any error raised.

func (*DummyCache) GetCounter

func (c *DummyCache) GetCounter(key string) (int64, error)

GetCounter Get int val from cache by given key.Count cache and data cache are in two independent namespace. Return int data value and any error raised.

func (*DummyCache) IncrCounter

func (c *DummyCache) IncrCounter(key string, increment int64, ttl time.Duration) (int64, error)

IncrCounter Increase int val in cache by given key.Count cache and data cache are in two independent namespace. Return int data value and any error raised.

func (*DummyCache) MGetBytesValue

func (c *DummyCache) MGetBytesValue(keys ...string) (map[string][]byte, error)

MGetBytesValue get multiple bytes data from cache by given keys. Return data bytes map and any error if raised.

func (*DummyCache) MSetBytesValue

func (c *DummyCache) MSetBytesValue(data map[string][]byte, ttl time.Duration) error

MSetBytesValue set multiple bytes data to cache with given key-value map. Return any error if raised.

func (*DummyCache) SetBytesValue

func (c *DummyCache) SetBytesValue(key string, bytes []byte, ttl time.Duration) error

SetBytesValue Set bytes data to cache by given key. Return any error raised.

func (*DummyCache) SetCounter

func (c *DummyCache) SetCounter(key string, v int64, ttl time.Duration) error

SetCounter Set int val in cache by given key.Count cache and data cache are in two independent namespace. Return any error raised.

func (*DummyCache) SetGCErrHandler

func (c *DummyCache) SetGCErrHandler(f func(err error))

SetGCErrHandler Set callback to handler error raised when gc.

func (*DummyCache) UpdateBytesValue

func (c *DummyCache) UpdateBytesValue(key string, bytes []byte, ttl time.Duration) error

UpdateBytesValue Update bytes data to cache by given key only if the cache exist. Return any error raised.

type Factory

type Factory func(loader func(v interface{}) error) (Driver, error)

Factory create driver with given loader. Reutrn driver created and any error if raised..

type Field

type Field struct {
	Cache     Cacheable
	FieldName string
}

Field cache field component

func (*Field) Del

func (f *Field) Del() error

Del del field value. Return any error if raised.

func (*Field) DelCounter

func (f *Field) DelCounter() error

DelCounter delete field counter. Return any error if raised.

func (*Field) Get

func (f *Field) Get(v interface{}) error

Get value from field . Return any error if raised.

func (*Field) GetBytesValue

func (f *Field) GetBytesValue() ([]byte, error)

GetBytesValue get bytes value from field Return bytes value and any error if raised.

func (*Field) GetCounter

func (f *Field) GetCounter() (int64, error)

GetCounter get field counter. Return counter value and any error if raised.

func (*Field) IncrCounter

func (f *Field) IncrCounter(increment int64, ttl time.Duration) (int64, error)

IncrCounter incr field counter with given increment and ttl Return new counter value and any error if raised.

func (*Field) Set

func (f *Field) Set(v interface{}, ttl time.Duration) error

Set set field value with given ttl. Return any error if raised.

func (*Field) SetBytesValue

func (f *Field) SetBytesValue(bytes []byte, ttl time.Duration) error

SetBytesValue set bytes to field with given ttl. Return any error if raised.

func (*Field) SetCounter

func (f *Field) SetCounter(v int64, ttl time.Duration) error

SetCounter set field counter with given value and ttl. Return any error if raised.

func (*Field) Update

func (f *Field) Update(v interface{}, ttl time.Duration) error

Update update field value with given ttl. Return any error if raised.

func (*Field) UpdateBytesValue

func (f *Field) UpdateBytesValue(bytes []byte, ttl time.Duration) error

UpdateBytesValue update bytes to field with given ttl. Return any error if raised.

type JSONMarshaler

type JSONMarshaler struct {
}

JSONMarshaler Marshaler which marshal data as JSON format

func (*JSONMarshaler) Marshal

func (m *JSONMarshaler) Marshal(v interface{}) ([]byte, error)

Marshal Marshal data model to bytes. Return marshaled bytes and any erro rasied.

func (*JSONMarshaler) Unmarshal

func (m *JSONMarshaler) Unmarshal(bytes []byte, v interface{}) error

Unmarshal Unmarshal bytes to data model. Parameter v should be pointer to empty data model which data filled in. Return any error raseid.

type Loader

type Loader func(key string) (interface{}, error)

Loader cache value loader used in cache load method. Load value with given key. Return loaded value and any error if raised.

type Locker

type Locker struct {
	sync.RWMutex
	Map *sync.Map
	Key string
}

Locker cache locker

func (*Locker) Unlock

func (l *Locker) Unlock()

Unlock unlock and delete locker

type Marshaler

type Marshaler interface {
	Marshal(v interface{}) ([]byte, error)
	Unmarshal(bytes []byte, v interface{}) error
}

Marshaler Marshaler inteface

func NewMarshaler

func NewMarshaler(name string) (Marshaler, error)

NewMarshaler create new marshaler with given name. Return marshaler created and any error if raised.

type MarshalerFactory

type MarshalerFactory func() (Marshaler, error)

MarshalerFactory create marshaler. Reutrn marshaler created and any error if raised..

type MinimumOperation

type MinimumOperation interface {
	Util() *Util
	SetUtil(*Util)
	//SetBytesValue Set bytes data to cache by given key.
	//If ttl is DefaultTTL(0),use default ttl in config instead.
	//Return any error raised.
	SetBytesValue(key string, bytes []byte, ttl time.Duration) error
	//UpdateBytesValue Update bytes data to cache by given key only if the cache exist.
	//If ttl is DefaultTTL(0),use default ttl in config instead.
	//Return any error raised.
	UpdateBytesValue(key string, bytes []byte, ttl time.Duration) error
	//GetBytesValue Get bytes data from cache by given key.
	//Return data bytes and any error raised.
	GetBytesValue(key string) ([]byte, error)
	//Del Delete data in cache by given name.
	//Return any error raised.
	Del(key string) error
	//IncrCounter Increase int val in cache by given key.Count cache and data cache are in two independent namespace.
	//If ttl is DefaultTTL(0),use default ttl in config instead.
	//Return int data value and any error raised.
	IncrCounter(key string, increment int64, ttl time.Duration) (int64, error)
	//SetCounter Set int val in cache by given key.Count cache and data cache are in two independent namespace.
	//If ttl is DefaultTTL(0),use default ttl in config instead.
	//Return any error raised.
	SetCounter(key string, v int64, ttl time.Duration) error
	//GetCounter Get int val from cache by given key.Count cache and data cache are in two independent namespace.
	//Return int data value and any error raised.
	GetCounter(key string) (int64, error)
	//DelCounter Delete int val in cache by given name.Count cache and data cache are in two independent namespace.
	//Return any error raised.
	DelCounter(key string) error
	//Expire set cache value expire duration by given key and ttl
	//Return any error raised.
	Expire(key string, ttl time.Duration) error
	//ExpireCounter set cache counter  expire duration by given key and ttl
	ExpireCounter(key string, ttl time.Duration) error
	//MGetBytesValue get multiple bytes data from cache by given keys.
	//Return data bytes map and any error if raised.
	MGetBytesValue(keys ...string) (map[string][]byte, error)
	//MSetBytesValue set multiple bytes data to cache with given key-value map.
	//Return  any error if raised.
	MSetBytesValue(map[string][]byte, time.Duration) error
	//Close close cache.
	Close() error
	//Flush Delete all data in cache.
	Flush() error
}

type Node

type Node struct {
	Cache  Cacheable
	Prefix string
}

Node cache Collection Node is Permanent-able sub cache create from other cacheable.

func NewNode

func NewNode(c Cacheable, prefix string) *Node

NewNode create new cache node with given cacheable and prefix. Return node created.

func (*Node) Close

func (n *Node) Close() error

Close close cache.

func (*Node) Collection

func (n *Node) Collection(prefix string) *Collection

Collection get a cache colletion with given prefix

func (*Node) DefaultTTL

func (n *Node) DefaultTTL() time.Duration

DefaultTTL return cache default ttl

func (*Node) Del

func (n *Node) Del(key string) error

Del Delete data in cache by given name. Return any error raised.

func (*Node) DelCounter

func (n *Node) DelCounter(key string) error

DelCounter Delete int val in cache by given name.Count cache and data cache are in two independent namespace. Return any error raised.

func (*Node) Expire

func (n *Node) Expire(key string, ttl time.Duration) error

Expire set cache value expire duration by given key and ttl

func (*Node) ExpireCounter

func (n *Node) ExpireCounter(key string, ttl time.Duration) error

ExpireCounter set cache counter expire duration by given key and ttl

func (*Node) Field

func (n *Node) Field(fieldname string) *Field

Field retuan a cache field with given field name

func (*Node) FinalKey

func (n *Node) FinalKey(key string) string

FinalKey get final key which passed to cache driver .

func (*Node) Flush

func (n *Node) Flush() error

Flush Delete all data in cache.

func (*Node) Get

func (n *Node) Get(key string, v interface{}) error

Get Get data model from cache by given key. Parameter v should be pointer to empty data model which data filled in. Return any error raised.

func (*Node) GetBytesValue

func (n *Node) GetBytesValue(key string) ([]byte, error)

GetBytesValue Get bytes data from cache by given key. Return data bytes and any error raised.

func (*Node) GetCacheKey

func (n *Node) GetCacheKey(key string) (string, error)

GetCacheKey return raw cache key by given key. Return key and any error if raised.

func (*Node) GetCounter

func (n *Node) GetCounter(key string) (int64, error)

GetCounter Get int val from cache by given key.Count cache and data cache are in two independent namespace. Return int data value and any error raised.

func (*Node) Hit

func (c *Node) Hit() int64

Hit return cache hit count

func (*Node) IncrCounter

func (n *Node) IncrCounter(key string, increment int64, ttl time.Duration) (int64, error)

IncrCounter Increase int val in cache by given key.Count cache and data cache are in two independent namespace. If ttl is DefaultTTL(0),use default ttl in config instead. Return int data value and any error raised.

func (*Node) Load

func (n *Node) Load(key string, v interface{}, TTL time.Duration, loader Loader) error

Load Get data model from cache by given key.If data not found,call loader to get current data value and save to cache. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Node) MGetBytesValue

func (n *Node) MGetBytesValue(keys ...string) (map[string][]byte, error)

MGetBytesValue get multiple bytes data from cache by given keys. Return data bytes map and any error if raised.

func (*Node) MSetBytesValue

func (n *Node) MSetBytesValue(data map[string][]byte, ttl time.Duration) error

MSetBytesValue set multiple bytes data to cache with given key-value map. Return any error if raised.

func (*Node) Miss

func (c *Node) Miss() int64

Miss return cache miss count

func (*Node) MustGetCacheKey

func (n *Node) MustGetCacheKey(key string) string

MustGetCacheKey return raw cache key by given key. Return key. Panic if any error raised.

func (*Node) Node

func (n *Node) Node(prefix string) *Node

Node get a cache node with given prefix

func (*Node) Set

func (n *Node) Set(key string, v interface{}, ttl time.Duration) error

Set Set data model to cache by given key. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Node) SetBytesValue

func (n *Node) SetBytesValue(key string, bytes []byte, ttl time.Duration) error

SetBytesValue Set bytes data to cache by given key. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Node) SetCounter

func (n *Node) SetCounter(key string, v int64, ttl time.Duration) error

SetCounter Set int val in cache by given key.Count cache and data cache are in two independent namespace. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Node) SetUtil

func (n *Node) SetUtil(u *Util)

func (*Node) Update

func (n *Node) Update(key string, v interface{}, TTL time.Duration) error

Update Update data model to cache by given key only if the cache exist. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Node) UpdateBytesValue

func (n *Node) UpdateBytesValue(key string, bytes []byte, TTL time.Duration) error

UpdateBytesValue Update bytes data to cache by given key only if the cache exist. If ttl is DefaultTTL(0),use default ttl in config instead. Return any error raised.

func (*Node) Util

func (n *Node) Util() *Util

type Option

type Option interface {
	ApplyTo(*Cache) error
}

Option cache option interface.

type OptionConfig

type OptionConfig struct {
	Driver    string
	TTL       int64
	Marshaler string
	Config    func(v interface{}) error `config:", lazyload"`
}

OptionConfig cache option

func NewOptionConfig

func NewOptionConfig() *OptionConfig

NewOptionConfig create new cache option.

func (*OptionConfig) ApplyTo

func (o *OptionConfig) ApplyTo(cache *Cache) error

ApplyTo apply option to given cache. Return any error if raised.

type Proxy

type Proxy struct {
	Cacheable
}

func NewProxy

func NewProxy(c Cacheable) *Proxy

func ProxyWithPrefix

func ProxyWithPrefix(c Cacheable, prefix string) *Proxy

type Util

type Util struct {
	Marshaler Marshaler
	// contains filtered or unexported fields
}

Util cache util

func NewUtil

func NewUtil() *Util

NewUtil create new util

func (*Util) Clone

func (u *Util) Clone() *Util

Clone clone util

func (*Util) Locker

func (u *Util) Locker(key string) (*Locker, bool)

Locker create new locker with given key. Return locker and if locker is locked.

func (*Util) Marshal

func (u *Util) Marshal(v interface{}) ([]byte, error)

Marshal Marshal data model to bytes. Return marshaled bytes and any error rasied.

func (*Util) Unmarshal

func (u *Util) Unmarshal(bytes []byte, v interface{}) error

Unmarshal Unmarshal bytes to data model. Parameter v should be pointer to empty data model which data filled in. Return any error raseid.

Directories

Path Synopsis
drivers
cachegroup
Package cachegroup provides a cache drive which combined with give caches driver.
Package cachegroup provides a cache drive which combined with give caches driver.
freecache
Package freecache provides cache driver uses memory to store cache data.
Package freecache provides cache driver uses memory to store cache data.
syncmapcache
Package syncmapcache provides cache driver uses sync.map to store cache data.
Package syncmapcache provides cache driver uses sync.map to store cache data.
marshalers

Jump to

Keyboard shortcuts

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