-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSoftCacheDemo.java
More file actions
74 lines (65 loc) · 1.66 KB
/
Copy pathSoftCacheDemo.java
File metadata and controls
74 lines (65 loc) · 1.66 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
import java.util.HashMap;
import java.lang.ref.SoftReference;
class SoftCache<K, V>{
private HashMap<K, SoftReference<V>> map;
public SoftCache(){
map = new HashMap<K, SoftReference<V>>();
}
public V get(K key){
SoftReference<V> softRef = map.get(key);
if(softRef == null){
return null;
}else{
return softRef.get();
}
}
public V put(K key, V value){
SoftReference<V> previousSoftRef = map.put(key, new SoftReference<V>(value));
if(previousSoftRef == null){
return null;
}else{
V oldValue = previousSoftRef.get();
previousSoftRef.clear(); // Make the old referent null.
return oldValue;
}
}
public V remove(K key){
SoftReference<V> softRef = map.remove(key);
if(softRef == null){
return null;
}else{
V oldValue = softRef.get();
softRef.clear();
return oldValue;
}
}
}
class Image{
private byte[] image;
private Image(String name){
image = new byte[1024*1024*100];
}
static Image getImage(String name){
return new Image(name);
}
}
public class SoftCacheDemo{
public static void main(String[] args){
SoftCache<Integer, Image> sc = new SoftCache<Integer, Image>();
int i = 0;
while(true){
System.out.printf("Putting large image %d into soft cache%n", i);
sc.put(i, Image.getImage("large.png" + i));
i++;
int x = (int)(Math.random()*i); // less than i
System.out.printf("Acquiring image %d from cache.%n", x);
Image img = sc.get(x);
if(img == null){
System.out.printf("Image %d no longer in cache. Re-caching.%n", x);
sc.put(x, img = Image.getImage("large.png" + x));
}
System.out.printf("Drawing image %d%n", x);
img = null; // Remove strong reference to image.
}
}
}