Go implementation LRU Cache

Source: Internet
Author: User
Tags key string
This is a creation in Article, where the information may have evolved or changed.

1. LRU Introduction

1.1 Overview

Caching resources are often expensive, usually with a large amount of data, it is possible to satisfy as many accesses as possible from fewer caches, where there is a hypothetical, usually recently accessed data, which is likely to be subsequently continued to be accessed, based on this assumption, to sort all data by access time and to evict old data, The existence of cached data is hot data, which saves memory resources and greatly satisfies the access. The LRU (Least recently used) algorithm is an always-on cache substitution algorithm based on this hypothesis.

1.2 Algorithmic Flow

Assume that the cache size is 4, and that the write order is a B C D E d F. The access order is divided into write and read two operations, the write needs to update the access time, and when the data reaches the maximum cache need to evict the data, and read only update access time, write the displacement algorithm flow as shown.

When the cache size is not reached, all data is written to the store and the write order is recorded.
When writing E, the cache is full, and the value of e does not exist, and you need to evict the data A that is not accessed for the longest time, when the cache content is e D C B.
The next write D, D, in the cache, directly updates the access order of D, when the cache content is D E C B
The next write F, F is not in the cache, evict the end of C in the cache, at which time the cache content is F D E C

2 Go implementation

2.1 Ideas

Using go, you can use the list plus map to implement LRU cache, the specific idea is:
Write, the first query from the map, if you can query, if you can query the value, then the value of the list to move to the front. If the query does not have a value, determine whether the current map reaches the maximum value, and if the maximum value is reached, remove the value from the list, and delete the values in the map. If the map capacity does not reach the maximum value, write a map and place the value at the front of the list.

When reading, from the map query, if you can query the value, then directly in the list to move the value to the front, return the query results.

To ensure concurrency security, read-write locks are introduced.
In addition, there is a case where the content in the list is read in contrast to map, because declaring a container object holds both the key and the value, and the list and the map store a reference to the container object.
The introduction of atomic objects to statistics on the number of hits and misses

2.2 Key Codes

Full code See: Https://github.com/g4zhuj/cache

    • Set (write operation)
func (c *MemCache) Set(key string, value interface{}) {    c.mutex.Lock()    defer c.mutex.Unlock()    if c.cache == nil {        c.cache = make(map[interface{}]*list.Element)        c.cacheList = list.New()    }    //判断是否在map中,如果在map中,则将value从list中移动到前面.    if ele, ok := c.cache[key]; ok {        c.cacheList.MoveToFront(ele)        ele.Value.(*entry).value = value        return    }    //如果不再map中,将值存到List最前面    ele := c.cacheList.PushFront(&entry{key: key, value: value})    c.cache[key] = ele    //判断是否到达容量限制,到达容量限制时删除List中最后面的值.    if c.maxItemSize != 0 && c.cacheList.Len() > c.maxItemSize {        c.RemoveOldest()    }}
    • Get (read operation)
func (c *MemCache) Get(key string) (interface{}, bool) {    c.mutex.RLock()    defer c.mutex.RUnlock()    c.gets.Add(1)    //如果读取到值,移动在List中位置,并返回value    if ele, hit := c.cache[key]; hit {        c.hits.Add(1)        c.cacheList.MoveToFront(ele)        return ele.Value.(*entry).value, true    }    return nil, false}

3. Reference

Https://en.wikipedia.org/wiki ...

Related Article

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.