
shz.core.cache.Cache Maven / Gradle / Ivy
package shz.core.cache;
import shz.core.NullHelp;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
public interface Cache {
V get(K key);
void put(K key, V val, long expireMills);
default void put(K key, V val) {
put(key, val, 0L);
}
boolean containsKey(K key);
int size();
Set keys();
Set keys(Predicate super K> predicate);
void delete(K key);
default void delete(Set extends K> keys) {
if (NullHelp.isEmpty(keys)) return;
keys.forEach(this::delete);
}
default void delete(Predicate super K> predicate) {
delete(keys(predicate));
}
default long ttlMillis(K key) {
return 0L;
}
void clear();
default V computeIfAbsent(K key, Function super K, ? extends V> mappingFunction) {
V v;
if ((v = get(key)) == null) {
synchronized (this) {
if ((v = get(key)) == null) {
V newValue;
if ((newValue = mappingFunction.apply(key)) != null) {
put(key, newValue);
return newValue;
}
}
}
}
return v;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy