icache

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

icache

开箱即用的缓存库,插件式实现和支持singleflight回源

CacheIf 缓存器接口

type CacheIf interface {
	Get(context.Context, string) (interface{}, error)
	Set(context.Context, string, interface{}, int32) error
	Del(context.Context, string) error
	IsErrNotFound(err error) bool
}

GetterIf 回源接口

type GetterIf interface {
	Get(context.Context, string, SinkIf) error
}

FlightGroupIf flight group

type FlightGroupIf interface {
	Do(string, func() (interface{}, error)) (interface{}, error)
}
// 两个协程并发回调回源
// 1: Get("key")
// 2: Get("key")
// 1: loadCache("key")
// 2: loadCache("key")
// 1: load("key")
// 2: load("key")
// 1: flightGroup.Do("key", fn)
// 1: fn()
// 2: flightGroup.Do("key", fn)
// 2: fn()

Stats 操作统计

type Stats struct {
	GetCnt  int64 // cache get op cnt
	DelCnt  int64 // cache del op cnt
	HitCnt  int64 // cache hit cnt
	ErrCnt  int64 // cache errors cnt
	MissCnt int64 // cache miss cnt

	SourceCnt    int64 // get from source cnt
	SourceHitCnt int64 // get from source hit cnt
	SourceErrCnt int64 // get from source err cnt
}

example

see icache_test.go

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound not found err
	ErrNotFound = fmt.Errorf("err not found")
	// ErrCacheIf CacheIf err
	ErrCacheIf = fmt.Errorf("err CacheIf")
	// ErrGetterIf GetterIf err
	ErrGetterIf = fmt.Errorf("err GetterIf")
	// ErrRateLimit rate limit err
	ErrRateLimit = fmt.Errorf("err ratelimit")
)

Functions

This section is empty.

Types

type CacheIf

type CacheIf interface {
	Get(context.Context, string) (interface{}, error)
	Set(context.Context, string, interface{}, int32) error
	Del(context.Context, string) error
	IsErrNotFound(err error) bool
}

CacheIf cache interface

func NewLRUByteCache

func NewLRUByteCache(iSize int) CacheIf

NewLRUByteCache new lru cache

func NewLRUByteCacheWithEvict

func NewLRUByteCacheWithEvict(iSize int, onEvicted func(key interface{}, value interface{})) CacheIf

NewLRUByteCacheWithEvict new lru cache

func NewLRUObjCache

func NewLRUObjCache(iSize int) CacheIf

NewLRUObjCache new lru cache

func NewLRUObjCacheWithEvict

func NewLRUObjCacheWithEvict(iSize int, onEvicted func(key interface{}, value interface{})) CacheIf

NewLRUObjCacheWithEvict new lru cache

type FlightGroupIf

type FlightGroupIf interface {
	Do(string, func() (interface{}, error)) (interface{}, error)
}

FlightGroupIf flight group

type GetterIf

type GetterIf interface {
	Get(context.Context, string, SinkIf) error
}

GetterIf getter interface

type GetterIfFunc

type GetterIfFunc func(context.Context, string, SinkIf) error

GetterIfFunc func

func (GetterIfFunc) Get

func (f GetterIfFunc) Get(ctx context.Context, strKey string, ifSink SinkIf) error

Get get

type ICache

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

ICache ICache struct

func NewICache

func NewICache(opts ...Option) (*ICache, error)

NewICache new ICache

func (*ICache) Delete

func (ic *ICache) Delete(ctx context.Context, strKey string) error

Delete del key

func (*ICache) Get

func (ic *ICache) Get(ctx context.Context, strKey string, dest SinkIf) error

Get get key

func (*ICache) GetStat

func (ic *ICache) GetStat() Stats

GetStat get stat

type LRUByteCache

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

LRUByteCache

func (*LRUByteCache) Del

func (c *LRUByteCache) Del(ctx context.Context, strKey string) error

Del del

func (*LRUByteCache) Get

func (c *LRUByteCache) Get(ctx context.Context, strKey string) (interface{}, error)

Get get

func (*LRUByteCache) IsErrNotFound

func (c *LRUByteCache) IsErrNotFound(err error) bool

IsErrNotFound is not found err

func (*LRUByteCache) Set

func (c *LRUByteCache) Set(ctx context.Context, strKey string, valIf interface{}, iTTL int32) error

Set set

type LRUObjCache

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

LRUObjCache lru obj cache

func (*LRUObjCache) Del

func (c *LRUObjCache) Del(ctx context.Context, strKey string) error

Del del

func (*LRUObjCache) Get

func (c *LRUObjCache) Get(ctx context.Context, strKey string) (interface{}, error)

Get get

func (*LRUObjCache) IsErrNotFound

func (c *LRUObjCache) IsErrNotFound(err error) bool

func (*LRUObjCache) Set

func (c *LRUObjCache) Set(ctx context.Context, strKey string, valIf interface{}, iTTL int32) error

Set set

type Option

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

Option option

func SetCache

func SetCache(cache CacheIf) Option

SetCache set cache

func SetFlightGroup

func SetFlightGroup(flightGroup FlightGroupIf) Option

SetFlightGroup set flight group

func SetGetter

func SetGetter(getter GetterIf) Option

SetGetter set getter

func SetRateLimit

func SetRateLimit(iPerSecLimit int64) Option

SetRateLimit set rate limit

type SinkIf

type SinkIf interface {
	SetView(v View) error
	GetView() (View, error)
	SetBytes([]byte) error
	SetString(string) error
	SetObj(interface{}) error
	SetTTL(int32) error
}

SinkIf sink interface

func ByteSink

func ByteSink(bp *[]byte) SinkIf

ByteSink new byteSink

func ObjSink

func ObjSink(pObj interface{}) SinkIf

ObjSink new obj sink

func StringSink

func StringSink(sp *string) SinkIf

StringSink new stringSink

type Stats

type Stats struct {
	GetCnt  int64 // cache get op cnt
	DelCnt  int64 // cache del op cnt
	HitCnt  int64 // cache hit cnt
	ErrCnt  int64 // cache errors cnt
	MissCnt int64 // cache miss cnt

	SourceCnt    int64 // get from source cnt
	SourceHitCnt int64 // get from source hit cnt
	SourceErrCnt int64 // get from source err cnt
}

Stats stat

func (*Stats) AddDel

func (s *Stats) AddDel(n int64)

AddDel add del

func (*Stats) AddErr

func (s *Stats) AddErr(n int64)

AddErr add err

func (*Stats) AddGet

func (s *Stats) AddGet(n int64)

AddGet add get

func (*Stats) AddHit

func (s *Stats) AddHit(n int64)

AddHit add hit

func (*Stats) AddMiss

func (s *Stats) AddMiss(n int64)

AddMiss add miss

func (*Stats) AddSource

func (s *Stats) AddSource(n int64)

AddSource add source

func (*Stats) AddSourceErr

func (s *Stats) AddSourceErr(n int64)

AddSourceErr add source err

func (*Stats) AddSourceHit

func (s *Stats) AddSourceHit(n int64)

AddSourceHit add source hit

type View

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

View view

Directories

Path Synopsis
Package singleflight provides a duplicate function call suppression mechanism.
Package singleflight provides a duplicate function call suppression mechanism.

Jump to

Keyboard shortcuts

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