博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
URL缓存机制
阅读量:3961 次
发布时间:2019-05-24

本文共 2175 字,大约阅读时间需要 7 分钟。

在这里插入图片描述

class LRUCache {
class Node {
int key; int value; Node pre; Node post; public Node() {
} public Node(int key, int value) {
this.key = key; this.value = value; } } private Map
map; private int size;//节点的个数 private int capacity;//缓存的容量 private Node head;//头节点 private Node tail;//未节点 public LRUCache(int capacity) {
map = new HashMap<>(); size = 0; this.capacity = capacity; head = new Node(); tail = new Node(); head.post = tail; tail.pre = head; } public int get(int key) {
Node cur = map.get(key); //当前节点不存在返回-1 if(cur == null) {
return -1; }else {
//存在就移到队列头部 moveToHead(cur); return cur.value; } } public void put(int key, int value) {
//获取该节点 Node cur = map.get(key); //存在 if(cur != null) {
//覆盖 cur.value = value; //移到对头 moveToHead(cur); }else {
//不存在就创建一个 Node newNode = new Node(key, value); //加进map map.put(key, newNode); //加到对头 addToHead(newNode); //数量加一 size++; if(size > capacity) {
//超过容量删除队尾元素 删除map的值 Node tail = removeTail(); map.remove(tail.key); size--; } } } public void moveToHead(Node node) {
removeNode(node); addToHead(node); } public void removeNode(Node node) {
node.pre.post = node.post; node.post.pre = node.pre; } public void addToHead(Node node) {
node.pre = head; node.post = head.post; head.post = node; node.post.pre = node; } public Node removeTail() {
Node res = tail.pre; removeNode(res); return res; }}/** * Your LRUCache object will be instantiated and called as such: * LRUCache obj = new LRUCache(capacity); * int param_1 = obj.get(key); * obj.put(key,value); */

转载地址:http://eqhzi.baihongyu.com/

你可能感兴趣的文章
Ruby1.9.2之——Require
查看>>
Ruby1.9.2之——关联Excel
查看>>
Ruby1.9.2之——生成HTML文件
查看>>
Watir2.0.1之——简介及实例
查看>>
Watir2.0.1之——屏幕截图
查看>>
Ruby+Watir经验谈: Understanding Watir
查看>>
watir + autoit3
查看>>
Ruby+Watir安装
查看>>
(原博客转移)诺基亚手机成板砖无法开机后,强刷修复手机系统的方法!本人亲测
查看>>
Ruby使用Win32API来操作鼠标
查看>>
代替Watir中click_no_wait的方法——left_click
查看>>
autoit3 ie.au3 函数之——_IE_Example、_IE_Introduction
查看>>
Android开发之——自定义标题栏titlebar
查看>>
autoit3 ie.au3 函数之——_IE_VersionInfo
查看>>
autoit3 ie.au3 函数之——_IEAction
查看>>
autoit3 ie.au3 函数之——_IEGetObjById、_IEGetObjByName
查看>>
autoit3 ie.au3 函数之——_IEAttach
查看>>
autoit3 ie.au3 函数之——_IEBodyReadHTML、_IEBodyWriteHTML
查看>>
autoit3 ie.au3 函数之——_IEBodyReadText
查看>>
autoit3 ie.au3 函数之——_IECreate
查看>>