Skip to content

Commit 31f2c01

Browse files
committed
ApiBoot Resource Load Redis方式集成
1 parent 4210b87 commit 31f2c01

File tree

7 files changed

+210
-40
lines changed

7 files changed

+210
-40
lines changed

api-boot-project/api-boot-plugins/api-boot-plugin-resource-load/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@
2222
<groupId>org.minbox.framework</groupId>
2323
<artifactId>api-boot-plugin</artifactId>
2424
</dependency>
25+
<!--Spring Boot Aop-->
2526
<dependency>
2627
<groupId>org.springframework.boot</groupId>
2728
<artifactId>spring-boot-starter-aop</artifactId>
2829
<optional>true</optional>
2930
</dependency>
31+
<!--Spring Boot Redis-->
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-data-redis</artifactId>
35+
<optional>true</optional>
36+
</dependency>
3037
</dependencies>
3138
</project>

api-boot-project/api-boot-plugins/api-boot-plugin-resource-load/src/main/java/org/minbox/framework/api/boot/plugin/resource/load/aop/interceptor/ApiBootResourceLoadMethodInterceptor.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818

1919
import org.aopalliance.intercept.MethodInterceptor;
2020
import org.aopalliance.intercept.MethodInvocation;
21-
import org.minbox.framework.api.boot.common.exception.ApiBootException;
22-
import org.minbox.framework.api.boot.plugin.resource.load.ApiBootResourceStoreDelegate;
23-
import org.minbox.framework.api.boot.plugin.resource.load.pusher.ResourcePusher;
2421
import org.minbox.framework.api.boot.plugin.resource.load.annotation.ResourceLoad;
22+
import org.minbox.framework.api.boot.plugin.resource.load.pusher.ApiBootResourcePusher;
2523
import org.slf4j.Logger;
2624
import org.slf4j.LoggerFactory;
2725
import org.springframework.aop.support.AopUtils;
@@ -50,16 +48,13 @@ public class ApiBootResourceLoadMethodInterceptor implements MethodInterceptor {
5048
static Logger logger = LoggerFactory.getLogger(ApiBootResourceLoadMethodInterceptor.class);
5149

5250
/**
53-
* ApiBoot Resource Load Data Store
51+
* ApiBoot Resource Pusher
5452
* Use to query resource url
5553
*/
56-
private ApiBootResourceStoreDelegate resourceStoreDelegate;
54+
private ApiBootResourcePusher apiBootResourcePusher;
5755

58-
public ApiBootResourceLoadMethodInterceptor(ApiBootResourceStoreDelegate resourceStoreDelegate) {
59-
this.resourceStoreDelegate = resourceStoreDelegate;
60-
if (ObjectUtils.isEmpty(this.resourceStoreDelegate)) {
61-
throw new ApiBootException("Unable to load [ApiBootResourceStoreDelegate] implementation class instance");
62-
}
56+
public ApiBootResourceLoadMethodInterceptor(ApiBootResourcePusher apiBootResourcePusher) {
57+
this.apiBootResourcePusher = apiBootResourcePusher;
6358
}
6459

6560
/**
@@ -87,7 +82,7 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
8782
ResourceLoad resourceLoad = declaredMethod.getDeclaredAnnotation(ResourceLoad.class);
8883
if (!ObjectUtils.isEmpty(resourceLoad)) {
8984
// resource push
90-
ResourcePusher.pushResource(resourceStoreDelegate, declaredMethod, result);
85+
apiBootResourcePusher.pushResource(declaredMethod, result);
9186
}
9287
}
9388
return result;

api-boot-project/api-boot-plugins/api-boot-plugin-resource-load/src/main/java/org/minbox/framework/api/boot/plugin/resource/load/context/ApiBootResourceContext.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,19 @@ public static String formatterMethodName(Method method) {
132132
String expression = "%s.%s";
133133
return String.format(expression, method.getDeclaringClass().getName(), method.getName());
134134
}
135+
136+
/**
137+
* formatter redis key
138+
*
139+
* @param method method instance
140+
* @param sourceFieldValue source field value
141+
* @param resourceType resource type
142+
* @return redis key
143+
*/
144+
public static String formatterRedisCacheKey(Method method, String sourceFieldValue, String resourceType) {
145+
String methodName = formatterMethodName(method);
146+
String expression = "%s.%s.%s";
147+
return String.format(expression, methodName, resourceType, sourceFieldValue);
148+
}
135149
}
136150

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,30 @@
1717

1818
package org.minbox.framework.api.boot.plugin.resource.load.pusher;
1919

20+
import org.minbox.framework.api.boot.common.exception.ApiBootException;
21+
22+
import java.lang.reflect.Method;
23+
2024
/**
21-
* Redis away push resource url
25+
* ApiBoot Resource Pusher Interface
2226
*
2327
* @author:恒宇少年 - 于起宇
2428
* <p>
25-
* DateTime:2019-04-12 17:10
29+
* DateTime:2019-04-19 09:32
2630
* Blog:http://blog.yuqiyu.com
2731
* WebSite:http://www.jianshu.com/u/092df3f77bca
2832
* Gitee:https://gitee.com/hengboy
2933
* GitHub:https://github.com/hengboy
34+
* @see org.minbox.framework.api.boot.plugin.resource.load.pusher.support.ApiBootJdbcResourcePusher
35+
* @see org.minbox.framework.api.boot.plugin.resource.load.pusher.support.ApiBootRedisResourcePusher
3036
*/
31-
public class ResourceRedisPusher extends ResourcePusher {
32-
//...
37+
public interface ApiBootResourcePusher {
38+
/**
39+
* Push resource to result field
40+
*
41+
* @param declaredMethod declared method
42+
* @param methodExecuteResult method execute result
43+
* @throws ApiBootException
44+
*/
45+
void pushResource(Method declaredMethod, Object methodExecuteResult) throws ApiBootException;
3346
}
Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
*
1616
*/
1717

18-
package org.minbox.framework.api.boot.plugin.resource.load.pusher;
18+
package org.minbox.framework.api.boot.plugin.resource.load.pusher.support;
1919

2020
import org.minbox.framework.api.boot.common.exception.ApiBootException;
21-
import org.minbox.framework.api.boot.plugin.resource.load.ApiBootResourceStoreDelegate;
2221
import org.minbox.framework.api.boot.plugin.resource.load.annotation.ResourceField;
2322
import org.minbox.framework.api.boot.plugin.resource.load.context.ApiBootResourceContext;
2423
import org.minbox.framework.api.boot.plugin.resource.load.loader.ResourceFieldLoader;
2524
import org.minbox.framework.api.boot.plugin.resource.load.model.ResourcePushField;
25+
import org.minbox.framework.api.boot.plugin.resource.load.pusher.ApiBootResourcePusher;
2626
import org.springframework.util.ObjectUtils;
2727
import org.springframework.util.ReflectionUtils;
2828

@@ -32,42 +32,35 @@
3232
import java.util.Map;
3333

3434
/**
35-
* ApiBoot Resource Load Pusher
36-
*
3735
* @author:恒宇少年 - 于起宇
3836
* <p>
39-
* DateTime:2019-04-12 16:27
37+
* DateTime:2019-04-19 09:40
4038
* Blog:http://blog.yuqiyu.com
4139
* WebSite:http://www.jianshu.com/u/092df3f77bca
4240
* Gitee:https://gitee.com/hengboy
4341
* GitHub:https://github.com/hengboy
4442
*/
45-
public class ResourcePusher {
46-
/**
47-
* ApiBoot Resource Load Data Store
48-
* Use to query resource url
49-
*/
50-
private static ApiBootResourceStoreDelegate resourceStoreDelegate;
43+
public abstract class ApiBootAbstractResourcePusher implements ApiBootResourcePusher {
5144

5245
/**
5346
* unified push resource
5447
*
5548
* @param method method
5649
* @param result method execute result
5750
*/
58-
public static void pushResource(ApiBootResourceStoreDelegate resourceStoreDelegate, Method method, Object result) {
59-
ResourcePusher.resourceStoreDelegate = resourceStoreDelegate;
51+
@Override
52+
public void pushResource(Method method, Object result) {
6053
// list
6154
if (result instanceof List) {
62-
ResourcePusher.pushToList(method, (List<Object>) result);
55+
pushToList(method, (List<Object>) result);
6356
}
6457
// map
6558
else if (result instanceof Map) {
66-
ResourcePusher.pushToMap(method, (Map) result);
59+
pushToMap(method, (Map) result);
6760
}
6861
// single
6962
else if (result instanceof Object) {
70-
ResourcePusher.pushToObject(method, result);
63+
pushToObject(method, result);
7164
}
7265
}
7366

@@ -78,7 +71,7 @@ else if (result instanceof Object) {
7871
* @param executeResultList method execute result list
7972
* @throws Exception Exception
8073
*/
81-
private static void pushToList(Method method, List<Object> executeResultList) {
74+
private void pushToList(Method method, List<Object> executeResultList) {
8275
List<ResourceField> resourceFields = getResourceFields(method);
8376
executeResultList.stream().forEach(o -> push(method, resourceFields, o));
8477
}
@@ -90,7 +83,7 @@ private static void pushToList(Method method, List<Object> executeResultList) {
9083
* @param executeResultMap method execute result map
9184
* @throws Exception Exception
9285
*/
93-
private static void pushToMap(Method method, Map executeResultMap) {
86+
private void pushToMap(Method method, Map executeResultMap) {
9487
List<ResourceField> resourceFields = getResourceFields(method);
9588
executeResultMap.keySet().stream().forEach(o -> push(method, resourceFields, executeResultMap.get(o)));
9689
}
@@ -101,19 +94,28 @@ private static void pushToMap(Method method, Map executeResultMap) {
10194
* @param method method
10295
* @param executeResult method execute result object
10396
*/
104-
private static void pushToObject(Method method, Object executeResult) {
97+
private void pushToObject(Method method, Object executeResult) {
10598
List<ResourceField> resourceFields = getResourceFields(method);
10699
push(method, resourceFields, executeResult);
107100
}
108101

102+
/**
103+
* load resource url
104+
*
105+
* @param declaredMethod declared method
106+
* @param sourceFieldValue sourceFieldValue
107+
* @param resourceType resourceType
108+
* @return resource list
109+
*/
110+
public abstract List<String> loadResourceUrl(Method declaredMethod, String sourceFieldValue, String resourceType);
109111

110112
/**
111113
* execute push
112114
*
113115
* @param resourceFields ResourceField Annotation List
114116
* @param object single object
115117
*/
116-
private static void push(Method method, List<ResourceField> resourceFields, Object object) {
118+
private void push(Method method, List<ResourceField> resourceFields, Object object) {
117119
Class objectClass = object.getClass();
118120
resourceFields.stream().forEach(resourceField -> {
119121
try {
@@ -125,7 +127,7 @@ private static void push(Method method, List<ResourceField> resourceFields, Obje
125127
// get source filed value
126128
Object sourceFieldValue = sourceField.get(object);
127129
// load resource urls
128-
List<String> resourceUrls = resourceStoreDelegate.loadResourceUrl(String.valueOf(sourceFieldValue), resourceField.type());
130+
List<String> resourceUrls = loadResourceUrl(method, String.valueOf(sourceFieldValue), resourceField.type());
129131

130132
if (!ObjectUtils.isEmpty(resourceUrls)) {
131133
// resource field is array
@@ -157,7 +159,7 @@ else if (resourceField.isList()) {
157159
* @return Field Instance
158160
* @throws NoSuchFieldException No Such Field Exception
159161
*/
160-
private static Field getSourceField(Method method, Class objectClass, String sourceFieldName, String resourceFieldName) throws NoSuchFieldException {
162+
private Field getSourceField(Method method, Class objectClass, String sourceFieldName, String resourceFieldName) throws NoSuchFieldException {
161163
// cache from memory
162164
ResourcePushField resourcePushField = ApiBootResourceContext.getPushFieldFromCache(method, resourceFieldName);
163165
// if don't have source field from cache
@@ -185,7 +187,7 @@ private static Field getSourceField(Method method, Class objectClass, String sou
185187
* @param resourceFieldName resource field name
186188
* @return Field
187189
*/
188-
private static Field getResourceField(Method method, Class objectClass, String resourceFieldName) throws NoSuchFieldException {
190+
private Field getResourceField(Method method, Class objectClass, String resourceFieldName) throws NoSuchFieldException {
189191
// cache from memory
190192
ResourcePushField resourcePushField = ApiBootResourceContext.getPushFieldFromCache(method, resourceFieldName);
191193
// if don't have source field from cache
@@ -212,7 +214,7 @@ private static Field getResourceField(Method method, Class objectClass, String r
212214
* @param method method
213215
* @return ResourceField List
214216
*/
215-
private static List<ResourceField> loadMethodResourceFields(Method method) {
217+
private List<ResourceField> loadMethodResourceFields(Method method) {
216218
// load method declared ResourceField Annotation List
217219
List<ResourceField> resourceFields = ResourceFieldLoader.getDeclaredResourceField(method);
218220
return resourceFields;
@@ -226,7 +228,7 @@ private static List<ResourceField> loadMethodResourceFields(Method method) {
226228
* @param method method
227229
* @return List ResourceField
228230
*/
229-
private static List<ResourceField> getResourceFields(Method method) {
231+
private List<ResourceField> getResourceFields(Method method) {
230232
// get from cache
231233
List<ResourceField> resourceFields = ApiBootResourceContext.getResourceFieldFromCache(method);
232234

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
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+
18+
package org.minbox.framework.api.boot.plugin.resource.load.pusher.support;
19+
20+
import org.minbox.framework.api.boot.common.exception.ApiBootException;
21+
import org.minbox.framework.api.boot.plugin.resource.load.ApiBootResourceStoreDelegate;
22+
import org.springframework.util.ObjectUtils;
23+
24+
import java.lang.reflect.Method;
25+
import java.util.List;
26+
27+
/**
28+
* ApiBoot Jdbc Resource Pusher
29+
*
30+
* @author:恒宇少年 - 于起宇
31+
* <p>
32+
* DateTime:2019-04-19 09:33
33+
* Blog:http://blog.yuqiyu.com
34+
* WebSite:http://www.jianshu.com/u/092df3f77bca
35+
* Gitee:https://gitee.com/hengboy
36+
* GitHub:https://github.com/hengboy
37+
*/
38+
public class ApiBootJdbcResourcePusher extends ApiBootAbstractResourcePusher {
39+
/**
40+
* ApiBoot Resource Store Delegate
41+
* Use load resource url from jdbc
42+
*/
43+
private ApiBootResourceStoreDelegate apiBootResourceStoreDelegate;
44+
45+
public ApiBootJdbcResourcePusher(ApiBootResourceStoreDelegate apiBootResourceStoreDelegate) {
46+
this.apiBootResourceStoreDelegate = apiBootResourceStoreDelegate;
47+
if (ObjectUtils.isEmpty(this.apiBootResourceStoreDelegate)) {
48+
throw new ApiBootException("Unable to load [ApiBootResourceStoreDelegate] implementation class instance");
49+
}
50+
}
51+
52+
/**
53+
* load resource from ApiBootResourceStoreDelegate#loadResourceUrl method
54+
*
55+
* @param declaredMethod declared method
56+
* @param sourceFieldValue sourceFieldValue
57+
* @param resourceType resourceType
58+
* @return resource List
59+
* @see ApiBootResourceStoreDelegate
60+
*/
61+
@Override
62+
public List<String> loadResourceUrl(Method declaredMethod, String sourceFieldValue, String resourceType) {
63+
return apiBootResourceStoreDelegate.loadResourceUrl(sourceFieldValue, resourceType);
64+
}
65+
}

0 commit comments

Comments
 (0)