lruttl

package module
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2022 License: Apache-2.0 Imports: 7 Imported by: 2

README

lru-ttl

Build Status

LRU cache with ttl. It's useful for short ttl cache. L2Cache use lru cache as first cache, and slow cache as second cache. Lru cache should be set max entries for less memory usage but faster, slow cache is slower but larger capacity.

LRU TTL

cache := lruttl.New(1000, 60 * time.Second)
cache.Add("tree.xie", "my data")
data, ok := cache.Get("tree.xie")
cache.Remove("tree.xie")
cache.Add("tree.xie", "my data", time.Second)

L2Cache

// redisCache
l2 := lruttl.NewL2Cache(redisCache, 200, 10 * time.Minute)
ctx := context.Background()
err := l2.Set(ctx, "key", &map[string]string{
    "name": "test",
})
fmt.Println(err)
m := make(map[string]string)
err = l2.Get(ctx, "key", &m)
fmt.Println(err)
fmt.Println(m)

Ring

ringCache := lruttl.NewRing(lruttl.RingCacheParams{
    Size:       10,
    MaxEntries: 1000,
    DefaultTTL: time.Minute,
})
lruCache := ringCache("key")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidType = errors.New("invalid type")

ErrInvalidType is the error of invalid type

View Source
var ErrIsNil = errors.New("cache is nil")

ErrIsNil is the error of nil cache

View Source
var ErrKeyIsNil = errors.New("key is nil")

ErrKeyIsNil is the error of nil key

Functions

func BufferMarshal added in v0.3.2

func BufferMarshal(result interface{}) ([]byte, error)

BufferMarshal converts *bytes.Buffer to bytes, it returns a ErrInvalidType if restult is not *bytes.Buffer

func BufferUnmarshal added in v0.3.2

func BufferUnmarshal(data []byte, result interface{}) error

BufferUnmarshal writes the data to buffer, it returns a ErrInvalidType if restult is not *bytes.Buffer

func CPUTicks added in v0.5.0

func CPUTicks() int64

CPUTicks is a faster alternative to NanoTime to measure time duration.

func FastRand added in v0.5.0

func FastRand() uint32

FastRand is a fast thread local random function.

func MemHash added in v0.5.0

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 added in v0.5.0

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 Memclr added in v0.5.0

func Memclr(b []byte)

func NanoTime added in v0.5.0

func NanoTime() int64

NanoTime returns the current time in nanoseconds from a monotonic clock.

func NewRing added in v0.5.0

func NewRing(params RingCacheParams, opts ...CacheOption) *ringCache

NewRing returns a new ring cache

Types

type Cache

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

func New

func New(maxEntries int, defaultTTL time.Duration, opts ...CacheOption) *Cache

New returns a new lru cache with ttl

func (*Cache) Add

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

Add adds a value to the cache, it will use default ttl if the ttl is nil.

func (*Cache) Get

func (c *Cache) Get(key Key) (interface{}, bool)

Get returns value and exists from the cache by key, if value is expired then remove it. If the value is expired, value is not nil but exists is false.

func (*Cache) GetBytes added in v1.4.0

func (c *Cache) GetBytes(key Key) ([]byte, bool)

GetBytes is the same as Get function, but returns []byte

func (*Cache) Keys added in v0.2.0

func (c *Cache) Keys() []Key

Keys gets all keys of cache

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of items in the cache.

func (*Cache) Peek added in v0.4.0

func (c *Cache) Peek(key Key) (interface{}, bool)

Peek get a key's value from the cache, but not move to front. The performance is better than get. It will not be removed if the cache is expired.

func (*Cache) PeekBytes added in v1.4.0

func (c *Cache) PeekBytes(key Key) ([]byte, bool)

PeekBytes is the same as Peek function, but returns []byte

func (*Cache) Remove

func (c *Cache) Remove(key Key)

Remove removes the key's value from the cache.

func (*Cache) TTL

func (c *Cache) TTL(key Key) time.Duration

TTL returns the ttl of key

type CacheOption added in v0.4.1

type CacheOption func(c *Cache)

CacheOption cache option

func CacheEvictedOption added in v0.4.1

func CacheEvictedOption(fn func(key Key, value interface{})) CacheOption

CacheEvictedOption sets evicted function to cache

type Key added in v0.2.0

type Key interface{}

type L2Cache added in v0.3.0

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

A l2cache for frequently visited data

func NewL2Cache added in v0.3.0

func NewL2Cache(slowCache SlowCache, maxEntries int, defaultTTL time.Duration, opts ...L2CacheOption) *L2Cache

NewL2Cache return a new L2Cache, it returns panic if maxEntries or defaultTTL is nil

func (*L2Cache) Del added in v1.1.0

func (l2 *L2Cache) Del(ctx context.Context, key string) (int64, error)

Del deletes data from lru cache and slow cache

func (*L2Cache) Get added in v0.3.0

func (l2 *L2Cache) Get(ctx context.Context, key string, result interface{}) error

Get gets data from lru cache first, if not exists, then gets the data from slow cache. Use unmarshal function coverts the data to result

func (*L2Cache) GetBytes added in v1.3.0

func (l2 *L2Cache) GetBytes(ctx context.Context, key string) ([]byte, error)

GetBytes gets data from lur cache first, if not exists, then gets the data from slow cache.

func (*L2Cache) GetIgnoreNilErr added in v1.5.0

func (l2 *L2Cache) GetIgnoreNilErr(ctx context.Context, key string, result interface{}) error

Get gets data from lru cache first, if not exists, then gets the data from slow cache. Use unmarshal function coverts the data to result. It will not return nil error.

func (*L2Cache) Set added in v0.3.0

func (l2 *L2Cache) Set(ctx context.Context, key string, value interface{}, ttl ...time.Duration) error

Set converts the value to bytes, then sets it to lru cache and slow cache

func (*L2Cache) SetBytes added in v1.3.0

func (l2 *L2Cache) SetBytes(ctx context.Context, key string, value []byte, ttl ...time.Duration) error

SetBytes sets data to lru cache and slow cache

func (*L2Cache) TTL added in v0.5.0

func (l2 *L2Cache) TTL(ctx context.Context, key string) (time.Duration, error)

TTL returns the ttl for key

type L2CacheMarshal added in v1.1.0

type L2CacheMarshal func(v interface{}) ([]byte, error)

type L2CacheOption added in v0.4.1

type L2CacheOption func(c *L2Cache)

L2CacheOption l2cache option

func L2CacheMarshalOption added in v0.4.1

func L2CacheMarshalOption(fn L2CacheMarshal) L2CacheOption

L2CacheMarshalOption sets custom marshal function for l2cache

func L2CacheNilErrOption added in v1.5.0

func L2CacheNilErrOption(nilErr error) L2CacheOption

L2CacheNilErrOption set nil error for l2cache

func L2CachePrefixOption added in v0.4.1

func L2CachePrefixOption(prefix string) L2CacheOption

L2CachePrefixOption sets prefix for l2cache

func L2CacheUnmarshalOption added in v0.4.1

func L2CacheUnmarshalOption(fn L2CacheUnmarshal) L2CacheOption

L2CacheUnmarshalOption sets custom unmarshal function for l2cache

type L2CacheUnmarshal added in v1.1.0

type L2CacheUnmarshal func(data []byte, v interface{}) error

type RingCacheParams added in v0.5.0

type RingCacheParams struct {
	// ring size
	Size int
	// max entries
	MaxEntries int
	// default ttl
	DefaultTTL time.Duration
}

type SlowCache added in v0.3.0

type SlowCache interface {
	Get(ctx context.Context, key string) ([]byte, error)
	Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
	TTL(ctx context.Context, key string) (time.Duration, error)
	Del(ctx context.Context, key string) (int64, error)
}

Jump to

Keyboard shortcuts

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