Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Commit

Permalink
Add trace logging to DB based user service
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkL4YG committed Nov 14, 2019
1 parent bfb1806 commit 1de21af
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/main/java/de/fearnixx/jeak/service/teamspeak/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,44 +64,50 @@ public void onPreInitialize(IBotStateEvent.IPreInitializeEvent event) {
public List<IUser> findUserByUniqueID(String ts3uniqueID) {
return computeIfNotCached(
u -> u.getClientUniqueID().equals(ts3uniqueID),
() -> serviceImplementation.findUserByUniqueID(ts3uniqueID)
() -> serviceImplementation.findUserByUniqueID(ts3uniqueID),
"uid:" + ts3uniqueID
);
}

private List<IUser> computeIfNotCached(Predicate<IUser> matchBy, Supplier<List<IUser>> getBy) {
synchronized (userCache) {
final LocalDateTime now = LocalDateTime.now();
userCache.removeIf(entry -> entry.getExpiry().isBefore(now));
Optional<CachedUserResult> optResult = userCache.stream()
.filter(entry -> entry.getUsers().stream().allMatch(matchBy))
.findFirst();
return optResult.map(CachedUserResult::getUsers)
.orElseGet(() -> {
List<IUser> users = getBy.get();
CachedUserResult result =
new CachedUserResult(users, LocalDateTime.now().plusSeconds(USR_CACHE_TTL));
userCache.add(result);
return result.getUsers();
});
}
}

@Override
public List<IUser> findUserByDBID(int ts3dbID) {
return computeIfNotCached(
u -> u.getClientDBID().equals(ts3dbID),
() -> serviceImplementation.findUserByDBID(ts3dbID)
() -> serviceImplementation.findUserByDBID(ts3dbID),
"dbid:" + ts3dbID
);
}

@Override
public List<IUser> findUserByNickname(String ts3nickname) {
return computeIfNotCached(
u -> u.getNickName().contains(ts3nickname),
() -> serviceImplementation.findUserByNickname(ts3nickname)
() -> serviceImplementation.findUserByNickname(ts3nickname),
"nick:" + ts3nickname
);
}

private List<IUser> computeIfNotCached(Predicate<IUser> matchBy, Supplier<List<IUser>> getBy, String searchHint) {
synchronized (userCache) {
final LocalDateTime now = LocalDateTime.now();
userCache.removeIf(entry -> entry.getExpiry().isBefore(now));
Optional<CachedUserResult> optResult = userCache.stream()
.filter(entry -> entry.getUsers().stream().allMatch(matchBy))
.findFirst();
return optResult.map(cachedUserResult -> {
logger.trace("Returning cached result for search: {}", searchHint);
return cachedUserResult.getUsers();
}).orElseGet(() -> {
logger.trace("Computing result for search: {}", searchHint);
List<IUser> users = getBy.get();
CachedUserResult result =
new CachedUserResult(users, LocalDateTime.now().plusSeconds(USR_CACHE_TTL));
userCache.add(result);
return result.getUsers();
});
}
}

@Override
public List<IClient> findClientByUniqueID(String ts3uniqueID) {
return serviceImplementation.findClientByUniqueID(ts3uniqueID);
Expand Down

0 comments on commit 1de21af

Please sign in to comment.