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 org.rx.bean.RxConfig;
import org.rx.core.cache.LocalCache;
import org.rx.core.cache.ThreadCache;
import org.rx.util.function.BiFunc;
import java.util.*;
import static org.rx.core.App.*;
public interface Cache {
int NON_EXPIRE_MINUTES = 0;
String LRU_CACHE = "LruCache";
String LOCAL_CACHE = "localCache";
String THREAD_CACHE = "ThreadCache";
static TV getOrSet(TK key, BiFunc supplier) {
return getOrSet(key, supplier, NON_EXPIRE_MINUTES);
}
static TV getOrSet(TK key, BiFunc supplier, int expireMinutes) {
return getOrSet(key, supplier, expireMinutes, App.getConfig().getDefaultCache());
}
static TV getOrSet(TK key, BiFunc supplier, String cacheName) {
return getOrSet(key, supplier, NON_EXPIRE_MINUTES, cacheName);
}
static TV getOrSet(TK key, BiFunc supplier, int expireMinutes, String cacheName) {
require(key, supplier);
return Cache.getInstance(cacheName).get(key, supplier, expireMinutes);
}
static Cache getInstance() {
return getInstance(App.getConfig().getDefaultCache());
}
static Cache getInstance(String name) {
return Container.getInstance().getOrRegister(name, () -> {
switch (name) {
case THREAD_CACHE:
return new ThreadCache<>();
case LOCAL_CACHE:
return new LocalCache<>(4);
default:
return new LocalCache<>();
}
});
}
long size();
Set keySet();
TV put(TK key, TV val);
default TV put(TK key, TV value, int expireMinutes) {
return put(key, value);
}
TV remove(TK key);
default void remove(TK key, boolean destroy) {
TV v = remove(key);
if (destroy) {
tryClose(v);
}
}
void clear();
TV get(TK key);
TV get(TK key, BiFunc supplier);
default TV get(TK key, BiFunc supplier, int expireMinutes) {
return get(key, supplier);
}
}