码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • c++ LRU(最近最少使用)缓存机制


    // LRU(最近最少使用)缓存机制
    #ifndef _ZD_LRU_CACHE_H_
    #define _ZD_LRU_CACHE_H_
    
    #include 
    #include 
    #include 
    
    class ZDLRUCahce
    {
    public:
    	ZDLRUCahce(int capacity)
    		: m_capacity(capacity)
    	{
    	}
    
    	~ZDLRUCahce()
    	{
    	}
    
    	// 1.key不存在,返回-1
    	// 2.key存在,list中删除key,在list头部重新插入,更新map
    	int Get(int key)
    	{
    		RecursiveGuard mtx(m_recursive_mutex);
    		auto it = m_mapCache.find(key);
    		if (it == m_mapCache.end())
    			return -1;
    
    		std::pair<int, int> kv = *m_mapCache[key];
    		m_listCache.erase(m_mapCache[key]);
    		m_listCache.push_front(kv);
    		m_mapCache[key] = m_listCache.begin();
    		return kv.second;
    	}
    
    	// 1.key存在:删除该key,在list头部插入数据,更新map
    	// 2.key不存在
    	 2.1 容量未满:在list头部插入数据,更新map
    	 2.2 容量已满:删除list尾部数据,在list头部重新插入数据,,更新map
    	void Put(int key, int value)
    	{
    		RecursiveGuard mtx(m_recursive_mutex);
    		auto it = m_mapCache.find(key);
    		if (it != m_mapCache.end())
    		{
    			m_listCache.erase(m_mapCache[key]);
    			m_listCache.push_front(std::make_pair(key, value));
    			m_mapCache[key] = m_listCache.begin();
    			return;
    		}
    
    		if (m_listCache.size() == m_capacity)
    		{
    			auto iter = m_listCache.back();
    			m_mapCache.erase(iter.first);
    			m_listCache.pop_back();
    			m_listCache.push_front(std::make_pair(key, value));
    			m_mapCache[key] = m_listCache.begin();
    		}
    		else
    		{
    			m_listCache.push_front(std::make_pair(key, value));
    			m_mapCache[key] = m_listCache.begin();
    		}
    	}
    
    private:
    	int m_capacity;
    
    	typedef std::lock_guard<std::recursive_mutex> RecursiveGuard;
    	typedef std::list<std::pair<int, int>> Cache;
    	typedef std::unordered_map<int, Cache::iterator> CacheMap;
    	std::recursive_mutex m_recursive_mutex;
    	Cache      m_listCache;
    	CacheMap   m_mapCache;
    };
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
  • 相关阅读:
    【架构方法论(一)】架构的定义与架构要解决的问题
    spring IOC 概念理解
    java计算机毕业设计瀚绅睿茨二人二轮车租赁管理源码+mysql数据库+系统+lw文档+部署
    重置(resetting)CSS 和 标准化(normalizing)CSS 的区别是什么?你会选择哪种方式,为什么?
    数据结构排序算法之直接插入排序与希尔排序【图文详解】
    未履行数据保护义务造成数据泄露,某大药房被罚110万
    探索数据结构世界之排序篇章(超级详细,你想看的都有)
    DevOps-Jenkins-CI持续集成操作
    [MySQL]order by失效
    5分钟搞懂https原理
  • 原文地址:https://blog.csdn.net/www_dong/article/details/134563174
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号