
io.datakernel.loader.CachedStaticLoader Maven / Gradle / Ivy
package io.datakernel.loader;
import io.datakernel.async.SettableStage;
import io.datakernel.async.Stage;
import io.datakernel.bytebuf.ByteBuf;
import io.datakernel.http.HttpException;
import io.datakernel.loader.cache.Cache;
class CachedStaticLoader implements StaticLoader {
private static final byte[] BYTES_ERROR = new byte[]{};
private final StaticLoader resourceLoader;
private final Cache cache;
public CachedStaticLoader(StaticLoader resourceLoader, Cache cache) {
this.resourceLoader = resourceLoader;
this.cache = cache;
}
@Override
public Stage getResource(String name) {
byte[] bytes = cache.get(name);
if (bytes == null) {
SettableStage stage = SettableStage.create();
resourceLoader.getResource(name)
.whenComplete((byteBuf, throwable) -> {
cache.put(name, throwable == null ? byteBuf.getRemainingArray() : BYTES_ERROR);
stage.set(byteBuf, throwable);
});
return stage;
} else {
if (bytes == BYTES_ERROR) {
return Stage.ofException(HttpException.notFound404());
} else {
return Stage.of(ByteBuf.wrapForReading(bytes));
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy