Skip to content

Commit 5c0125a

Browse files
author
robert
committed
add cache algorithm
1 parent 789863b commit 5c0125a

File tree

7 files changed

+208
-13
lines changed

7 files changed

+208
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# simplealgorithmcom.algorithm.digest 信息摘要,快速计算文件的MD5<br>com.algorithm.list 实现链表和List的基本操作<br>com.algorithm.tree 树的实现,二分查找<br>com.algorithm UnionFind 并查集<br>com.algorithm PrimeNumber 打表法判断素数<br>com.algorithm JavaSort 简单冒泡排序<br>com.algorithm TestAsynTreadXunlei 开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。<br>
1+
# simplealgorithmcom.algorithm.digest 信息摘要,快速计算文件的MD5<br>com.algorithm.list 实现链表和List的基本操作<br>com.algorithm.tree 树的实现,二分查找<br>com.algorithm UnionFind 并查集<br>com.algorithm PrimeNumber 打表法判断素数<br>com.algorithm JavaSort 简单冒泡排序<br>com.algorithm TestAsynTreadXunlei 开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。<br>com.algorithm.cache 缓存策略,FIFO,LRU缓存的具体实现
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.algorithm.cache;
2+
3+
import java.sql.ResultSet;
4+
import java.util.LinkedHashMap;
5+
6+
/**
7+
*
8+
* @author chao
9+
* @param <V>
10+
* @param <K>
11+
*
12+
*/
13+
public class BaseCache<K, V> implements ICache<K, V> {
14+
protected int currentsize = 0;
15+
protected LinkedHashMap<K, V> hashMap = new LinkedHashMap<>();
16+
17+
@Override
18+
public void put(K key, V value) {
19+
V oldobj = hashMap.get(key);
20+
if (oldobj != null) {
21+
currentsize = currentsize - getObjSize(oldobj);
22+
hashMap.remove(key);
23+
}
24+
currentsize = currentsize + getObjSize(value);
25+
reset();
26+
hashMap.put(key, value);
27+
28+
}
29+
30+
@Override
31+
public V get(K key) {
32+
return hashMap.get(key);
33+
}
34+
35+
@Override
36+
public V remove(K key) {
37+
V valueObject = hashMap.remove(key);
38+
currentsize = currentsize - getObjSize(valueObject);
39+
return valueObject;
40+
}
41+
42+
protected void reset() {
43+
44+
}
45+
46+
public int getObjSize(V obj) {
47+
if (obj == null) {
48+
return 0;
49+
} else if (obj instanceof String) {
50+
return ((String) obj).getBytes().length;
51+
}
52+
return 1;
53+
}
54+
55+
public void printAll() {
56+
System.out.println("currentSize: " + currentsize);
57+
System.out.println("info:" + hashMap);
58+
}
59+
60+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.algorithm.cache;
2+
3+
public class CacheTest {
4+
5+
public static void main(String[] args) {
6+
test();
7+
}
8+
9+
public static void test() {
10+
FIFOCache<String, String> fifoCache = new FIFOCache<String, String>(10);
11+
fifoCache.put("1", "aaa");
12+
fifoCache.printAll();
13+
fifoCache.put("2", "bbb");
14+
fifoCache.printAll();
15+
fifoCache.put("1", "bdd");
16+
fifoCache.printAll();
17+
fifoCache.put("1", "dddddddddd");
18+
fifoCache.printAll();
19+
fifoCache.put("3", "e");
20+
fifoCache.printAll();
21+
22+
System.out.println("===============");
23+
LRUCache<String, String> lruCache = new LRUCache<>(10);
24+
lruCache.put("1", "aaaaaa");
25+
lruCache.printAll();
26+
lruCache.put("1", "b");
27+
lruCache.printAll();
28+
lruCache.put("2", "c");
29+
lruCache.printAll();
30+
lruCache.put("3", "dd");
31+
lruCache.printAll();
32+
lruCache.put("1", "ee");
33+
lruCache.printAll();
34+
lruCache.put("9", "dddddd");
35+
lruCache.printAll();
36+
}
37+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.algorithm.cache;
2+
3+
import java.util.LinkedList;
4+
5+
/**
6+
*
7+
* @author chao
8+
* @param <T>
9+
*
10+
*/
11+
public class FIFOCache<K, V> extends BaseCache<K, V> {
12+
private int maxsize = DEFAULT_SIZE;
13+
14+
public FIFOCache(int size) {
15+
if (maxsize <= 0) {
16+
return;
17+
}
18+
this.maxsize = size;
19+
}
20+
21+
public void put(K key, V value) {
22+
int valuesize = getObjSize(value);
23+
if (valuesize > maxsize) {
24+
System.out.println("error : the object is too large!!!");
25+
return;
26+
}
27+
V oldobj = hashMap.get(key);
28+
if (oldobj != null) {
29+
currentsize = currentsize - getObjSize(oldobj);
30+
}
31+
currentsize = currentsize + getObjSize(value);
32+
reset();
33+
hashMap.put(key, value);
34+
35+
}
36+
37+
protected void reset() {
38+
while (currentsize > maxsize) {
39+
super.remove(hashMap.keySet().iterator().next());
40+
41+
}
42+
}
43+
44+
public void printAll() {
45+
super.printAll();
46+
System.out.println("keyset: " + hashMap.keySet());
47+
System.out.println();
48+
// System.out.println("max size: " + maxsize);
49+
}
50+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.algorithm.cache;
2+
3+
/**
4+
*
5+
* @author chao
6+
* @param <K>
7+
* @param <V>
8+
*
9+
*/
10+
public interface ICache<K, V> {
11+
public static final int DEFAULT_SIZE = 10;
12+
13+
public void put(K key, V value);
14+
15+
public V get(K key);
16+
17+
public V remove(K key);
18+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.algorithm.cache;
2+
3+
/**
4+
*
5+
* @author chao
6+
*
7+
*/
8+
public class LRUCache<K, V> extends BaseCache<K, V> {
9+
private int maxsize = DEFAULT_SIZE;
10+
11+
public LRUCache(int size) {
12+
if (maxsize <= 0) {
13+
return;
14+
}
15+
this.maxsize = size;
16+
}
17+
18+
@Override
19+
public void put(K key, V value) {
20+
int valuesize = getObjSize(value);
21+
if (valuesize > maxsize) {
22+
System.out.println("error : the object is too large!!!");
23+
return;
24+
}
25+
super.put(key, value);
26+
27+
}
28+
29+
protected void reset() {
30+
while (currentsize > maxsize) {
31+
super.remove(hashMap.keySet().iterator().next());
32+
}
33+
}
34+
35+
public void printAll() {
36+
super.printAll();
37+
System.out.println("keyset:" + hashMap.keySet());
38+
System.out.println();
39+
// System.out.println("max size" + maxsize);
40+
}
41+
}

src/com/algorithm/digest/DigestMain.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
package com.algorithm.digest;
22

33
import java.io.File;
4-
import java.io.FileInputStream;
5-
import java.io.FileNotFoundException;
6-
import java.io.FileOutputStream;
7-
import java.io.IOException;
8-
import java.io.InputStream;
9-
import java.math.BigInteger;
10-
import java.nio.MappedByteBuffer;
11-
import java.nio.channels.FileChannel;
12-
import java.nio.channels.FileChannel.MapMode;
13-
import java.security.DigestOutputStream;
14-
import java.security.MessageDigest;
15-
import java.security.NoSuchAlgorithmException;
4+
165
/**
176
*
187
* @author robert

0 commit comments

Comments
 (0)