This is a creation in Article, where the information may have evolved or changed.
LRU is usually implemented using the hash map + doubly linked list. In Golange, it's easy to use list to save data, map for quick access.
The following functions are implemented specifically:
Func newlrucache (Cap int) (*lrucache) func (LRU *lrucache) Set (K,v interface{}) (Error) func (LRU *lrucache) Get (K interface {}) (v Interface{},ret bool,err error) func (LRU *lrucache) Remove (k interface{}) (BOOL)
Demonstrate:
Package Main//lru Cache//author:xiong Chuan liang//date:2015-2-3import ("FMT" "github.com/xcltapestry/xclpkg/ Algorithm " ) func main () {LRU: = algorithm. Newlrucache (3) LRU. Set (Ten, "value1") LRU. Set ("value2") LRU. Set ("Value3") LRU. Set (Ten, "Value4") LRU. Set ("Value5") fmt. Println ("LRU Size:", LRU.) Size ()) V,ret,_: = LRU. Get () if RET {fmt. Println ("Get ():", v)}if LRU. Remove () {fmt. Println ("Remove (): true")}else{fmt. Println ("Remove (): false")}fmt. Println ("LRU Size:", LRU.) Size ())}
Operation Result:
LRU Size:3get (+): value3remove (+): Truelru size:2
Specific implementation of the source code:
Package Algorithm//lru Cache//author:xiong Chuan liang//date:2015-2-3//"Github.com/xcltapestry/xclpkg/algorithm" Import ("Container/list" "errors") type Cachenode struct {key,value interface{}}func (Cnode *cachenode) Newcachenode (k,v interface{}) *cachenode{return &cachenode{k,v}}type LRUCache struct {capacity intdlist *list. Listcachemap map[interface{}]*list. Element}func newlrucache (Cap int) (*lrucache) {return &lrucache{capacity:cap,dlist:list. New (), Cachemap:make (map[interface{}]*list. Element)}}func (LRU *lrucache) Size () (int) {return lru.dlist.Len ()}func (LRU *lrucache) Set (K,v interface{}) (Error) {if Lru.dlist = = Nil {return errors. New ("LRUCache struct not initialized.")} If Pelement,ok: = Lru.cachemap[k]; OK {Lru.dlist.MoveToFront (pelement) pelement.value. ( *cachenode). Value = Vreturn Nil}newelement: = Lru.dlist.PushFront (&cachenode{k,v}) lru.cachemap[k] = Newelementif Lru.dlist.Len () > LRU. Capacity {//Remove last lastelement: = Lru.dlist.Back () if lastelement = = Nil {return Nil}cachenode: = LasteLement. Value. (*cachenode) Delete (Lru.cachemap,cachenode.key) lru.dlist.Remove (lastelement)}return nil}func (LRU *lrucache) Get (k interface{}) (v Interface{},ret bool,err error) {if Lru.cachemap = = Nil {return v,false,errors. New ("LRUCache struct not initialized.")} If Pelement,ok: = Lru.cachemap[k]; OK {Lru.dlist.MoveToFront (pelement) return Pelement.value. ( *cachenode). Value,true,nil}return V,false,nil}func (LRU *lrucache) Remove (k interface{}) (bool) {if Lru.cachemap = nil {return false} If Pelement,ok: = Lru.cachemap[k]; OK {cachenode: = Pelement.value. ( *cachenode) Delete (Lru.cachemap,cachenode.key) lru.dlist.Remove (pelement) return True}return false}Note:
1.key recorded in map
2. Move to the linked header for Set/get added or hit elements
3. If the total number is greater than the cache capacity (CAP), the last element is removed.
Mail:xcl_168@aliyun.com
blog:http://blog.csdn.net/xcl168