scache

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2020 License: MIT Imports: 7 Imported by: 1

README

scache

Cache library for golang. It supports expirable cache: LRU

Install

go get github.com/khevse/scache

Examples

Simple:

c, err := scache.New(100, 10000).TTL(time.Hour).LRU().Build()

With loader function:

loadFunc = func(key interface{}) (value interface{}, err error) {
    ... load from DB
    value = rowData
    return
}

c, err := scache.New(10, 10000).LRU().LoaderFunc(loadFunc).Build()

From configuration:

conf := &scache.Config{
    Shards:       2,
    MaxSize:      4,
    Kind:         scache.scache.KindLRU,
    TTL:          time.Hour,
    ItemsToPrune: 10,
}

loadFunc := func(key interface{}) (val interface{}, err error) {
    val = key
    return
}

c, err := scache.FromConfig(conf).LoaderFunc(loadFunc).Build()

Benchmarks

The benchmarks can be found in page

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound             = errors.New("not found")
	ErrKeyIsNil             = errors.New("key is nil")
	ErrInvlidKeyTypeForHash = errors.New("invalid key type for hash function")
)

Functions

func FromConfig

func FromConfig(conf *Config) *builder

func New

func New(shards int, maxSize int64) *builder

Types

type Cache

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

func (*Cache) Close

func (c *Cache) Close()

func (*Cache) Count

func (c *Cache) Count() (count int64)

func (*Cache) Del

func (c *Cache) Del(key interface{}) (ok bool)

func (*Cache) Get

func (c *Cache) Get(key interface{}) (value interface{}, err error)

func (*Cache) Set

func (c *Cache) Set(key interface{}, value interface{})

func (*Cache) SetExp

func (c *Cache) SetExp(key interface{}, value interface{}, ttl time.Duration)

type Config

type Config struct {
	Kind         Kind
	TTL          time.Duration
	MaxSize      int64
	Shards       int
	ItemsToPrune uint32
}

type ICache

type ICache interface {
	// Set value with default lifetime(optional)
	Set(key interface{}, value interface{})
	// Set value with custom lifetime time
	SetExp(key interface{}, value interface{}, ttl time.Duration)
	Get(key interface{}) (value interface{}, err error)
	Del(key interface{}) bool
	Count() int64
	Close()
}

type Kind

type Kind int
const (
	KindUnknown Kind = iota
	KindLRU
)

type LoadFunc

type LoadFunc func(key interface{}) (value interface{}, err error)

Jump to

Keyboard shortcuts

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