|
@@ -18,23 +18,23 @@ public abstract class Table<T extends Persistable> {
|
|
|
|
|
|
private Map<Long, T> cacheMap = new HashMap<>();
|
|
|
|
|
|
- public Optional<T> findById(long id) {
|
|
|
+ public T getById(long id) {
|
|
|
if (cacheMap.containsKey(id)) {
|
|
|
- return Optional.of(cacheMap.get(id));
|
|
|
+ return cacheMap.get(id);
|
|
|
}
|
|
|
- Optional<T> obj = getFromDbById(id);
|
|
|
- if(obj.isPresent()) {
|
|
|
- cache(obj.get());
|
|
|
+ Optional<T> opt = getFromDbById(id);
|
|
|
+ if(opt.isEmpty()) {
|
|
|
+ return null;
|
|
|
}
|
|
|
- return obj;
|
|
|
+ return cache(opt.get());
|
|
|
}
|
|
|
|
|
|
- public T getById(long id) {
|
|
|
- Optional<T> opt = findById(id);
|
|
|
- if(opt.isEmpty()) {
|
|
|
- return null;
|
|
|
+ public Optional<T> findById(long id) {
|
|
|
+ T obj = getById(id);
|
|
|
+ if (obj == null) {
|
|
|
+ return Optional.empty();
|
|
|
}
|
|
|
- return opt.get();
|
|
|
+ return Optional.of(obj);
|
|
|
}
|
|
|
|
|
|
public List<T> getByField(String fieldname, Object value) {
|
|
@@ -115,7 +115,7 @@ public abstract class Table<T extends Persistable> {
|
|
|
delete(obj.getId());
|
|
|
}
|
|
|
|
|
|
- private void cache(T obj) {
|
|
|
+ private T cache(T obj) {
|
|
|
if (cacheMap.containsKey(obj.getId())) {
|
|
|
cacheMap.replace(obj.getId(), obj);
|
|
|
} else {
|
|
@@ -123,6 +123,7 @@ public abstract class Table<T extends Persistable> {
|
|
|
}
|
|
|
//System.out.println("Cache size: " + cacheMap.size());
|
|
|
//cacheMap.forEach((key, val) -> System.out.println("Cache: " + key));
|
|
|
+ return obj;
|
|
|
}
|
|
|
|
|
|
private void remove(long id) {
|