Skip to content

Commit b87bc0b

Browse files
committed
feat: create shared InternalCaching entity
1 parent d5952c7 commit b87bc0b

File tree

4 files changed

+122
-15
lines changed

4 files changed

+122
-15
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.pakland.mdas.githubstats.domain.lib;
2+
3+
import java.util.*;
4+
5+
public class InternalCaching<K, T> {
6+
7+
private final Map<K, Integer> cacheIndex = new HashMap<>();
8+
private final List<T> cacheStore = new ArrayList<>();
9+
10+
public InternalCaching() {
11+
}
12+
13+
protected boolean has(K key) {
14+
return this.cacheIndex.containsKey(key);
15+
}
16+
17+
protected Integer size() {
18+
return this.cacheIndex.size();
19+
}
20+
21+
public void add(K key, T value) {
22+
if (!cacheIndex.containsKey(key)) {
23+
cacheStore.add(value);
24+
cacheIndex.put(key, cacheStore.size() - 1);
25+
}
26+
}
27+
28+
public T get(K key) {
29+
Integer index = cacheIndex.get(key);
30+
if (index == null) {
31+
return null;
32+
}
33+
return cacheStore.get(index);
34+
}
35+
36+
public T delete(K key) {
37+
Integer index = cacheIndex.get(key);
38+
if (index == null) {
39+
return null;
40+
}
41+
T element = cacheStore.get(index);
42+
cacheStore.remove(element);
43+
cacheIndex.remove(key);
44+
return element;
45+
}
46+
}

