Skip to content

Instantly share code, notes, and snippets.

@mikepenz
Created July 29, 2015 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikepenz/21c5b0e0a7cd6e1e8593 to your computer and use it in GitHub Desktop.
Save mikepenz/21c5b0e0a7cd6e1e8593 to your computer and use it in GitHub Desktop.
A small util class to store any data (single or list) and load it again
public class CachingUtil {
public <C> List<C> loadParsed(Class<C> type, String locale) {
String name = getName(type);
Path path = Paths.get(Config.getEntry("paths.cache") + "/" + name + "/", name + "." + locale + ".json");
if (Files.exists(path) && !Files.isDirectory(path)) {
try {
return new ObjectMapper()
.readValue(path.toFile(), new ObjectMapper().getTypeFactory().constructCollectionType(List.class, type));
} catch (IOException e) {
//nothing to do
}
}
return null;
}
public <C> C loadParsed(Class<C> type, String id, String locale) {
String name = getName(type);
Path path = Paths.get(Config.getEntry("paths.cache") + "/" + name + "/", id + "." + locale + ".json");
if (Files.exists(path) && !Files.isDirectory(path)) {
try {
return new ObjectMapper().readValue(path.toFile(), type);
} catch (IOException e) {
//nothing to do
}
}
return null;
}
public <T> boolean storeParsed(List<T> types, String locale) throws CustomException {
if (types == null || types.size() == 0) {
return false;
}
try {
String name = getName(types.get(0));
Path userDir = Paths.get(Config.getEntry("paths.cache") + "/" + name + "/");
if (!Files.exists(userDir)) {
try {
userDir.toFile().mkdirs();
} catch (SecurityException e) {
CustomException ce = new CustomException(getClass().getName(), e);
ce.setCustomMessage("Couldn't store catalog throw security reasons - Security Manager is activ! ");
throw ce;
}
}
JacksonUtil.getJsonMapperInstance()
.writeValue(new File(Config.getEntry("paths.cache") + "/" + name + "/" + name + "." + locale + ".json"), types);
} catch (IOException e) {
return false;
}
return true;
}
public <T> boolean storeParsed(T type, String id, String locale) throws CustomException {
if (type == null) {
return false;
}
try {
String name = getName(type);
Path userDir = Paths.get(Config.getEntry("paths.cache") + "/" + name + "/");
if (!Files.exists(userDir)) {
try {
userDir.toFile().mkdirs();
} catch (SecurityException e) {
CustomException ce = new CustomException(getClass().getName(), e);
ce.setCustomMessage("Couldn't store catalog throw security reasons - Security Manager is activ! ");
throw ce;
}
}
JacksonUtil.getJsonMapperInstance()
.writeValue(new File(Config.getEntry("paths.cache") + "/" + name + "/" + id + "." + locale + ".json"), type);
} catch (IOException e) {
return false;
}
return true;
}
private <T> String getName(T type) {
String name = type.getClass().getSimpleName().toLowerCase();
if (!name.endsWith("s")) {
name = name + "s";
}
return name;
}
private String getName(Class clazz) {
String name = clazz.getSimpleName().toLowerCase();
if (!name.endsWith("s")) {
name = name + "s";
}
return name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment