localcache

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2021 License: MIT Imports: 6 Imported by: 0

README

What is go-localcache ?

A mem cache library by golang.

Why choose go-localcache ?

1、Support to set different TTL for every key

2、LRU policy to delete useless keys

3、Similar performance like sync.Map -> see bench_test

How to use

	cache := localcache.NewLocalCache(
		localcache.WithCapacity(1024), // WithShardCount set max Capacity
		localcache.WithShardCount(256),// WithShardCount shardCnt must be a power of 2
		localcache.WithGlobalTTL(120), // WithGlobalTTL set all keys default expire time of seconds
		localcache.WithStatist(true),  // WithStatist set whether need to caculate the cache stastic
		localcache.WithPolicy(localcache.PolicyTypeLRU), // WithPolicy set the elimination policy of key
	)
	
	// Get a key and return the value and if the key exists
	cache.Get(key string) (interface{}, bool)

	// GetOrLoad get a key, while key not exists, call f() to load data, and will set the load data to cache.
	// Load data process will called singleFlight called 
	cache.GetOrLoad(key string, f LoadFunc) (interface{}, error)

	// Set a key-value with default seconds to live
	cache.Set(key string, value interface{})
	
	// SetWithExpire set a key-value with seconds to live
	cache.SetWithExpire(key string, value interface{}, ttl int64)
	
	// Del delete key and return if the key exists
	cache.Del(key string) bool
	
	// Len return count of keys in cache
	cache.Len() int
	
	// Flush clear all keys in chache, should do this when set and del is stop
	cache.Flush()
	
	// Stop the cacheProcess by close stopChan
	cache.Stop()

	// Statistic return cache Statics {"hit":1, "miss":1, "hitRate":50.0}
	Statistic() map[string]interface{}

Documentation

Index

Constants

View Source
const (
	PolicyTypeLRU = "lru"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get a key and return the value and if the key exists
	Get(key string) (interface{}, bool)
	// GetOrLoad get a key, while not exists, call f() to load data
	GetOrLoad(key string, f LoadFunc) (interface{}, error)
	// Set a key-value with default seconds to live
	Set(key string, value interface{})
	// SetWithExpire set a key-value with seconds to live
	SetWithExpire(key string, value interface{}, ttl int64)
	// Del delete key
	Del(key string)
	// Len return count of keys in cache
	Len() int
	// Flush clear all keys in chache, should do this when set and del is stop
	Flush()
	// Stop the cacheProcess by close stopChan
	Stop()
	// Statistic return cache Statistic {"hit":1, "miss":1, "hitRate":50.0}
	Statistic() map[string]interface{}
}

func NewLocalCache

func NewLocalCache(options ...Option) Cache

NewLocalCache return Cache obj with options

type LoadFunc added in v0.0.7

type LoadFunc func() (interface{}, error)

LoadFunc is called to load data from user storage

type Option

type Option func(*localCache)

func WithCapacity

func WithCapacity(cap int) Option

WithShardCount set max Capacity

func WithGlobalTTL

func WithGlobalTTL(expireSecond int64) Option

WithGlobalTTL set all keys default expire time of seconds

func WithPolicy

func WithPolicy(policyType string) Option

WithPolicy set the elimination policy of keys

func WithShardCount

func WithShardCount(shardCnt int) Option

WithShardCount shardCnt must be a power of 2

func WithStatist

func WithStatist(needStatistic bool) Option

WithStatist set whether need to caculate the cache`s statist, default false.

not need may led performance a very little better ^-^

Directories

Path Synopsis
datastruct
list
Package list copy from datastruct.list what is more: 1、add a func NewElement() to support get a single Element whithout set prev and next and list 2、add a func PushElementFront() to insert the single element to first of the list which created by call NewElement()
Package list copy from datastruct.list what is more: 1、add a func NewElement() to support get a single Element whithout set prev and next and list 2、add a func PushElementFront() to insert the single element to first of the list which created by call NewElement()

Jump to

Keyboard shortcuts

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