lrucache

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

README

lrucache

golang impl lru cache reference leveldb

  • it's a lru chche
  • very easy to use
  • good performance
  • adapt most application scenario

this's (1<<num_shard_bits(<10)) hash table in cache; modify every hash table use sync.mutex;so it's provide good performance when memory use up to capacity; Earliest insert will be drop

how to use

method 1
    	// use get set delete (just provide string type support)
    	lru := NewLRUCache(1024*1024/*capacity*/, 0/*num shard bits*/) // num_shard_bit is 0, code will auto make one
		
    	lru.Put("key", "value")
    	value := lru.Get("key")
    	//displayed remove
    	lru.Delete("key")
method 2

	lru := NewLRUCache(1024*1024 /*capacity*/, 0 /*num shard bits*/) // num_shard_bit is 0, code will auto make one

	key := []byte("key")
	type V struct {a int; b int}
	value := V{4, 5}

	lru.Insert(key, value, uint64(len(key)+4*2), func(key []byte, entry interface{}) {
		fmt.Println("key:%s will be deleted from cache", key)
	})
	origin := lru.Lookup(key)
	if origin != nil {
		origin_value := origin.(V)
		fmt.Println(origin_value)
	}
method 3; use like redis incr;but merge return old value

	key := []byte("key")
	var merge_value int = 1
	lru := NewLRUCache(1024, 1)
	for i := 0; i < 1000; i++ {
		lru.Merge(key, merge_value, 4, IntMergeOperator, IntChargeOperator) // real value = value+1
	}
more use case, you can see lrucache_test.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HashSlice

func HashSlice(key []byte) uint32

Types

type Cache

type Cache interface {
	Put(key string, value string)
	Get(key string) (string, bool)
	Delete(key string)
	NewId() uint64
	Prune()
	TotalCharge() uint64

	Insert(key []byte, entry interface{}, charge uint64, deleter DeleteCallback)
	Lookup(key []byte) interface{}
	Remove(key []byte) interface{}
	Merge(key []byte, entry interface{}, charge uint64, merge_opt MergeOperator, charge_opt ChargeOperator) (old_entry interface{})
	ApplyToAllCacheEntries(TravelEntryOperator)
}

type ChargeOperator

type ChargeOperator func(entry interface{}, old_charge, new_charge uint64) uint64
var Int64ChargeOperator ChargeOperator = func(entry interface{}, old_charge, new_charge uint64) uint64 {
	var a int64
	return uint64(unsafe.Sizeof(a))
}
var IntChargeOperator ChargeOperator = func(entry interface{}, old_charge, new_charge uint64) uint64 {
	var a int
	return uint64(unsafe.Sizeof(a))
}

type DeleteCallback

type DeleteCallback func(key []byte, entry interface{})

type HandleTable

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

func NewLRUHandleTable

func NewLRUHandleTable() *HandleTable

func (*HandleTable) ApplyToAllCacheEntries

func (this *HandleTable) ApplyToAllCacheEntries(travel_fun TravelEntryOperator)

func (*HandleTable) Insert

func (this *HandleTable) Insert(e *LRUHandle) *LRUHandle

*

when not find return nil;
else replace handl and return old handle

func (*HandleTable) Lookup

func (this *HandleTable) Lookup(key []byte, hash uint32) *LRUHandle

func (*HandleTable) Remove

func (this *HandleTable) Remove(key []byte, hash uint32) *LRUHandle

func (*HandleTable) Resize

func (this *HandleTable) Resize()

type LRUCache

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

impl of interface Cache

func NewLRUCache

func NewLRUCache(capacity uint64, num_shard_bits uint) *LRUCache

func (*LRUCache) ApplyToAllCacheEntries

func (this *LRUCache) ApplyToAllCacheEntries(travel_fun TravelEntryOperator)

func (*LRUCache) Delete

func (this *LRUCache) Delete(key string)

func (*LRUCache) Get

func (this *LRUCache) Get(key string) (string, bool)

func (*LRUCache) Insert

func (this *LRUCache) Insert(key []byte, entry interface{}, charge uint64, deleter DeleteCallback)

func (*LRUCache) Lookup

func (this *LRUCache) Lookup(key []byte) interface{}

func (*LRUCache) Merge

func (this *LRUCache) Merge(key []byte, entry interface{}, charge uint64, merge_opt MergeOperator, charge_opt ChargeOperator) interface{}

func (*LRUCache) NewId

func (this *LRUCache) NewId() uint64

func (*LRUCache) Prune

func (this *LRUCache) Prune()

func (*LRUCache) Put

func (this *LRUCache) Put(key, value string)

func (*LRUCache) Remove

func (this *LRUCache) Remove(key []byte) interface{}

func (*LRUCache) SetCapacity

func (this *LRUCache) SetCapacity(capacity uint64)

func (*LRUCache) TotalCharge

func (this *LRUCache) TotalCharge() uint64

type LRUCacheShard

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

func NewLRUCacheShard

func NewLRUCacheShard(capacity uint64) *LRUCacheShard

func (*LRUCacheShard) ApplyToAllCacheEntries

func (this *LRUCacheShard) ApplyToAllCacheEntries(travel_fun TravelEntryOperator)

func (*LRUCacheShard) EvictLRU

func (this *LRUCacheShard) EvictLRU()

func (*LRUCacheShard) Insert

func (this *LRUCacheShard) Insert(key []byte, hash uint32, entry interface{}, charge uint64, deleter DeleteCallback) error

* create lruhandle and Insert to cache,

func (*LRUCacheShard) Lookup

func (this *LRUCacheShard) Lookup(key []byte, hash uint32) interface{}

* find key's lruhandle, return nil if not find;

func (*LRUCacheShard) Merge

func (this *LRUCacheShard) Merge(key []byte, hash uint32, entry interface{}, charge uint64, merge MergeOperator, charge_opt ChargeOperator) interface{}

func (*LRUCacheShard) Prune

func (this *LRUCacheShard) Prune()

func (*LRUCacheShard) Remove

func (this *LRUCacheShard) Remove(key []byte, hash uint32) interface{}

func (*LRUCacheShard) SetCapacity

func (this *LRUCacheShard) SetCapacity(capacity uint64)

func (*LRUCacheShard) TotalCharge

func (this *LRUCacheShard) TotalCharge() uint64

type LRUHandle

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

type MergeOperator

type MergeOperator func(old_entry, new_entry interface{}) interface{}
var Int64MergeOperator MergeOperator = func(old_entry, new_entry interface{}) interface{} {
	if old_entry == nil {
		return new_entry
	}

	old, ok_old := old_entry.(int64)
	new, ok_new := new_entry.(int64)
	if !ok_old || !ok_new {
		panic("error of merge type, old:" + reflect.TypeOf(old_entry).Name() +
			"  new:" + reflect.TypeOf(new_entry).Name())
	}
	res := old + new
	return res
}
var IntMergeOperator MergeOperator = func(old_entry, new_entry interface{}) interface{} {
	if old_entry == nil {
		return new_entry
	}

	old, ok_old := old_entry.(int)
	new, ok_new := new_entry.(int)
	if !ok_old || !ok_new {
		panic("error of merge type, old:" + reflect.TypeOf(old_entry).Name() +
			"  new:" + reflect.TypeOf(new_entry).Name())
	}
	res := old + new
	return res
}

type TravelEntryOperator

type TravelEntryOperator func(key []byte, entry interface{})

Jump to

Keyboard shortcuts

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