提到缓存,不得不提就是缓存算法(淘汰算法),常见算法有LRU、LFU和FIFO等算法,每种算法各有各的优势和缺点及适应环境。
1、LRU(Least Recently Used ,最近最少使用)
算法根据数据的最近访问记录来淘汰数据,其原理是如果数据最近被访问过,将来被访问的几概率相对比较高,最常见的实现是使用一个链表保存缓存数据,详细具体算法如下:
1. 新数据插入到链表头部;
2. 每当缓存数据命中,则将数据移到链表头部;
3. 当链表满的时候,将链表尾部的数据丢弃;
2、LFU(Least Frequently Used,最不经常使用)
算法根据数据的历史访问频率来淘汰数据,其原理是如果数据过去被访问次数越多,将来被访问的几概率相对比较高。LFU的每个数据块都有一个引用计数,所有数据块按照引用计数排序,具有相同引用计数的数据块则按照时间排序。
具体算法如下:
1. 新加入数据插入到队列尾部(因为引用计数为1);
2. 队列中的数据被访问后,引用计数增加,队列重新排序;
3. 当需要淘汰数据时,将已经排序的列表最后的数据块删除;
3、FIFO(First In First Out ,先进先出)
算法是根据先进先出原理来淘汰数据的,实现上是最简单的一种,具体算法如下:
1. 新访问的数据插入FIFO队列尾部,数据在FIFO队列中顺序移动;
2. 淘汰FIFO队列头部的数据;
评价一个缓存算法好坏的标准主要有两个,一是命中率要高,二是算法要容易实现。当存在热点数据时,LRU的效率很好,但偶发性的、周期性的批量操作会导致LRU命中率急剧下降,缓存污染情况比较严重。LFU效率要优于LRU,且能够避免周期性或者偶发性的操作导致缓存命中率下降的问题。但LFU需要记录数据的历史访问记录,一旦数据访问模式改变,LFU需要更长时间来适用新的访问模式,即:LFU存在历史数据影响将来数据的“缓存污染”效用。FIFO虽然实现很简单,但是命中率很低,实际上也很少使用这种算法。
基于现有jdk类库,我们可以很容易实现上面的缓存算法
首先定义缓存接口类
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
|
/**
*
*
*
*/
public
/**
*
*
*
*/
int
/**
*
*
*
*/
long
/**
*
*
*
*
*/
void
/**
*
*
*
*
*/
void
long
/**
*
*
*
*/
V
/**
*
*
*
*/
int
/**
*
*
*/
boolean
/**
*
*
*
*/
void
/**
*
*/
void
/**
*
*
*
*/
int
/**
*
*/
boolean
}
|
基本实现抽象类
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
|
import
import
import
/**
*
*/
public
implements
class
CacheObject(K2 long
this .key
this .cachedObject
this .ttl
this .lastAccess
}
final
final
long
//
long
//
long
//
boolean
if
0 )
return
;
}
return
}
V2
lastAccess
accessCount++;
return
}
}
protected
private
new
private
private
protected
//
protected
//是否设置默认过期时间
public
return
}
protected
//
public
int
long
this .cacheSize
this .defaultExpire
}
public
return
}
protected
return
0
}
public
put(key,
}
public
long
writeLock.lock();
try
CacheObject<K,V> new
if
0 )
existCustomExpire true ;
}
if
eliminate()
}
cacheMap.put(key,
}
finally
writeLock.unlock();
}
}
/**
*
*/
public
readLock.lock();
try
CacheObject<K,V>
if
null )
return
;
}
if
true )
cacheMap.remove(key);
return
;
}
return
}
finally
readLock.unlock();
}
}
public
writeLock.lock();
try
return
}
finally
writeLock.unlock();
}
}
/**
*
*
*
*/
protected
public
if
0 ) //o
return
;
}
return
}
public
writeLock.lock();
try
cacheMap.remove(key);
}
finally
writeLock.unlock();
}
}
public
writeLock.lock();
try
cacheMap.clear();
}
finally
writeLock.unlock();
}
}
public
return
}
public
return
0 ;
}
}
|
LRU缓存实现类
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
|
import
import
import
/**
*
*
*
*
*
*/
public
extends
public
int
long
super (cacheSize
//linkedHash已经实现LRU算法
this .cacheMap new
1
true
@Override
protected
Map.Entry<K,
return
this .removeEldestEntry(eldest);
}
};
}
private
if
0 )
return
;
return
}
/**
*
*/
@Override
protected
if (!isNeedClearExpiredObject()){ return
Iterator<CacheObject<K,
int
0
while (iterator.hasNext()){
CacheObject<K,
if (cacheObject.isExpired()
iterator.remove();
count++
}
}
return
}
}
|
LFU实现类
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
|
import
import
//LFU实现
public
extends
public
int
long
super (cacheSize,
cacheMap new
1 )
}
/**
*
*
*/
@Override
protected
Iterator<CacheObject<K,
int
0
long
while (iterator.hasNext()){
CacheObject<K,
if (cacheObject.isExpired()
iterator.remove();
count++
continue
} else {
minAccessCount
}
}
if (count 0
return
if (minAccessCount
iterator
while (iterator.hasNext()){
CacheObject<K,
cacheObject.accessCount
if (cacheObject.accessCount 0
iterator.remove();
count++
}
}
}
return
}
}
|
FIFO实现类
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
|
import
import
/**
*
*
*
*
*
*/
public
extends
public
int
long
super (cacheSize,
cacheMap new
1 );
}
@Override
protected
int
0 ;
K null ;
Iterator<CacheObject<K,
while
CacheObject<K,
if
iterator.remove();
count++;
} else
if
null )
firstKey
}
}
if
null
//删除过期对象还是满,继续删除链表第一个
cacheMap.remove(firstKey);
}
return
}
}
|