Go语言内存缓存本地缓存实现1. LRU缓存type LRUCache struct { capacity int cache map[int]*list.Element order *list.List } type entry struct { key int value int } func NewLRUCache(capacity int) *LRUCache { return LRUCache{ capacity: capacity, cache: make(map[int]*list.Element), order: list.New(), } } func (c *LRUCache) Get(key int) (int, bool) { if elem, ok : c.cache[key]; ok { c.order.MoveToFront(elem) return elem.Value.(*entry).value, true } return 0, false } func (c *LRUCache) Put(key, value int) { if elem, ok : c.cache[key]; ok { c.order.MoveToFront(elem) elem.Value.(*entry).value value return } if c.order.Len() c.capacity { oldest : c.order.Back() c.order.Remove(oldest) delete(c.cache, oldest.Value.(*entry).key) } elem : c.order.PushFront(entry{key: key, value: value}) c.cache[key] elem }2. 总结LRU缓存是本地缓存的经典实现Go语言可以通过标准库的list和map实现。