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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
| package com.kael.modules.data;
import org.jetbrains.annotations.NotNull;
import java.util.*; import java.util.concurrent.DelayQueue; import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit;
public class LRUTest {
public static void main(String[] args) {
LRUTest lruTest = new LRUTest(5); lruTest.put(1,1,0); lruTest.put(2,2,1); lruTest.put(6,6,1); lruTest.print(); lruTest.put(3,3,10); lruTest.print(); try { Thread.sleep(3000); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println("");
lruTest.get(1); lruTest.print(); }
private Map<Integer,Node> map ;
private Node head;
private Node tail;
private int cap;
private DelayQueue queue = new DelayQueue<Node>();
public LRUTest(int cap){ this.map = new HashMap<>(); this.head = new Node(0,0); this.tail = new Node(0,0); head.next = tail; tail.pre = head; this.cap = cap;
new Thread(()->{ while (true){ try { Node take = (Node) queue.take(); this.removeNode(take); } catch (InterruptedException e) { throw new RuntimeException(e); }
} }).start(); }
public void addTail(Node node){ Node tailPre = tail.pre;
tail.pre = node; node.next = tail; node.pre = tailPre;
tailPre.next = node; }
public void popToTail(Node node){ Node nodeNext = node.next; Node nodePre = node.pre;
nodePre.next = nodeNext; nodeNext.pre = nodePre;
Node tailPre = tail.pre; tailPre.next = node; node.pre = tailPre; node.next = tail;
tail.pre = node;
}
public synchronized void removeNode(Node node){ if(map.get(node.key)!=null){ System.out.println("del node:"+node); Node nodeNext = node.next; Node nodePre = node.pre; nodePre.next = nodeNext; nodeNext.pre = node.pre; map.remove(node.key); } }
public void removeFirst(){ Node firstNode = head.next; Node sce = firstNode.next; head.next = sce; sce.pre = head;
queue.remove(firstNode); }
public synchronized Object get(int id){ if(map.get(id)!=null){
Node node = map.get(id); queue.remove(node); node.refreshExpire(node.expire);
popToTail(node); queue.put(node); return map.get(id).value; } return null; }
public synchronized void put(int id, Object value,int expire){
if(map.get(id)==null){ Node node = new Node(id, value,expire); if(cap<=map.size()){ removeFirst(); map.remove(id); } addTail(node); map.put(id,node); if(expire>0){ queue.put(node); } } else { Node node = (Node) map.get(id); popToTail(node); if(expire>0){ queue.remove(node); node.refreshExpire(expire); queue.put(node); } }
}
public void print(){ Node node = head; while (node.next!=null){ System.out.print(node.value + " -> "); node = node.next; } System.out.println(); }
class Node implements Delayed { public int expire; public long expireTime;
public Integer key; public Object value; public Node pre; public Node next;
public Node(Integer key, Object value) { this.key = key; this.value = value; }
public Node(Integer key, Object value,int expire) { this.key = key; this.value = value; this.expire = expire; this.expireTime = new Date().getTime() + expire*1000; }
public void refreshExpire(int expire){ this.expire = expire; this.expireTime = new Date().getTime() + expire*1000; }
@Override public long getDelay(@NotNull TimeUnit unit) { Date date = new Date();
return unit.convert(expireTime-date.getTime(),TimeUnit.MILLISECONDS); }
@Override public int compareTo(@NotNull Delayed o) { if(o instanceof Node){ Node node = (Node)o; if(expireTime > node.expireTime){ return 1; } if(expireTime <= node.expireTime){ return -1; } } return 0; }
@Override public String toString() { return "Node{" + "expire=" + expire + ", expireTime=" + expireTime + ", key=" + key + ", value=" + value + '}'; } }
}
|