From ecfa1734cb1e181978b8aae31e7f9434d0df8986 Mon Sep 17 00:00:00 2001 From: bhuism Date: Sat, 11 Jul 2020 22:18:19 +0200 Subject: [PATCH] cleanups --- .../appsource/badge/expected/GitHubImpl.java | 2 ++ .../appsource/badge/expected/GitLabImpl.java | 2 ++ .../appsource/badge/expected/MyCacheImpl.java | 22 +++++++++---------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main/java/nl/appsource/badge/expected/GitHubImpl.java b/src/main/java/nl/appsource/badge/expected/GitHubImpl.java index a142e60..7b4240e 100644 --- a/src/main/java/nl/appsource/badge/expected/GitHubImpl.java +++ b/src/main/java/nl/appsource/badge/expected/GitHubImpl.java @@ -71,8 +71,10 @@ public BadgeStatus getBadgeStatus(final String owner, final String repo, final S final BadgeStatus cacheValue = cache.getIfPresent(getKey(owner, repo, branch, commit_sha)); if (cacheValue != null) { + log.info("Cache hit"); return cacheValue; } else { + log.info("Cache miss"); return callGitHub(owner, repo, branch, commit_sha); } diff --git a/src/main/java/nl/appsource/badge/expected/GitLabImpl.java b/src/main/java/nl/appsource/badge/expected/GitLabImpl.java index ea899e3..0a03124 100644 --- a/src/main/java/nl/appsource/badge/expected/GitLabImpl.java +++ b/src/main/java/nl/appsource/badge/expected/GitLabImpl.java @@ -41,8 +41,10 @@ public BadgeStatus getBadgeStatus(final String id, final String branch, final St final BadgeStatus cacheValue = cache.getIfPresent(getKey(id, branch, commit_sha)); if (cacheValue != null) { + log.info("Cache hit"); return cacheValue; } else { + log.info("Cache miss"); return callGitLab(id, branch, commit_sha); } diff --git a/src/main/java/nl/appsource/badge/expected/MyCacheImpl.java b/src/main/java/nl/appsource/badge/expected/MyCacheImpl.java index ce26628..80f896d 100644 --- a/src/main/java/nl/appsource/badge/expected/MyCacheImpl.java +++ b/src/main/java/nl/appsource/badge/expected/MyCacheImpl.java @@ -4,7 +4,6 @@ import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; -import java.util.stream.Collectors; @Slf4j public class MyCacheImpl implements MyCache { @@ -16,20 +15,19 @@ public class MyCacheImpl implements MyCache { @Override public V getIfPresent(final K key) { + synchronized (this) { - final Long expired = System.currentTimeMillis() + (EXPIRED_IN_SECONDS * 1000); - - _timestamp.keySet().removeAll( - _timestamp - .entrySet() - .stream() - .filter(e -> e.getValue() < expired) - .map(Entry::getKey) - .map(_cache::remove) - .collect(Collectors.toSet())) - ; + final Long expired = System.currentTimeMillis() - (EXPIRED_IN_SECONDS * 1000); + + _timestamp + .entrySet() + .stream() + .filter(e -> e.getValue() < expired) + .map(Entry::getKey) + .forEach(_cache::remove); } + return _cache.get(key); }