Skip to content

Commit bd26532

Browse files
author
Yang Guo
committed
optimize branch cache
1 parent 537a56c commit bd26532

File tree

5 files changed

+18
-35
lines changed

5 files changed

+18
-35
lines changed

platform-api/src/main/java/com/flow/platform/api/config/CachingConfig.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public class CachingConfig {
3333

3434
private final static int EXPIRE_CACHE_SECOND = 3600 * 24;
3535

36-
private final static int MAX_CACHE_NUM = 1000;
37-
36+
private final static int MAX_CACHE_NUM = 100;
3837

3938
private CacheBuilder cacheBuilder = CacheBuilder
4039
.newBuilder()

platform-api/src/main/java/com/flow/platform/api/controller/FlowController.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public BooleanValue isFlowNameExist() {
235235
/**
236236
* @api {get} /flows/:root/branches List Branches
237237
* @apiParam {String} root flow node name
238-
* @apiParam {refresh} [refresh] true or false
238+
* @apiParam {Boolean} [refresh] true or false, the default is false
239239
* @apiGroup Flows
240240
*
241241
* @apiSuccessExample {json} Success-Response
@@ -248,13 +248,13 @@ public BooleanValue isFlowNameExist() {
248248
*/
249249
@GetMapping("/{root}/branches")
250250
public List<String> listBranches(@RequestParam(required = false) Boolean refresh) {
251-
Node root = nodeService.find(currentNodePath.get());
252-
if (refresh != null && refresh == true) {
253-
return gitService.refreshBranches(root);
251+
if (refresh == null) {
252+
refresh = false;
254253
}
255-
return gitService.listBranches(root);
256-
}
257254

255+
Node root = nodeService.find(currentNodePath.get());
256+
return gitService.branches(root, refresh);
257+
}
258258

259259
/**
260260
* @api {get} /flows/:root/tags List Tags

platform-api/src/main/java/com/flow/platform/api/service/GitService.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,7 @@ interface ProgressListener {
6161
/**
6262
* Fetch branches from git repo
6363
*/
64-
List<String> listBranches(Node node);
65-
66-
/**
67-
* refresh branches
68-
*/
69-
List<String> refreshBranches(Node node);
64+
List<String> branches(Node node, boolean refresh);
7065

7166
/**
7267
* Fetch tags from git repo

platform-api/src/main/java/com/flow/platform/api/service/GitServiceImpl.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
import javax.annotation.PostConstruct;
4343
import org.eclipse.jgit.lib.ProgressMonitor;
4444
import org.springframework.beans.factory.annotation.Autowired;
45-
import org.springframework.cache.CacheManager;
46-
import org.springframework.cache.annotation.CachePut;
4745
import org.springframework.cache.annotation.Cacheable;
4846
import org.springframework.stereotype.Service;
4947

@@ -103,18 +101,9 @@ public String fetch(Node node, String filePath, ProgressListener progressListene
103101
return client.fetch(branch, filePath, new GitCloneProgressMonitor(progressListener));
104102
}
105103

106-
107-
@Cacheable(value = "branches", key = "#node.getPath()")
108-
public List<String> listBranches(Node node) {
109-
return fetchBranches(node);
110-
}
111-
112-
@CachePut(value = "branches", key = "#node.getPath()")
113-
public List<String> refreshBranches(Node node) {
114-
return fetchBranches(node);
115-
}
116-
117-
private List<String> fetchBranches(Node node) {
104+
@Override
105+
@Cacheable(value = "git.branches", key = "#node.getPath()", condition = "#refresh == false")
106+
public List<String> branches(Node node, boolean refresh) {
118107
GitClient client = gitClientInstance(node);
119108
try {
120109
return client.branches();

platform-api/src/test/java/com/flow/platform/api/test/service/GitServiceTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,18 @@ public void onFinishTask(String task) {
9494

9595
@Test
9696
public void should_list_branches_of_git_repo() {
97-
List<String> branches = gitService.listBranches(node);
97+
List<String> branches = gitService.branches(node, false);
9898
Assert.assertNotNull(branches);
9999
Assert.assertEquals("develop", branches.get(0));
100100
Assert.assertEquals("master", branches.get(1));
101-
}
102101

103-
@Test
104-
public void should_refresh_branches_of_git_repo() {
105-
List<String> branches = gitService.refreshBranches(node);
102+
// should load branches from cache
103+
branches = gitService.branches(node, false);
104+
Assert.assertNotNull(branches);
105+
106+
// should load branched from git repo for refresh
107+
branches = gitService.branches(node, true);
106108
Assert.assertNotNull(branches);
107-
Assert.assertEquals("develop", branches.get(0));
108-
Assert.assertEquals("master", branches.get(1));
109109
}
110110

111111
@Test

0 commit comments

Comments
 (0)