Skip to content

Commit 1a4295e

Browse files
author
yang.guo
authored
Merge pull request #106 from FlowCI/feature/api/branches_cache
add branches cache
2 parents a9d31b7 + 69f8f7a commit 1a4295e

File tree

8 files changed

+91
-12
lines changed

8 files changed

+91
-12
lines changed

platform-api/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
<artifactId>gson</artifactId>
6666
</dependency>
6767

68+
<dependency>
69+
<groupId>org.springframework</groupId>
70+
<artifactId>spring-context-support</artifactId>
71+
</dependency>
72+
6873
<dependency>
6974
<groupId>org.hibernate</groupId>
7075
<artifactId>hibernate-validator</artifactId>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2017 flow.ci
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.flow.platform.api.config;
18+
19+
import com.google.common.cache.CacheBuilder;
20+
import java.util.concurrent.TimeUnit;
21+
import org.springframework.cache.CacheManager;
22+
import org.springframework.cache.annotation.EnableCaching;
23+
import org.springframework.cache.guava.GuavaCacheManager;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
27+
/**
28+
* @author yh@firim
29+
*/
30+
@Configuration
31+
@EnableCaching
32+
public class CachingConfig {
33+
34+
private final static int EXPIRE_CACHE_SECOND = 3600 * 24;
35+
36+
private final static int MAX_CACHE_NUM = 100;
37+
38+
private CacheBuilder cacheBuilder = CacheBuilder
39+
.newBuilder()
40+
.expireAfterAccess(EXPIRE_CACHE_SECOND, TimeUnit.SECONDS)
41+
.maximumSize(MAX_CACHE_NUM);
42+
43+
44+
@Bean
45+
public CacheManager cacheManager() {
46+
GuavaCacheManager guavaCacheManager = new GuavaCacheManager();
47+
guavaCacheManager.setCacheBuilder(cacheBuilder);
48+
return guavaCacheManager;
49+
}
50+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"com.flow.platform.api.util",
6565
"com.flow.platform.api.consumer",
6666
"com.flow.platform.api.initializers"})
67-
@Import({AppConfig.class})
67+
@Import({AppConfig.class, CachingConfig.class})
6868
public class WebConfig extends WebMvcConfigurerAdapter {
6969

7070
private final static Gson GSON_CONFIG_FOR_RESPONE = new GsonBuilder()

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package com.flow.platform.api.controller;
1818

19-
import com.flow.platform.api.domain.permission.Actions;
2019
import com.flow.platform.api.domain.node.Flow;
2120
import com.flow.platform.api.domain.node.Node;
21+
import com.flow.platform.api.domain.permission.Actions;
2222
import com.flow.platform.api.domain.request.ListParam;
2323
import com.flow.platform.api.domain.response.BooleanValue;
2424
import com.flow.platform.api.domain.user.User;
@@ -235,6 +235,7 @@ public BooleanValue isFlowNameExist() {
235235
/**
236236
* @api {get} /flows/:root/branches List Branches
237237
* @apiParam {String} root flow node name
238+
* @apiParam {Boolean} [refresh] true or false, the default is false
238239
* @apiGroup Flows
239240
*
240241
* @apiSuccessExample {json} Success-Response
@@ -246,9 +247,13 @@ public BooleanValue isFlowNameExist() {
246247
* ]
247248
*/
248249
@GetMapping("/{root}/branches")
249-
public List<String> listBranches() {
250+
public List<String> listBranches(@RequestParam(required = false) Boolean refresh) {
251+
if (refresh == null) {
252+
refresh = false;
253+
}
254+
250255
Node root = nodeService.find(currentNodePath.get());
251-
return gitService.branches(root);
256+
return gitService.branches(root, refresh);
252257
}
253258

254259
/**
@@ -266,7 +271,7 @@ public List<String> listBranches() {
266271
@GetMapping("/{root}/tags")
267272
public List<String> listTags() {
268273
Node root = nodeService.find(currentNodePath.get());
269-
return gitService.tags(root);
274+
return gitService.tags(root, false);
270275
}
271276

272277
/**
@@ -415,7 +420,7 @@ public Node createFromYml(@RequestBody String yml) {
415420
*/
416421
@PostMapping("/{root}/users/auth")
417422
@WebSecurity(action = Actions.FLOW_AUTH)
418-
public List<User> flowAuthUsers(@RequestBody ListParam<String> listParam){
423+
public List<User> flowAuthUsers(@RequestBody ListParam<String> listParam) {
419424
return nodeService.authUsers(listParam.getArrays(), currentNodePath.get());
420425
}
421426
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ interface ProgressListener {
6161
/**
6262
* Fetch branches from git repo
6363
*/
64-
List<String> branches(Node node);
64+
List<String> branches(Node node, boolean refresh);
6565

6666
/**
6767
* Fetch tags from git repo
6868
*/
69-
List<String> tags(Node node);
69+
List<String> tags(Node node, boolean refresh);
7070

7171
/**
7272
* Fetch latest commit from git repo

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
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.annotation.Cacheable;
4546
import org.springframework.stereotype.Service;
4647

4748
/**
@@ -101,7 +102,8 @@ public String fetch(Node node, String filePath, ProgressListener progressListene
101102
}
102103

103104
@Override
104-
public List<String> branches(Node node) {
105+
@Cacheable(value = "git.branches", key = "#node.getPath()", condition = "#refresh == false")
106+
public List<String> branches(Node node, boolean refresh) {
105107
GitClient client = gitClientInstance(node);
106108
try {
107109
return client.branches();
@@ -111,7 +113,8 @@ public List<String> branches(Node node) {
111113
}
112114

113115
@Override
114-
public List<String> tags(Node node) {
116+
@Cacheable(value = "git.tags", key = "#node.getPath()", condition = "#refresh == false")
117+
public List<String> tags(Node node, boolean refresh) {
115118
GitClient client = gitClientInstance(node);
116119
try {
117120
return client.tags();

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

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

9595
@Test
9696
public void should_list_branches_of_git_repo() {
97-
List<String> branches = gitService.branches(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+
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);
108+
Assert.assertNotNull(branches);
101109
}
102110

103111
@Test
104112
public void should_list_tags_of_git_repo() {
105-
List<String> tags = gitService.tags(node);
113+
List<String> tags = gitService.tags(node, false);
106114
Assert.assertNotNull(tags);
107115
}
108116

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<javax.mail.version>1.5.5</javax.mail.version>
5858
<yamlbeans.version>1.12</yamlbeans.version>
5959
<velocity.version>1.5</velocity.version>
60+
<spring-context-support.version>4.3.8.RELEASE</spring-context-support.version>
6061
</properties>
6162

6263
<build>
@@ -102,6 +103,13 @@
102103

103104
<dependencyManagement>
104105
<dependencies>
106+
107+
<dependency>
108+
<groupId>org.springframework</groupId>
109+
<artifactId>spring-context-support</artifactId>
110+
<version>${spring-context-support.version}</version>
111+
</dependency>
112+
105113
<dependency>
106114
<groupId>javax.mail</groupId>
107115
<artifactId>javax.mail-api</artifactId>

0 commit comments

Comments
 (0)