Go語言實現LRU演算法

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

         LRU 通常使用hash map + doubly linked list實現。在Golange中很簡單,使用List儲存資料,Map來做快速存取即可. 

具體實現了下面幾個函數:

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)

示範:

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(10,"value1")lru.Set(20,"value2")lru.Set(30,"value3")lru.Set(10,"value4")lru.Set(50,"value5")fmt.Println("LRU Size:",lru.Size())v,ret,_ := lru.Get(30)if ret  {fmt.Println("Get(30) : ",v)}if lru.Remove(30) {fmt.Println("Remove(30) : true ")}else{fmt.Println("Remove(30) : false ")}fmt.Println("LRU Size:",lru.Size())}

運行結果:

LRU Size: 3Get(30) :  value3Remove(30) : trueLRU Size: 2

具體的 實現源碼: 

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結構體未初始化.")}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 {//移掉最後一個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結構體未初始化.")}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}
 附註:

1.key記錄在map
2.對於set/get添加或命中的元素移到鏈表頭
3.如總個數大於Cache容量(cap),則將最末的元素移除.


MAIL: xcl_168@aliyun.com

BLOG:http://blog.csdn.net/xcl168


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.