首页 > 编程语言 >LRUCache算法缓存策略(map+doubleLinkedList)

LRUCache算法缓存策略(map+doubleLinkedList)

时间:2023-09-07 17:56:15浏览次数:35  
标签:Node map head node doubleLinkedList key null public LRUCache

package arithmetic;

import java.util.HashMap;

public class FaceTest81 {
//LRUcache缓存策略map+双向链表
//get、update、put需要时间复杂度达到O1
//map+双向链表结构

public FaceTest81(int capacity) {
	cache = new MyCache(capacity);
}
private MyCache<Integer,Integer> cache;

public int get(int key) {//复用get
	Integer ans = cache.get(key);
	return ans == null ? -1 : ans;
}
//没有就是新增,有就是修改
public void put(int key,int value) {
	cache.set(key,value);
}

//节点类型,包含键值,上一个节点和下一个节点
public static class Node<K,V> {
	public K key;
	public V value;
	public Node <K,V> last;//上一个节点
	public Node <K,V> next;//下一个节点
	public Node(K key,V value) {
		this.key = key;
		this.value=value;
	}
}


//双向链表
public static class NodeDoubleLinkedList<K,V>{
	private Node<K,V> head;
	private Node<K, V> tail;
	public NodeDoubleLinkedList() {
		head = null;
		tail = null;
	}
	
	//构建双向链表的增删改查的方法
	public void addNode(Node<K, V> newNode) {
		if (newNode == null) {
			return;
		}
		if (head == null) {
			head = newNode;
			tail = newNode;
		}else {
			tail.next = newNode;
			newNode.last=tail;
			tail = newNode;
		}
	}
	
	//将节点放入尾部
	public void moveNodeToTail(Node<K, V> node) {//每次操作都会调用
		if (this.tail == node) {//本来就是尾部,直接返回
			return;
		}
		//先将节点拿出来
		if (this.head == node) {//如果他是头部,直接让下一个当头
			this.head = node.next;
			this.head.last = null;
		} else {//
			node.last.next = node.next;
			node.next.last = node.last;
		}
		//将拿出来的节点节点放入尾部
		node.last = this.tail;
		node.next = null;
		this.head.last = null;
	}
	
	public Node<K, V> removeHead(){//超出容量的时候使用
		if (this.head == null) {
			return null;
		}
		//先将节点拿出来
		Node<K, V> res = this.head;
		//直接处理
		if (this.head == this.tail) {
			this.head = null;
			this.tail = null;
		}else {
			this.head = res.next;
			res.next = null;
			this.head.last = null;
		}
		return res;
	}
	
}
//建立缓存策略
public static class MyCache<K,V>{
	private HashMap<K, Node<K,V>> keyNodeMap;
	private NodeDoubleLinkedList<K,V> nodeList;
	private final int capacity;
	public MyCache(int cap) {
		keyNodeMap = new HashMap<K, Node<K,V>>();
		nodeList = new NodeDoubleLinkedList<K, V>();
		capacity = cap;
	}

	public V get(K key) {
		if (keyNodeMap.containsKey(key)) {
			Node<K, V> res = keyNodeMap.get(key);
			nodeList.moveNodeToTail(res);//取出来放入尾部
			return res.value;
		}
		return null;
	}
	//没有就是新增,有就是修改
	public void set(K key, V value) {
		//有就是修改
		if (keyNodeMap.containsKey(key)) {
			Node<K, V> node = keyNodeMap.get(key);
			node.value = value;
			nodeList.moveNodeToTail(node);
		}else {//新增
			Node<K, V> newNode = new Node<K, V>(key, value);
			keyNodeMap.put(key, newNode);
			nodeList.addNode(newNode);
			if (keyNodeMap.size() == capacity + 1) {
				remoceMostUnusedCache();
			}
		}
		
	}

	private void remoceMostUnusedCache() {//超出容量,移除不常用的头部
		Node<K, V> removenoNode = nodeList.removeHead();	
		keyNodeMap.remove(removenoNode.key);
	}
	
}

}

