LRUCache.java
3.48 KB
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
package com.zhaoonline.coupen.cache;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* Created by ZhaoOnline<br/>
* User: yangyoupeng<br/>
* Date: 2016/12/15<br/>
* Time: 17:54<br/>
* Description:
*/
public class LRUCache<T> {
private ConcurrentHashMap<String, T> caches = null;
private CacheEntry<T> first;
private CacheEntry<T> last;
private int capacity = 0;
public LRUCache() {
this(200);
}
public LRUCache(int capacity) {
this.capacity=capacity;
this.caches = new ConcurrentHashMap<String,T>(capacity);
}
public synchronized T put(String key, T value) {
CacheEntry<T> entry = getEntry(key);
if (entry == null) {
if (caches.size() >= capacity) {
caches.remove(last.key);
removeLast();
}
entry = new CacheEntry<T>();
entry.key = key;
}else{
entry.value=null;
}
entry.value = value;
moveToFirst(entry);
return caches.put(key, value);
}
public int getSize() {
return caches.size();
}
public synchronized T get(String key) {
CacheEntry<T> entry = getEntry(key);
if (entry == null){
return null;
}
moveToFirst(entry);
return entry.value;
}
public synchronized void remove(String key) {
CacheEntry<T> entry = getEntry(key);
if (entry != null) {
if (entry.pre != null)
entry.pre.next = entry.next;
if (entry.next != null)
entry.next.pre = entry.pre;
if (entry == first)
first = entry.next;
if (entry == last)
last = entry.pre;
}
caches.remove(key);
}
private void removeLast() {
if (last != null) {
last = last.pre;
if (last == null)
first = null;
else
last.next = null;
}
}
private void moveToFirst(CacheEntry<T> entry) {
if (entry == first)
return;
if (entry.pre != null)
entry.pre.next = entry.next;
if (entry.next != null)
entry.next.pre = entry.pre;
if (entry == last)
last = last.pre;
if (first == null || last == null) {
first = last = entry;
return;
}
entry.next = first;
first.pre = entry;
first = entry;
entry.pre = null;
}
private CacheEntry<T> getEntry(String key) {
T item = caches.get(key);
if (item == null) {
return null;
}
CacheEntry<T> entry = new CacheEntry<T>();
entry.key = key;
entry.value = item;
return entry;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
CacheEntry<T> entry = first;
while (entry != null) {
sb.append(String.format("%s:%s ", entry.key, entry.value));
entry = entry.next;
}
return sb.toString();
}
public Set<String> keySet(){
Set<String> keSet=new HashSet<String>();
ConcurrentHashMap.KeySetView<String, T> keysSetView= caches.keySet();
Iterator<String> iter=keysSetView.iterator();
while(iter.hasNext()){
String key=iter.next();
keSet.add(key);
}
return keSet;
}
}