Skip to content

Import path: gitlab.soludian.com/soludian/fountain/libs/cache/slru_cache

slru_cache

go
import "gitlab.soludian.com/soludian/fountain/libs/cache/slru_cache"

Package cache implements a LRU cache.

The implementation borrows heavily from SmallLRUCache (originally by Nathan Schrenk). The object maintains a doubly-linked list of elements. When an element is accessed, it is promoted to the head of the list. When space is needed, the element at the tail of the list (the least recently used element) is evicted.

Index

Constants

go
const (
    KPackageName    = "slru_cache"
    DefaultCapacity = int64(10 * 1024)
)

Variables

go
var DefaultConfig = fmt.Sprintf(`{"capacity": %d}`, DefaultCapacity)

func NewSLRUCache

go
func NewSLRUCache() cache.Cache

NewSLRUCache creates a new empty cache with the given capacity.

type Cache

LRUCache is a typical LRU cache implementation. If the cache reaches the capacity, the least recently used item is deleted from the cache. Note the capacity is not the number of items, but the total sum of the Size() of each item.

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

func (*Cache) ClearAll

go
func (sc *Cache) ClearAll(ctx context.Context) error

Clear all cache.

func (*Cache) Close

go
func (rc *Cache) Close() error

Close

func (*Cache) Decr

go
func (sc *Cache) Decr(key string) (int64, error)

Decrement a cached int value by key, as a counter.

func (*Cache) Delete

go
func (sc *Cache) Delete(key string) error

Delete cached value by key. Should not return error if key not found

func (*Cache) Evictions

go
func (sc *Cache) Evictions() int64

func (*Cache) Get

go
func (rc *Cache) Get(key string) ([]byte, error)

Get gets a key's value from memcache.

func (*Cache) GetDefaultConfig

go
func (rc *Cache) GetDefaultConfig() string

func (*Cache) GetMulti

go
func (rc *Cache) GetMulti(keys []string) ([][]byte, error)

GetMulti gets one or keys values from ssdb.

func (*Cache) Has

go
func (sc *Cache) Has(key string) bool

Check cache existed by key.

func (*Cache) Incr

go
func (sc *Cache) Incr(key string) (int64, error)

Increment a cached int value by key, as a counter.

func (*Cache) Peek

go
func (sc *Cache) Peek(key string) (any, error)

Peek a cached value by key.

func (*Cache) PeekMulti

go
func (sc *Cache) PeekMulti(keys []string) ([]any, error)

PeekMulti is a batch version of Get.

func (*Cache) ProbationarySegmentLen

go
func (sc *Cache) ProbationarySegmentLen() int64

func (*Cache) ProtectedSegmentLen

go
func (sc *Cache) ProtectedSegmentLen() int64

func (*Cache) Put

go
func (sc *Cache) Put(key string, val any, ttl time.Duration) error

Set a cached value with key and expire time.

func (*Cache) PutMulti

go
func (sc *Cache) PutMulti(values map[string]any, ttl time.Duration) error

Set a cached values with key and expire time

func (*Cache) Reset

go
func (rc *Cache) Reset() error

func (*Cache) Set

go
func (rc *Cache) Set(key string, val []byte, ttl time.Duration) error

func (*Cache) SetMulti

go
func (rc *Cache) SetMulti(values map[string][]byte, ttl time.Duration) error

func (*Cache) SetName

go
func (sc *Cache) SetName(name string)

func (*Cache) Size

go
func (sc *Cache) Size() int64

is Size of all elements, NOT len of list elements

func (*Cache) StartAndGC

go
func (sc *Cache) StartAndGC(configs ...string) error

Start gc routine based on config string settings. config: must be in the format {"capacity": 1024}

type Item

Item is what is stored in the cache

go
type Item struct {
    Key   string
    Value Value
}

type Value

Value is the interface values that go into LRUCache need to satisfy

go
type Value interface {
    // Size returns how big this value is. If you want to just track
    // the cache by number of objects, you may return the size as 1.
    Size() int
}

Generated by gomarkdoc