Go language Implementation LRU algorithm

Source: Internet
Author: User
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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.