标签:Node,map,head,node,doubleLinkedList,key,null,public,LRUCache
From: https://www.cnblogs.com/15078480385zyc/p/17685678.html

相关文章

  • @supermap/iclient-leaflet使用tiledMapLayer报错
    使用leaflet加载超图的时候有时候超图无法加载有时候报如下错误因为手上有好几个项目都在使用leaflet但是同样都使用@supermap/iclient-leaflet(版本11.1.0-a)加载超图,有的项目可行,有的不可行最后打开项目根目录下node_modules里查看@supermap文件夹里的iclient-leafl......
  • Redis复习:(1)RedisTempalte之BitMap操作
    packagecn.edu.tju.service.impl;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.dao.DataAccessException;importorg.springframework.data.redis.connection.RedisConnection;importorg.springframework.data.redis.co......
  • map、sync.map、concurrent-map适用场景与源码解析
    最近一直加班,无论工作日还是周末,虽然每天很忙但总感觉空空的,很少有时间停下来思考与总结。项目中各种甩锅,最后最苦逼的还是落到了研发的头上,文档编写、环境部署、问题排查虐得一遍又一遍。事情杂乱,研发效率超级低,不知道何是是个头呀背景在go中,map是最常用的集合之一。其底层key存......
  • hibernate怎么实现一个类对象map多个表名
    1)映射文件在一个映射文件中定义class和table的对应关系,用entity-name来区分不同的映射:<class=”MyClass”entity-name=”testA”table=”mytable_A”><propertyname=”name”column=”st_name”/>……</class><class=”MyClass”entity-name=”testB”table=”mytable......
  • Hadoop Map/Reduce教程
    【目的】       这篇教程从用户的角度出发,全面地介绍了HadoopMap/Reduce框架的各个方面。【先决条件】       请先确认Hadoop被正确安装、配置和正常运行中。更多信息见:       Hadoop快速入门对初次使用者。       Hadoop集群搭建对大规模分布式......
  • HashMap、LinkedHashMap和TreeMap:你真的了解它们吗?
    亲爱的小伙伴们,大家好呀!我是小米,一个热衷于技术分享的90后程序员。今天我要和大家聊聊一个在面试中经常会被问到的话题:HashMap、LinkedHashMap、TreeMap的区别。这可是一个非常重要的知识点,不仅在面试中会被频繁提及,而且在实际开发中也经常用到。让我们一起深入了解这三者的异同吧!H......
  • ubuntu20安装colmap
     教程https://colmap.github.io/install.html 前提r900k 3070显卡cuda11.5opencv3.4.9如果有acoda先从环境变量去掉,以免导致多重库问题 起作用source~/.bashrcgcc11 g++ 11 安装sudoapt-getinstall\git\cmake\ninja-bui......
  • ArcGIS Map SDK FeatureLayer点击查询要素与弹框展示
    ArcGISMapSDKFeatureLayer点击查询要素与弹框展示代码如下:<htmllang="en"><head><metacharset="utf-8"/><metaname="viewport"content="initial-scale=1,maximum-scale=1,user-scalable=no&quo......
  • HashMap的put方法
    HashMap结构简略图调用put()函数,如果table为空,则说明调用HashMap的无参构造函数并且第一次调用put函数,putVal内部调用resize()函数设置容量和阈值分别为16和12;(n-1)&hash进行取模计算下标,n是2的次幂,则n-1的有效位全为1,方便快速计算publicVput(Kkey,Vvalue){......
  • SQLMAP流量分析
    本篇文章作者幽壑,本文属i春秋原创奖励计划,未经许可禁止转载。https://bbs.ichunqiu.com/thread-63533-1-1.htmlSQLMAP流量特征分析测试靶场:pikachu测试环境:PHP5.4、Apache2.0、MYSQL5.0.10测试工具:sqlmap1.5.6.3关于环境配置,记得将mysql的secure_file_priv设置为空。SHOW......