-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
27 lines (22 loc) · 727 Bytes
/
Node.java
File metadata and controls
27 lines (22 loc) · 727 Bytes
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
package lrucache;
/**
* Doubly-linked list node storing a key-value pair with optional TTL expiry.
* The key is stored so we can remove it from the HashMap during eviction
* without a reverse lookup.
*/
public class Node<K, V> {
final K key;
V value;
long expiryMs; // absolute epoch ms; -1 = no expiry
Node<K, V> prev;
Node<K, V> next;
Node(K key, V value, long ttlMs) {
this.key = key;
this.value = value;
this.expiryMs = (ttlMs > 0) ? System.currentTimeMillis() + ttlMs : -1L;
}
/** Returns true if this entry has a TTL and it has elapsed. */
boolean isExpired() {
return expiryMs != -1L && System.currentTimeMillis() > expiryMs;
}
}