You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.4 KiB

package w8.W88;
import java.util.concurrent.ConcurrentHashMap;
public class Cache<K, V> {
private ConcurrentHashMap<K, V> cache;
private long defaultTTL;
public Cache() {
this.cache = new ConcurrentHashMap<>();
this.defaultTTL = 60000;
}
public Cache(long ttlMillis) {
this.cache = new ConcurrentHashMap<>();
this.defaultTTL = ttlMillis;
}
public void put(K key, V value) {
if (key == null) {
System.out.println("Error: Key cannot be null");
return;
}
cache.put(key, value);
System.out.println("Cache put: " + key + " = " + value);
}
public V get(K key) {
if (key == null) {
System.out.println("Error: Key cannot be null");
return null;
}
V value = cache.get(key);
if (value == null) {
System.out.println("Cache miss: " + key + " not found");
return null;
}
System.out.println("Cache hit: " + key + " = " + value);
return value;
}
public void remove(K key) {
cache.remove(key);
System.out.println("Cache removed: " + key);
}
public void clear() {
cache.clear();
System.out.println("Cache cleared");
}
public int size() {
return cache.size();
}
public boolean containsKey(K key) {
return cache.containsKey(key);
}
}