win.doyto.query.cache.CacheWrapper Maven / Gradle / Ivy
package win.doyto.query.cache;
import org.springframework.cache.Cache;
/**
* CacheWrapper
*
* @author f0rb
*/
public interface CacheWrapper {
@SuppressWarnings("unchecked")
static V invoke(Cache cache, Object key, Invocable invocable) {
if (cache == null || key == null) {
return invocable.invoke();
}
Cache.ValueWrapper valueWrapper = cache.get(key);
if (valueWrapper != null) {
return (V) valueWrapper.get();
}
V value = invocable.invoke();
cache.put(key, value);
return value;
}
static DefaultCacheWrapper createInstance() {
return new DefaultCacheWrapper<>();
}
default T execute(Object key, Invocable invocable) {
return CacheWrapper.invoke(getCache(), key, invocable);
}
void setCache(Cache cache);
Cache getCache();
default void evict(Object key) {
getCache().evict(key);
}
}