src/main/java/io/pakland/mdas/githubstats/infrastructure/github/repository/CommentGitHubRepository.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.pakland.mdas.githubstats.application.exceptions.HttpException;
44
import io.pakland.mdas.githubstats.application.mappers.CommentMapper;
55
import io.pakland.mdas.githubstats.domain.entity.Comment;
6+
import io.pakland.mdas.githubstats.domain.lib.InternalCaching;
67
import io.pakland.mdas.githubstats.domain.repository.CommentExternalRepository;
78
import io.pakland.mdas.githubstats.infrastructure.github.model.GitHubCommentDTO;
89
import java.util.*;
@@ -15,8 +16,7 @@ public class CommentGitHubRepository implements CommentExternalRepository {
1516
private final WebClientConfiguration webClientConfiguration;
1617
private final Logger logger = LoggerFactory.getLogger(CommentGitHubRepository.class);
1718

18-
private final Map<Integer, Integer> internalQueryCache = new HashMap<>();
19-
private final List<List<Comment>> commentStore = new ArrayList<>();
19+
private final InternalCaching<String, List<Comment>> cache = new InternalCaching<>();
2020

2121
public CommentGitHubRepository(WebClientConfiguration webClientConfiguration) {
2222
this.webClientConfiguration = webClientConfiguration;
@@ -29,9 +29,9 @@ public List<Comment> fetchCommentsFromPullRequest(
2929
throws HttpException {
3030
String query = String.format("/repos/%s/%s/pulls/%s/comments?%s", repositoryOwner,
3131
repositoryName, pullRequestNumber, getRequestParams(page, perPage));
32-
Integer dataPosition = internalQueryCache.get(query.hashCode());
33-
if (dataPosition != null) {
34-
return commentStore.get(dataPosition);
32+
List<Comment> maybeResult = cache.get(query);
33+
if (maybeResult != null) {
34+
return maybeResult;
3535
}
3636

3737
try {
@@ -43,8 +43,7 @@ public List<Comment> fetchCommentsFromPullRequest(
4343
.map(CommentMapper::dtoToEntity)
4444
.collectList()
4545
.block();
46-
commentStore.add(result);
47-
internalQueryCache.put(query.hashCode(), commentStore.size() - 1);
46+
cache.add(query, result);
4847

4948
return result;
5049

src/main/java/io/pakland/mdas/githubstats/infrastructure/github/repository/CommitGitHubRepository.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import io.pakland.mdas.githubstats.application.exceptions.HttpException;
44
import io.pakland.mdas.githubstats.application.mappers.CommitMapper;
55
import io.pakland.mdas.githubstats.domain.entity.Commit;
6+
import io.pakland.mdas.githubstats.domain.lib.InternalCaching;
67
import io.pakland.mdas.githubstats.domain.repository.CommitExternalRepository;
78
import io.pakland.mdas.githubstats.infrastructure.github.model.GitHubCommitDTO;
8-
import java.util.*;
9+
import java.util.List;
910
import org.slf4j.Logger;
1011
import org.slf4j.LoggerFactory;
1112
import org.springframework.web.reactive.function.client.WebClientResponseException;
@@ -15,8 +16,7 @@ public class CommitGitHubRepository implements CommitExternalRepository {
1516
private final WebClientConfiguration webClientConfiguration;
1617
private final Logger logger = LoggerFactory.getLogger(CommitGitHubRepository.class);
1718

18-
private final Map<Integer, Integer> internalQueryCache = new HashMap<>();
19-
private final List<List<Commit>> commitStore = new ArrayList<>();
19+
private final InternalCaching<String, List<Commit>> cache = new InternalCaching<>();
2020

2121
public CommitGitHubRepository(WebClientConfiguration webClientConfiguration) {
2222
this.webClientConfiguration = webClientConfiguration;
@@ -29,9 +29,9 @@ public List<Commit> fetchCommitsFromPullRequest(FetchCommitsFromPullRequestReque
2929
request.getRepositoryOwner(),
3030
request.getRepositoryName(), request.getPullRequestNumber(),
3131
getRequestParams(request));
32-
Integer resultPosition = internalQueryCache.get(query.hashCode());
33-
if (resultPosition != null) {
34-
return commitStore.get(resultPosition);
32+
List<Commit> maybeResult = cache.get(query);
33+
if (maybeResult != null) {
34+
return maybeResult;
3535
}
3636

3737
try {
@@ -44,8 +44,7 @@ public List<Commit> fetchCommitsFromPullRequest(FetchCommitsFromPullRequestReque
4444
.map(CommitMapper::dtoToEntity)
4545
.collectList()
4646
.block();
47-
commitStore.add(result);
48-
internalQueryCache.put(query.hashCode(), commitStore.size() - 1);
47+
cache.add(query, result);
4948

5049
return result;
5150
} catch (WebClientResponseException ex) {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package io.pakland.mdas.githubstats.domain.lib;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
public class InternalCachingTest {
10+
11+
@Test
12+
public void shouldAddAnElement() {
13+
InternalCaching<Integer, Integer> cache = new InternalCaching<>();
14+
cache.add(1, 1);
15+
assertTrue(cache.has(1));
16+
assertEquals(cache.size(), 1);
17+
}
18+
19+
@Test
20+
public void shouldNotAddRepeatedElement() {
21+
InternalCaching<Integer, Integer> cache = new InternalCaching<>();
22+
cache.add(1, 1);
23+
cache.add(2, 1);
24+
assertTrue(cache.has(1));
25+
assertTrue(cache.has(2));
26+
assertEquals(cache.size(), 2);
27+
28+
cache.add(2, 1);
29+
assertEquals(cache.size(), 2);
30+
}
31+
32+
@Test
33+
public void shouldReturnAnElementIfExists() {
34+
InternalCaching<Integer, Integer> cache = new InternalCaching<>();
35+
cache.add(3, 1);
36+
assertEquals(cache.get(3), 1);
37+
}
38+
39+
@Test
40+
public void shouldReturnNullIfKeyDoesNotExist(){
41+
InternalCaching<Integer, Integer> cache = new InternalCaching<>();
42+
cache.add(3, 1);
43+
assertNull(cache.get(4));
44+
}
45+
46+
@Test
47+
public void shouldDeleteExistingElement() {
48+
InternalCaching<Integer, Integer> cache = new InternalCaching<>();
49+
cache.add(4, 1);
50+
assertEquals(cache.size(), 1);
51+
52+
assertEquals(cache.delete(4), 1);
53+
assertEquals(cache.size(), 0);
54+
}
55+
56+
@Test
57+
public void shouldReturnNullIfDeletingNonExistingElement() {
58+
InternalCaching<Integer, Integer> cache = new InternalCaching<>();
59+
cache.add(4, 1);
60+
assertEquals(cache.size(), 1);
61+
assertNull(cache.delete(5));
62+
}
63+
}

0 commit comments

Comments
 (0)