LRU算法简介
1.iota简介2.go generate命令简介3.golang uuid库介绍4.gRPC基本教程5.protolator简介6.Govulncheck v1.0.0 发布了!7.每日一库:GORM简介8.Go泛型简介9.Go 1.21发布!10.Golang zip压缩文件读写操作11.本地搭建playground12.zap自定义日志级别13.Gorm日志设置14.gin启动https支持15.Go Plugin介绍16.Golang漏洞管理17.可以丢掉123456了18.如何优雅地退出程序19.PGO前瞻20.go多版本管理21.openAI发布v0.2.0了22.Gin中间件开发23.Fabric区块链浏览器(1)24.每日一库:Memcache25.protojson简介26.每日一库:gosec27.Fabric区块链浏览器(2)28.每日一库:fsnotify简介29.gRPC with JWT30.embed简介31.Fabric区块链浏览器(3)32.每日一库:pprof简介33.go 1.21:cmp34.完全可复制、经过验证的 Go 工具链35.PGO in Go 1.2136.Fabric 2.x 智能合约开发记录37.为不断增长的Go生态系统扩展gopls38.每日一库:lumberjack -- 日志轮换和管理39.2023-04-26-微信安全模式下消息解析40.WASI support in Go41.每日一库:Prometheus42.如何实现流量控制和熔断降级?43.消息队列 - RabbitMQ44.Go 1.22 中的 For 循环45.设计模式之单例模式46.每日一库:使用Viper处理Go应用程序的配置47.使用 gopkg.in/yaml.v3 解析 YAML 数据48.在Go中如何实现并发49.解析类型参数50.设计模式之工厂模式51.每日一库:cobra 简介52.slices in Go 1.2153.go defer简介54.slice简介55.Golang Map底层实现简述56.Go 如何实现多态57.查找数组中第K大的元素58.go中的内存逃逸59.数组 vs. 切片60.队列(Queue):先进先出(FIFO)的数据结构61.go 中如何实现定时任务62.go 中的循环依赖63.Go中字符串处理:fmt.Sprintf与string.Builder的比较64.理解Go中的零值65.go 上下文:context.Context66.go中异常处理流程67.Go实现网络代理68.Why gRPC ?69.Go:条件控制语句70.Go 获取 IP 地址71.Golang并发控制方式有几种?72.Golang面试:泛型
73.LRU算法简介
74.MRU算法实现75.ARC算法实现76.Go语言中的交互式CLI开发:survey库简介77.C如何调用GoLRU(Least Recently Used,最近最少使用)算法是一种常用于缓存管理的算法,用于在缓存空间有限的情况下,决定哪些数据应该被移除。它的基本思想是:如果一个数据最近被访问过,那么在将来一段时间内它被再次访问的概率较高。因此,当缓存已满,需要移除数据时,优先移除那些最近最少被使用的数据。
LRU算法的基本概念
- 缓存命中(Cache Hit):当访问的数据已经在缓存中时,称为缓存命中。
- 缓存未命中(Cache Miss):当访问的数据不在缓存中,需要从外部存储加载数据时,称为缓存未命中。
- 缓存替换(Cache Replacement):当缓存已满,需要替换掉某些数据以腾出空间时,称为缓存替换。
LRU算法的实现
LRU算法的实现通常需要两个数据结构:
- 哈希表(Hash Table):用于快速访问缓存中的数据。
- 双向链表(Doubly Linked List):用于维护数据的使用顺序,链表头部是最近使用的数据,尾部是最久未使用的数据。
LRU缓存的操作
- 访问数据:
- 如果数据在缓存中(缓存命中),将其移动到链表头部。
- 如果数据不在缓存中(缓存未命中),将数据插入到链表头部。
- 如果缓存已满,移除链表尾部的数据,以腾出空间。
- 插入数据:
- 将数据插入到链表头部。
- 更新哈希表,使其指向新节点。
- 如果缓存已满,移除链表尾部的数据,并在哈希表中删除相应的项。
Go示例
package lru import ( "container/list" "sync" ) type LRUCache struct { mtx sync.Mutex // protects the cache capacity int // capacity of the cache cache map[any]*list.Element // nearly O(1) lookups list *list.List // O(1) insert, update, delete } // NewLRUCache creates a new LRU cache with the given capacity. func NewLRUCache(capacity int) *LRUCache { return &LRUCache{ capacity: capacity, cache: make(map[any]*list.Element), list: list.New(), } } // Contains checks if the given item is in the cache. // This function is safe for concurrent access. func (c *LRUCache) Contains(item any) bool { c.mtx.Lock() defer c.mtx.Unlock() node, exists := c.cache[item] if exists { c.list.MoveToFront(node) } return exists } // Get returns the item from the cache. // This function is safe for concurrent access. func (c *LRUCache) Get(item any) any { node, exists := c.cache[item] if exists { c.mtx.Lock() c.list.MoveToFront(node) c.mtx.Unlock() return node.Value } else { c.Add(item) return item } } // Add adds the given item to the cache. // This function is safe for concurrent access. func (c *LRUCache) Add(item any) { c.mtx.Lock() defer c.mtx.Unlock() // if capacity is 0, nothing can be added, so just return if c.capacity == 0 { return } // check if the item is already in the cache if node, exists := c.cache[item]; exists { c.list.MoveToFront(node) return } // if the cache is full, remove the last element if c.list.Len() == c.capacity { last := c.list.Back() c.list.Remove(last) delete(c.cache, last.Value) } // add the new item to the front of the list node := c.list.PushFront(item) c.cache[item] = node } // Delete removes the given item from the cache if exists. // This function is safe for concurrent access. func (c *LRUCache) Delete(item any) { c.mtx.Lock() defer c.mtx.Unlock() // check if the item is already in the cache if node, exists := c.cache[item]; exists { c.list.Remove(node) delete(c.cache, item) } } // Len returns the number of items in the cache. // This function is safe for concurrent access. func (c *LRUCache) Len() int { c.mtx.Lock() defer c.mtx.Unlock() return c.list.Len() }

声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)进行许可,使用时请注明出处。
Author: mengbin
blog: mengbin
Github: mengbin92
cnblogs: 恋水无意
腾讯云开发者社区:孟斯特
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程