Skip to content

Commit 43d1d4c

Browse files
committed
修改集合类型缓存资源字段支持.
1 parent 84a2899 commit 43d1d4c

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

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: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
import org.minbox.framework.api.boot.plugin.resource.load.model.ResourcePushField;
2222
import org.slf4j.Logger;
2323
import org.slf4j.LoggerFactory;
24+
import org.springframework.util.ObjectUtils;
2425

2526
import java.lang.reflect.Method;
27+
import java.util.HashMap;
2628
import java.util.List;
29+
import java.util.Map;
2730
import java.util.concurrent.ConcurrentHashMap;
2831
import java.util.concurrent.ConcurrentMap;
2932

@@ -54,18 +57,20 @@ public class ApiBootResourceContext {
5457
* child map key -> resource field name
5558
* child map value -> resource field instance
5659
*/
57-
private static ConcurrentMap<String, ResourcePushField> RESOURCE_FIELDS = new ConcurrentHashMap();
60+
private static ConcurrentMap<String, Map<String, ResourcePushField>> RESOURCE_FIELDS = new ConcurrentHashMap();
5861

5962
/**
6063
* get push field from cache
6164
*
62-
* @param method method instance
65+
* @param method method instance
66+
* @param fieldName 字段名称
6367
* @return ResourceField list
6468
*/
65-
public static ResourcePushField getPushFieldFromCache(Method method) {
69+
public static ResourcePushField getPushFieldFromCache(Method method, String fieldName) {
6670
String methodName = formatterMethodName(method);
6771
logger.debug("Cache method [{}] push field from memory", methodName);
68-
return ApiBootResourceContext.RESOURCE_FIELDS.get(methodName);
72+
Map<String, ResourcePushField> resourcePushFieldMap = ApiBootResourceContext.RESOURCE_FIELDS.get(methodName);
73+
return ObjectUtils.isEmpty(resourcePushFieldMap) ? null : resourcePushFieldMap.get(fieldName);
6974
}
7075

7176
/**
@@ -74,10 +79,23 @@ public static ResourcePushField getPushFieldFromCache(Method method) {
7479
* @param method method instance
7580
* @param resourcePushField push fields
7681
*/
77-
public static void setPushFieldToCache(Method method, ResourcePushField resourcePushField) {
82+
public static void setPushFieldToCache(Method method, String fieldName, ResourcePushField resourcePushField) {
7883
String methodName = formatterMethodName(method);
7984
logger.debug("Cache method [{}] push field to memory", methodName);
80-
ApiBootResourceContext.RESOURCE_FIELDS.put(methodName, resourcePushField);
85+
Map<String, ResourcePushField> resourcePushFieldMap = ApiBootResourceContext.RESOURCE_FIELDS.get(methodName);
86+
// new map
87+
if (ObjectUtils.isEmpty(resourcePushFieldMap)) {
88+
resourcePushFieldMap = new HashMap(1) {
89+
{
90+
put(fieldName, resourcePushField);
91+
}
92+
};
93+
}
94+
//already have map
95+
else {
96+
resourcePushFieldMap.put(fieldName, resourcePushField);
97+
}
98+
ApiBootResourceContext.RESOURCE_FIELDS.put(methodName, resourcePushFieldMap);
8199
}
82100

83101
/**

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private static void push(Method method, List<ResourceField> resourceFields, Obje
117117
resourceFields.stream().forEach(resourceField -> {
118118
try {
119119
// source
120-
Field sourceField = getSourceField(method, objectClass, resourceField.source());
120+
Field sourceField = getSourceField(method, objectClass, resourceField.source(), resourceField.name());
121121
// target
122122
Field resourceFieldInstance = getResourceField(method, objectClass, resourceField.name());
123123

@@ -156,9 +156,9 @@ else if (resourceField.isList()) {
156156
* @return
157157
* @throws NoSuchFieldException
158158
*/
159-
private static Field getSourceField(Method method, Class objectClass, String sourceFieldName) throws NoSuchFieldException {
159+
private static Field getSourceField(Method method, Class objectClass, String sourceFieldName,String resourceFieldName) throws NoSuchFieldException {
160160
// cache from memory
161-
ResourcePushField resourcePushField = ApiBootResourceContext.getPushFieldFromCache(method);
161+
ResourcePushField resourcePushField = ApiBootResourceContext.getPushFieldFromCache(method, resourceFieldName);
162162
// if don't have source field from cache
163163
if (ObjectUtils.isEmpty(resourcePushField) || ObjectUtils.isEmpty(resourcePushField.getSourceField())) {
164164
Field sourceField = objectClass.getDeclaredField(sourceFieldName);
@@ -172,7 +172,7 @@ private static Field getSourceField(Method method, Class objectClass, String sou
172172
}
173173

174174
// cache to memory
175-
ApiBootResourceContext.setPushFieldToCache(method, resourcePushField);
175+
ApiBootResourceContext.setPushFieldToCache(method, resourceFieldName, resourcePushField);
176176
}
177177
return resourcePushField.getSourceField();
178178
}
@@ -186,7 +186,7 @@ private static Field getSourceField(Method method, Class objectClass, String sou
186186
*/
187187
private static Field getResourceField(Method method, Class objectClass, String resourceFieldName) throws NoSuchFieldException {
188188
// cache from memory
189-
ResourcePushField resourcePushField = ApiBootResourceContext.getPushFieldFromCache(method);
189+
ResourcePushField resourcePushField = ApiBootResourceContext.getPushFieldFromCache(method, resourceFieldName);
190190
// if don't have source field from cache
191191
if (ObjectUtils.isEmpty(resourcePushField) || ObjectUtils.isEmpty(resourcePushField.getResourceField())) {
192192
Field resourceFieldInstance = objectClass.getDeclaredField(resourceFieldName);
@@ -199,7 +199,7 @@ private static Field getResourceField(Method method, Class objectClass, String r
199199
resourcePushField.setResourceField(resourceFieldInstance);
200200
}
201201
// cache to memory
202-
ApiBootResourceContext.setPushFieldToCache(method, resourcePushField);
202+
ApiBootResourceContext.setPushFieldToCache(method, resourceFieldName, resourcePushField);
203203
}
204204

205205
return resourcePushField.getResourceField();

0 commit comments

Comments
 (0)