Skip to content

Commit af528a2

Browse files
authored
Merge pull request #4 from unidev-platform/feature/weak-cache
Add basic weak cache
2 parents d687573 + 12997b9 commit af528a2

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.unidev.platform.cache;
2+
3+
import lombok.Getter;
4+
5+
import java.lang.ref.WeakReference;
6+
import java.util.concurrent.ConcurrentHashMap;
7+
8+
/**
9+
* Weak concurrent cache
10+
* @param <K>
11+
* @param <V>
12+
*/
13+
@Getter
14+
public class WeakConcurrentCache<K, V> {
15+
private final ConcurrentHashMap<K, WeakReference<V>> cache = new ConcurrentHashMap<>();
16+
17+
public void put(K key, V value) {
18+
cache.put(key, new WeakReference<>(value));
19+
}
20+
21+
public V get(K key) {
22+
WeakReference<V> weakRef = cache.get(key);
23+
return weakRef == null ? null : weakRef.get();
24+
}
25+
}

0 commit comments

Comments
 (0)