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
// 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")
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)
}
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
}