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
|
packagecom.simonefolinojavablog.softreferencecache;
importjava.util.logging.Level;
publicclassImageLoader {
privatestaticSoftReferenceCache<String, ValueCache> cache =newSoftReferenceCache<String, ValueCache>();
publicstaticValueCache getImage(String key) {
ValueCache result =null;
ValueCache valueCache = (ValueCache) cache.get(key);
if(valueCache ==null) {
ProjectLogger.PROJECT_LOGGER.getLogger().log(Level.INFO,"entry: "+ key +" result: cache miss");
ValueCache newValue =newValueCache();
newValue.setKey(key);
cache.put(key, newValue);
result = newValue;
}else{
ProjectLogger.PROJECT_LOGGER.getLogger().log(Level.INFO,"entry: "+ key +" result: cache hit");
}
returnresult;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
packagecom.simonefolinojavablog.softreferencecache;
importjava.lang.ref.SoftReference;
importjava.util.HashMap;
publicclassSoftReferenceCache<KextendsComparable<K>, V> {
privateHashMap<K, SoftReference<V>> map =newHashMap<K, SoftReference<V>>();
publicV get(K key) {
SoftReference<V> softReference = map.get(key);
if(softReference ==null) {
returnnull;
}
returnsoftReference.get();
}
publicvoidput(K key, V value) {
map.put(key,newSoftReference<V>(value));
}
}
|
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
|
packagecom.simonefolinojavablog.weakreferencecache;
importjava.util.logging.Level;
publicclassImageLoader {
privatestaticWeakReferenceCache<String, ValueCache> cache =newWeakReferenceCache<String, ValueCache>();
publicstaticValueCache getImage(String key) {
ValueCache result =null;
ValueCache valueCache = (ValueCache) cache.get(key);
if(valueCache ==null) {
ProjectLogger.PROJECT_LOGGER.getLogger().log(Level.INFO,"entry: "+ key +" result: cache miss");
ValueCache newValue =newValueCache();
newValue.setKey(key);
cache.put(key, newValue);
result = newValue;
}else{
ProjectLogger.PROJECT_LOGGER.getLogger().log(Level.INFO,"entry: "+ key +" result: cache hit");
}
returnresult;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
packagecom.simonefolinojavablog.weakreferencecache;
importjava.lang.ref.WeakReference;
importjava.util.HashMap;
publicclassWeakReferenceCache<KextendsComparable<K>, V> {
privateHashMap<K, WeakReference<V>> map =newHashMap<K, WeakReference<V>>();
publicV get(K key) {
WeakReference<V> WeakReference = map.get(key);
if(WeakReference ==null) {
returnnull;
}
returnWeakReference.get();
}
publicvoidput(K key, V value) {
map.put(key,newWeakReference<V>(value));
}
}
|