Skip to content

Commit 17344e4

Browse files
authored
代码加了注释 (#20)
代码加了注释
2 parents 028d24a + 8c06a03 commit 17344e4

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/target/
2+
.classpath
3+
.project
4+
.settings/
5+
*.bak
6+
.idea/

src/main/java/com/crossoverjie/distributed/annotation/ControllerLimit.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@
77
@Documented
88
public @interface ControllerLimit {
99

10-
10+
/**
11+
* Error code
12+
* @return
13+
* code
14+
*/
15+
int errorCode() default 500;
1116
}

src/main/java/com/crossoverjie/distributed/intercept/WebIntercept.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
6161
boolean limit = redisLimit.limit();
6262
if (!limit) {
6363
logger.warn("request has bean limited");
64-
response.sendError(500, "request limited");
64+
response.sendError(annotation.errorCode(), "request limited");
6565
return false;
6666
}
6767

src/main/java/com/crossoverjie/distributed/lock/RedisLock.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,15 @@ private Object getConnection() {
7474
}
7575

7676
/**
77-
* Non-blocking lock
77+
* Non-blocking lock, default timeout :10s
7878
*
7979
* @param key lock business type
8080
* @param request value
8181
* @return true lock success
8282
* false lock fail
8383
*/
8484
public boolean tryLock(String key, String request) {
85-
//get connection
86-
Object connection = getConnection();
87-
String result ;
88-
if (connection instanceof Jedis){
89-
result = ((Jedis) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);
90-
((Jedis) connection).close();
91-
}else {
92-
result = ((JedisCluster) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);
93-
94-
}
95-
96-
if (LOCK_MSG.equals(result)) {
97-
return true;
98-
} else {
99-
return false;
100-
}
85+
return tryLock(key,request,10*TIME);
10186
}
10287

10388
/**

0 commit comments

Comments
 (0)