Skip to content

Commit 5390849

Browse files
In put method, we can specify action to be executed when item is expired
Add shutdown method to TTL map to shutdown the underlying scheduled executor
1 parent 3375ed2 commit 5390849

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

.idea/workspace.xml

Lines changed: 17 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/com/athingforcode/Main.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
public class Main {
66
public static void main(String[] args) throws InterruptedException {
77
TTLHashMap<String, String> map = new TTLHashMap<>();
8-
map.put("key1", "value1", 5, TimeUnit.SECONDS);
9-
map.put("key2", "value2", 10, TimeUnit.SECONDS);
8+
map.put("key1", "value1", 5, TimeUnit.SECONDS, null);
9+
map.put("key2", "value2", 10, TimeUnit.SECONDS, null);
10+
11+
map.put("key3", "value3", 5, TimeUnit.SECONDS, (key,value) -> System.out.println("Action executed when key-"+ key + " expired, its value is : " + value));
12+
map.put("key4", "value4", 10, TimeUnit.SECONDS, (key,value) -> System.out.println("Action executed when key-"+ key + " expired, its value is : " + value));
1013

1114
System.out.println(map.get("key1")); // prints "value1"
12-
Thread.sleep(6000);
15+
Thread.sleep(12000);
1316
System.out.println(map.get("key1")); // prints null
14-
System.out.println(map.get("key2")); // prints "value2"
17+
System.out.println(map.get("key2")); // prints null
1518
map.remove("key2");
1619
System.out.println(map.get("key2")); // prints "null"
20+
21+
map.shutdown();
1722
}
1823
}

src/main/java/com/athingforcode/TTLHashMap.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.athingforcode;
22

3+
import java.util.Objects;
34
import java.util.concurrent.ConcurrentHashMap;
45
import java.util.concurrent.Executors;
56
import java.util.concurrent.ScheduledExecutorService;
67
import java.util.concurrent.TimeUnit;
8+
import java.util.function.BiConsumer;
79

810
public class TTLHashMap<K, V> {
911
private final ConcurrentHashMap<K, Entry<K, V>> map;
@@ -14,10 +16,18 @@ public TTLHashMap() {
1416
this.scheduler = Executors.newSingleThreadScheduledExecutor();
1517
}
1618

17-
public void put(K key, V value, long ttl, TimeUnit unit) {
19+
public void shutdown() {
20+
scheduler.shutdownNow();
21+
}
22+
23+
public void put(K key, V value, long ttl, TimeUnit unit, BiConsumer<K, V> actionWhenExpired) {
24+
Objects.requireNonNull(key);
1825
Entry<K, V> entry = new Entry<>(key, value, ttl, unit);
1926
map.put(key, entry);
20-
setRemovalSchedule(entry);
27+
//if ttl is negative then entry is cosidered non expiring
28+
if(ttl >= 0) {
29+
setRemovalSchedule(entry,actionWhenExpired);
30+
}
2131
}
2232

2333
public V get(K key) {
@@ -33,9 +43,12 @@ public void remove(K key) {
3343
map.remove(key);
3444
}
3545

36-
private void setRemovalSchedule(Entry<K, V> entry) {
46+
private void setRemovalSchedule(Entry<K, V> entry, BiConsumer<K, V> actionWhenExpired) {
3747
scheduler.schedule(() -> {
3848
map.remove(entry.getKey());
49+
if(actionWhenExpired != null) {
50+
actionWhenExpired.accept(entry.getKey(), entry.getValue());
51+
}
3952
}, entry.getTTL(), entry.getTtlUnit());
4053
}
4154

0 commit comments

Comments
 (0)