Skip to content

Commit 3f3234c

Browse files
committed
add cache annotation
1 parent 9122736 commit 3f3234c

File tree

15 files changed

+189
-220
lines changed

15 files changed

+189
-220
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ dependencies {
3737
compile 'javax.servlet:jstl:1.2'
3838
compile 'javax.inject:javax.inject:1'
3939
compile 'org.atmosphere:atmosphere-runtime:2.4.4'
40-
compile 'io.netty:netty-all:4.1.2.Final'
40+
//compile 'io.netty:netty-all:4.1.2.Final'
4141

4242

4343

src/main/java/org/nlpcn/jcoder/run/annotation/Cache.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import java.lang.annotation.Target;
88

99
/**
10-
* make cache by action
10+
* make cache by action, if you used it , it only use method args by cache key ,
11+
* so not use request or response !
1112
*
1213
* @author ansj
1314
*
@@ -17,7 +18,7 @@
1718
@Documented
1819
public @interface Cache {
1920

20-
int time() default 1000; // SECONDS
21+
int time() default 10; // SECONDS
2122

2223
int size() default 1000;
2324

src/main/java/org/nlpcn/jcoder/run/mvc/ApiActionChainMaker.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
import org.nlpcn.jcoder.run.mvc.processor.ApiAdaptorProcessor;
7+
import org.nlpcn.jcoder.run.mvc.processor.ApiFailProcessor;
8+
import org.nlpcn.jcoder.run.mvc.processor.ApiMethodInvokeProcessor;
9+
import org.nlpcn.jcoder.run.mvc.processor.ApiModuleProcessor;
10+
import org.nlpcn.jcoder.run.mvc.processor.ApiViewProcessor;
611
import org.nutz.mvc.ActionChain;
712
import org.nutz.mvc.ActionChainMaker;
813
import org.nutz.mvc.ActionInfo;
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package org.nlpcn.jcoder.run.mvc.cache;
2+
3+
import java.util.Arrays;
4+
import java.util.Objects;
5+
6+
import javax.servlet.ServletContext;
7+
import javax.servlet.ServletRequest;
8+
import javax.servlet.ServletResponse;
9+
import javax.servlet.http.HttpSession;
10+
11+
import org.nutz.json.Json;
12+
import org.nutz.mvc.adaptor.injector.SessionInjector;
13+
14+
import com.alibaba.fastjson.JSON;
15+
16+
import jdk.nashorn.internal.ir.RuntimeNode.Request;
17+
18+
public class Args {
19+
20+
private Object[] args;
21+
22+
public Args(Object[] args) {
23+
this.args = args;
24+
}
25+
26+
public Object[] getArgs() {
27+
return args;
28+
}
29+
30+
public void setArgs(Object[] args) {
31+
this.args = args;
32+
}
33+
34+
public static Args create(Object[] args) {
35+
return new Args(args);
36+
}
37+
38+
@Override
39+
public int hashCode() {
40+
return Objects.hash(args);
41+
}
42+
43+
@Override
44+
public boolean equals(Object obj) {
45+
if (!(obj instanceof Args)) {
46+
return false;
47+
}
48+
49+
Object[] temp = ((Args) obj).getArgs();
50+
51+
if (temp.length != args.length) {
52+
return false;
53+
}
54+
55+
for (int i = 0; i < temp.length; i++) {
56+
57+
if (Objects.deepEquals(args[i], temp[i])) {
58+
continue;
59+
}
60+
if (jsonEqual(args[i], temp[i])) {
61+
continue;
62+
}
63+
64+
return false;
65+
}
66+
67+
return true;
68+
}
69+
70+
@Override
71+
public String toString() {
72+
return Arrays.toString(args);
73+
}
74+
75+
private boolean jsonEqual(Object o1, Object o2) {
76+
77+
// in deepequals , must not be null at the same time
78+
if (o1 == null || o2 == null) {
79+
return false;
80+
}
81+
82+
if (!o1.getClass().equals(o2.getClass())) {
83+
return false;
84+
}
85+
86+
if (o1 instanceof ServletRequest) {
87+
return true;
88+
}
89+
90+
if (o1 instanceof ServletResponse) {
91+
return true;
92+
}
93+
94+
if (o1 instanceof HttpSession) {
95+
return true;
96+
}
97+
98+
if (o1 instanceof ServletContext) {
99+
return true;
100+
}
101+
102+
return Json.toJson(o1).equals(JSON.toJSON(o2));
103+
}
104+
105+
public static void main(String[] args) {
106+
Object temp = new Object[0];
107+
108+
System.out.println(temp instanceof Object[]);
109+
}
110+
}

src/main/java/org/nlpcn/jcoder/run/mvc/CacheEntry.java renamed to src/main/java/org/nlpcn/jcoder/run/mvc/cache/CacheEntry.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
package org.nlpcn.jcoder.run.mvc;
1+
package org.nlpcn.jcoder.run.mvc.cache;
22

33
import java.lang.reflect.Method;
4-
import java.util.Arrays;
54
import java.util.concurrent.Callable;
65
import java.util.concurrent.ExecutionException;
76
import java.util.concurrent.Executors;
@@ -29,6 +28,8 @@ public class CacheEntry {
2928

3029
private static final Logger LOG = Logger.getLogger(CacheEntry.class);
3130

31+
public static final Object NULL = new Object();
32+
3233
private Method method;
3334

3435
// 缓存时间
@@ -43,10 +44,9 @@ public class CacheEntry {
4344
// 是否责塞
4445
private boolean block = true;
4546

46-
private LoadingCache<Object[], Object> cache = null;
47+
private LoadingCache<Args, Object> cache = null;
4748

4849
public CacheEntry(Task task, Method method, int time, int size, boolean block) {
49-
5050
this.task = task;
5151

5252
if (size > 0) {
@@ -64,24 +64,24 @@ public CacheEntry(Task task, Method method, int time, int size, boolean block) {
6464
}
6565

6666
if (this.block) {
67-
cache = CacheBuilder.newBuilder().maximumSize(this.size).softValues().refreshAfterWrite(time, TimeUnit.SECONDS).build(new CacheLoader<Object[], Object>() {
67+
cache = CacheBuilder.newBuilder().maximumSize(this.size).softValues().refreshAfterWrite(time, TimeUnit.SECONDS).build(new CacheLoader<Args, Object>() {
6868
@Override
69-
public Object load(Object[] args) {
69+
public Object load(Args args) {
7070
return executeNoCache(args);
7171
}
7272
});
7373

7474
} else {
75-
cache = CacheBuilder.newBuilder().maximumSize(this.size).refreshAfterWrite(time, TimeUnit.SECONDS).build(new CacheLoader<Object[], Object>() {
75+
cache = CacheBuilder.newBuilder().maximumSize(this.size).refreshAfterWrite(time, TimeUnit.SECONDS).build(new CacheLoader<Args, Object>() {
7676

77-
public Object load(Object[] args) {
77+
public Object load(Args args) {
7878
return executeNoCache(args);
7979
}
8080

8181
private ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
8282

8383
@Override
84-
public ListenableFuture<Object> reload(final Object[] args, Object oldValue) {
84+
public ListenableFuture<Object> reload(final Args args, Object oldValue) {
8585
ListenableFuture<Object> result = ListenableFutureTask.create(new Callable<Object>() {
8686
public Object call() {
8787
return executeNoCache(args);
@@ -99,20 +99,24 @@ public Object call() {
9999

100100
});
101101
}
102+
102103
}
103104

104-
public Object execute(Object[] args) throws ExecutionException {
105+
public Object execute(Object[] param) throws ExecutionException {
106+
107+
Args args = Args.create(param);
108+
105109
long start = System.currentTimeMillis();
106110
try {
107111
return cache.get(args);
108112
} finally {
109-
LOG.info("by cache action " + this.task.getName() + "/" + this.method.getName() + " Param:" + Arrays.toString(args) + " ok ! use time : "
110-
+ (System.currentTimeMillis() - start));
113+
LOG.info("by cache action " + this.task.getName() + "/" + this.method.getName() + " Param:" + args + " ok ! use time : " + (System.currentTimeMillis() - start));
111114
}
112115
}
113116

114-
private Object executeNoCache(Object[] args) {
115-
return new JavaRunner(task).compile().instance().execute(method, args);
117+
private Object executeNoCache(Args args) {
118+
Object result = new JavaRunner(task).compile().instance().execute(method, args.getArgs());
119+
return result == null ? NULL : result;
116120
}
117121

118122
}

src/main/java/org/nlpcn/jcoder/run/mvc/ApiAdaptorProcessor.java renamed to src/main/java/org/nlpcn/jcoder/run/mvc/processor/ApiAdaptorProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package org.nlpcn.jcoder.run.mvc;
1+
package org.nlpcn.jcoder.run.mvc.processor;
22

3+
import org.nlpcn.jcoder.run.mvc.ApiPairAdaptor;
34
import org.nutz.mvc.ActionContext;
45
import org.nutz.mvc.ActionInfo;
56
import org.nutz.mvc.HttpAdaptor;

src/main/java/org/nlpcn/jcoder/run/mvc/ApiFailProcessor.java renamed to src/main/java/org/nlpcn/jcoder/run/mvc/processor/ApiFailProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.nlpcn.jcoder.run.mvc;
1+
package org.nlpcn.jcoder.run.mvc.processor;
22

33
import org.nlpcn.jcoder.run.mvc.view.JsonView;
44
import org.nlpcn.jcoder.util.StaticValue;

src/main/java/org/nlpcn/jcoder/run/mvc/ApiMethodInvokeProcessor.java renamed to src/main/java/org/nlpcn/jcoder/run/mvc/processor/ApiMethodInvokeProcessor.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
1-
package org.nlpcn.jcoder.run.mvc;
1+
package org.nlpcn.jcoder.run.mvc.processor;
22

33
import java.lang.reflect.InvocationTargetException;
44
import java.lang.reflect.Method;
55
import java.util.Date;
66
import java.util.concurrent.atomic.AtomicLong;
77

88
import org.nlpcn.jcoder.domain.Task;
9+
import org.nlpcn.jcoder.run.annotation.Cache;
910
import org.nlpcn.jcoder.run.java.JavaRunner;
11+
import org.nlpcn.jcoder.run.mvc.cache.CacheEntry;
1012
import org.nlpcn.jcoder.scheduler.ThreadManager;
1113
import org.nlpcn.jcoder.util.DateUtils;
1214
import org.nutz.lang.Lang;
1315
import org.nutz.mvc.ActionContext;
16+
import org.nutz.mvc.ActionInfo;
17+
import org.nutz.mvc.NutConfig;
1418
import org.nutz.mvc.impl.processor.AbstractProcessor;
1519

1620
public class ApiMethodInvokeProcessor extends AbstractProcessor {
1721

1822
private AtomicLong al = new AtomicLong();
1923

24+
private Cache cache;
25+
26+
private CacheEntry cacheEntry;
27+
28+
@Override
29+
public void init(NutConfig config, ActionInfo ai) throws Throwable {
30+
super.init(config, ai);
31+
cache = ai.getMethod().getAnnotation(Cache.class);
32+
}
33+
2034
public void process(ActionContext ac) throws Throwable {
2135
String threadName = null;
2236
Task module = (Task) ac.getModule();
@@ -25,7 +39,15 @@ public void process(ActionContext ac) throws Throwable {
2539
try {
2640
threadName = module.getName() + "@" + ac.getRequest().getRemoteAddr() + "@" + DateUtils.formatDate(new Date(), "yyyyMMddHHmmss") + "@" + al.getAndIncrement();
2741
ThreadManager.add2ActionTask(threadName, Thread.currentThread());
28-
Object result = executeByCache(module, method, args);
42+
Object result = null;
43+
if (cache == null) {
44+
result = new JavaRunner(module).compile().instance().execute(method, args);
45+
} else {
46+
result = getCache(module, method).execute(args);
47+
if (result == CacheEntry.NULL) {
48+
result = null;
49+
}
50+
}
2951
ac.setMethodReturn(result);
3052
doNext(ac);
3153
} catch (IllegalAccessException e) {
@@ -39,7 +61,17 @@ public void process(ActionContext ac) throws Throwable {
3961
}
4062
}
4163

42-
private Object executeByCache(Task module, Method method, Object[] args) {
43-
return new JavaRunner(module).compile().instance().execute(method, args);
64+
private CacheEntry getCache(Task module, Method method) {
65+
66+
if (cacheEntry == null) {
67+
synchronized (al) {
68+
if (cacheEntry == null) {
69+
cacheEntry = new CacheEntry(module, method, cache.time(), cache.size(), cache.block());
70+
}
71+
}
72+
}
73+
74+
return cacheEntry;
4475
}
76+
4577
}

src/main/java/org/nlpcn/jcoder/run/mvc/ApiModuleProcessor.java renamed to src/main/java/org/nlpcn/jcoder/run/mvc/processor/ApiModuleProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.nlpcn.jcoder.run.mvc;
1+
package org.nlpcn.jcoder.run.mvc.processor;
22

33
import java.lang.reflect.Method;
44

src/main/java/org/nlpcn/jcoder/run/mvc/ApiViewProcessor.java renamed to src/main/java/org/nlpcn/jcoder/run/mvc/processor/ApiViewProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.nlpcn.jcoder.run.mvc;
1+
package org.nlpcn.jcoder.run.mvc.processor;
22

33
import org.nlpcn.jcoder.run.mvc.view.JsonView;
44
import org.nlpcn.jcoder.run.mvc.view.JsonpView;

0 commit comments

Comments
 (0)