org.rx.core.Cache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxlib Show documentation
Show all versions of rxlib Show documentation
A set of utilities for Java
package org.rx.core;
import lombok.NonNull;
import lombok.SneakyThrows;
import org.rx.bean.AbstractMap;
import org.rx.core.cache.MemoryCache;
import org.rx.util.function.BiFunc;
import static org.rx.core.Constants.NON_RAW_TYPES;
public interface Cache extends AbstractMap {
Object NULL_VALUE = new Object();
static TV getOrSet(TK key, BiFunc loadingFunc) {
return getOrSet(key, loadingFunc, null);
}
static TV getOrSet(@NonNull TK key, @NonNull BiFunc loadingFunc, CachePolicy expiration) {
return Cache.getInstance().get(key, loadingFunc, expiration);
}
static Cache getInstance() {
return IOC.get(Cache.class);
}
/**
* @param cacheType MEMORY_CACHE, DISK_CACHE
* @param
* @param
* @return
*/
@SuppressWarnings(NON_RAW_TYPES)
static Cache getInstance(Class extends Cache> cacheType) {
return IOC.get(cacheType, (Class) MemoryCache.class);
}
default TV get(TK key, BiFunc loadingFunc) {
return get(key, loadingFunc, null);
}
@SneakyThrows
default TV get(TK key, BiFunc loadingFunc, CachePolicy policy) {
TV v;
if ((v = get(key)) == null) {
TV newValue;
if ((newValue = loadingFunc.invoke(key)) != null) {
put(key, newValue, policy);
return newValue;
}
}
return v;
}
@Override
default TV put(TK key, TV value) {
return put(key, value, null);
}
TV put(TK key, TV value, CachePolicy policy);
}