mcache

package module
v0.0.0-...-20629ee Latest Latest
Warning

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

Go to latest
Published: May 1, 2022 License: MIT Imports: 14 Imported by: 0

README

mcache-go

A local memory cache for golang service. Support LFU, LRU, ARC evication cache.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	KeyNotFoundError   = errors.New("mcache: key not found.")
	KeyExpiredError    = errors.New("mcache: key expired.")
	RedisNotFoundError = errors.New("mcache: redis not found.")
	SerializeError     = errors.New("mcache: serialize error.")
	KeyValueLenError   = errors.New("mcache: len of key != len of value.")
	DefValSetError     = errors.New("mcache: set def val, 1min expiration.")
)

Functions

func Fnv32

func Fnv32(key []byte) uint32

func KeyToHash

func KeyToHash(key interface{}) uint64

func MemHash

func MemHash(data []byte) uint64

MemHash is the hash function used by go map, it utilizes available hardware instructions(behaves as aeshash if aes instruction is available). NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.

func MemHashString

func MemHashString(str string) uint64

MemHashString is the hash function used by go map, it utilizes available hardware instructions (behaves as aeshash if aes instruction is available). NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.

func RandString

func RandString(l int) string

func XXHashString

func XXHashString(str string) uint64

Types

type ArcCache

type ArcCache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*ArcCache) Evict

func (c *ArcCache) Evict(ctx context.Context, count int)

func (*ArcCache) Exists

func (c *ArcCache) Exists(ctx context.Context, key string) bool

func (*ArcCache) Get

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

func (*ArcCache) Init

func (c *ArcCache) Init(clock Clock, capacity int)

func (*ArcCache) Remove

func (c *ArcCache) Remove(ctx context.Context, key string) bool

func (*ArcCache) Set

func (c *ArcCache) Set(ctx context.Context, key string, val interface{}, ttl time.Duration) error

type Cache

type Cache interface {
	Set(ctx context.Context, key string, value interface{}, opts ...Option) error
	MSet(ctx context.Context, keys []string, values []interface{}, opts ...Option) error

	Get(ctx context.Context, key string, opts ...Option) (interface{}, error)
	MGet(ctx context.Context, keys []string, opts ...Option) (map[string]interface{}, error)

	Remove(ctx context.Context, key string) bool
	MRemove(ctx context.Context, keys []string) bool
	Exists(ctx context.Context, key string) bool

	//only for debug
	DebugShardIndex(key string) uint64
	// contains filtered or unexported methods
}

func New

func New[T any, P CachePolicy[T]](size int, opts ...Option) Cache

type CachePolicy

type CachePolicy[T any] interface {
	Init(clock Clock, capacity int)
	Set(ctx context.Context, key string, val interface{}, ttl time.Duration) error
	Get(ctx context.Context, key string) (interface{}, error)
	Exists(ctx context.Context, key string) bool
	Remove(ctx context.Context, key string) bool
	Evict(ctx context.Context, count int)

	sync.Locker
	*T
}

type Clock

type Clock interface {
	Now() time.Time
}

func NewRealClock

func NewRealClock() Clock

type FakeClock

type FakeClock interface {
	Clock
	Advance(d time.Duration)
}

func NewFakeClock

func NewFakeClock() FakeClock

type LfuCache

type LfuCache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*LfuCache) Evict

func (c *LfuCache) Evict(ctx context.Context, count int)

func (*LfuCache) Exists

func (c *LfuCache) Exists(ctx context.Context, key string) bool

func (*LfuCache) Get

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

func (*LfuCache) Init

func (c *LfuCache) Init(clock Clock, capacity int)

func (*LfuCache) Remove

func (c *LfuCache) Remove(ctx context.Context, key string) bool

func (*LfuCache) Set

func (c *LfuCache) Set(ctx context.Context, key string, val interface{}, ttl time.Duration) error

type LoaderFunc

type LoaderFunc func(context.Context, string) (interface{}, error)

type LruCache

type LruCache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*LruCache) Evict

func (c *LruCache) Evict(ctx context.Context, count int)

func (*LruCache) Exists

func (c *LruCache) Exists(ctx context.Context, key string) bool

func (*LruCache) Get

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

func (*LruCache) Init

func (c *LruCache) Init(clock Clock, capacity int)

func (*LruCache) Remove

func (c *LruCache) Remove(ctx context.Context, key string) bool

func (*LruCache) Set

func (c *LruCache) Set(ctx context.Context, key string, val interface{}, ttl time.Duration) error

type MLoaderFunc

type MLoaderFunc func(context.Context, []string) (map[string]interface{}, error)

type Option

type Option func(*options)

func WithDefaultVal

func WithDefaultVal(defaultVal interface{}) Option

func WithLoaderFn

func WithLoaderFn(fn LoaderFunc) Option

func WithMLoaderFn

func WithMLoaderFn(fn MLoaderFunc) Option

func WithRedisClient

func WithRedisClient(client *redis.Client) Option

func WithRedisOptions

func WithRedisOptions(opts *redis.Options) Option

func WithTTL

func WithTTL(ttl time.Duration) Option

func WithUnSafeValBind

func WithUnSafeValBind(valPtrFunc valPtrFunc) Option

type RedisCli

type RedisCli struct {
	*redis.Client
}

type SimpleCache

type SimpleCache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*SimpleCache) Evict

func (c *SimpleCache) Evict(ctx context.Context, count int)

func (*SimpleCache) Exists

func (c *SimpleCache) Exists(ctx context.Context, key string) bool

func (*SimpleCache) Get

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

func (*SimpleCache) Init

func (c *SimpleCache) Init(clock Clock, capacity int)

func (*SimpleCache) Remove

func (c *SimpleCache) Remove(ctx context.Context, key string) bool

func (*SimpleCache) Set

func (c *SimpleCache) Set(ctx context.Context, key string, val interface{}, ttl time.Duration) error

Jump to

Keyboard shortcuts

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