Skip to content

Commit 3ec4009

Browse files
committed
调整ApiBoot Resource Load 资源更新相关细节
1 parent 60d67d7 commit 3ec4009

File tree

6 files changed

+95
-37
lines changed

6 files changed

+95
-37
lines changed

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: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
8787
logger.debug("Execute select resource.");
8888
if (!ObjectUtils.isEmpty(result)) {
8989
// resource push
90-
apiBootResourcePusher.pushResource(declaredMethod, result);
90+
apiBootResourcePusher.loadResource(declaredMethod, result);
9191
}
9292
break;
9393
case INSERT:
@@ -103,10 +103,6 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
103103
logger.debug("Execute update resource.");
104104
apiBootResourcePusher.updateResource(declaredMethod, params);
105105
break;
106-
case INSERT_OR_UPDATE:
107-
logger.debug("Execute insert or update resource.");
108-
apiBootResourcePusher.insertOrUpdateResource(declaredMethod, params);
109-
break;
110106
default:
111107
break;
112108
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,5 @@ public enum ResourceStoreEvent {
3030
/**
3131
* 删除资源
3232
*/
33-
DELETE,
34-
/**
35-
* 添加或更新资源
36-
*/
37-
INSERT_OR_UPDATE
33+
DELETE
3834
}

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public interface ApiBootResourcePusher {
4242
* @param methodExecuteResult method execute result
4343
* @throws ApiBootException ApiBoot Exception
4444
*/
45-
void pushResource(Method declaredMethod, Object methodExecuteResult) throws ApiBootException;
45+
void loadResource(Method declaredMethod, Object methodExecuteResult) throws ApiBootException;
4646

4747
/**
4848
* Pull resource from param
@@ -70,13 +70,4 @@ public interface ApiBootResourcePusher {
7070
* @throws ApiBootException ApiBoot Exception
7171
*/
7272
void updateResource(Method declaredMethod, Object[] param) throws ApiBootException;
73-
74-
/**
75-
* Insert resource
76-
*
77-
* @param declaredMethod declared method
78-
* @param param method param array
79-
* @throws ApiBootException ApiBoot Exception
80-
*/
81-
void insertOrUpdateResource(Method declaredMethod, Object[] param) throws ApiBootException;
8273
}

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

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,34 +144,25 @@ public void updateResource(Method declaredMethod, Object[] param) throws ApiBoot
144144
resourceFields.stream().forEach(resourceField -> {
145145
// get expression match source filed value
146146
Object sourceFieldValue = ResourceFieldTools.getMatchSourceValue(resourceField.source(), param);
147+
// get expression match resource field value
148+
Object resourceFieldValue = ResourceFieldTools.getMatchResourceValue(resourceField.name(), param);
147149
// if have value
148-
if (!ObjectUtils.isEmpty(sourceFieldValue)) {
150+
if (!ObjectUtils.isEmpty(sourceFieldValue) && !ObjectUtils.isEmpty(resourceFieldValue)) {
151+
List<String> resourceUrls = ListTools.convertToList(resourceFieldValue);
149152
// call implementation class "insertResourceUrl" method
150-
this.updateResourceUrl(declaredMethod, String.valueOf(sourceFieldValue), resourceField.type(), null);
153+
this.updateResourceUrl(declaredMethod, String.valueOf(sourceFieldValue), resourceField.type(), resourceUrls);
151154
}
152155
});
153156
}
154157

155-
/**
156-
* unified insert or update resource
157-
*
158-
* @param declaredMethod declared method
159-
* @param param method param array
160-
* @throws ApiBootException
161-
*/
162-
@Override
163-
public void insertOrUpdateResource(Method declaredMethod, Object[] param) throws ApiBootException {
164-
// TODO
165-
}
166-
167158
/**
168159
* unified push resource
169160
*
170161
* @param method method
171162
* @param result method execute result
172163
*/
173164
@Override
174-
public void pushResource(Method method, Object result) {
165+
public void loadResource(Method method, Object result) {
175166
// list
176167
if (result instanceof List) {
177168
pushToList(method, (List<Object>) result);
@@ -262,6 +253,4 @@ else if (resourceField.isList()) {
262253
});
263254

264255
}
265-
266-
267256
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919

2020
import org.minbox.framework.api.boot.plugin.resource.load.ApiBootResourceStoreDelegate;
2121
import org.minbox.framework.api.boot.plugin.resource.load.context.ApiBootResourceContext;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
2224
import org.springframework.util.ObjectUtils;
2325

2426
import java.lang.reflect.Method;
27+
import java.util.Collections;
2528
import java.util.HashMap;
2629
import java.util.List;
2730
import java.util.Map;
@@ -38,6 +41,10 @@
3841
* GitHub:https://github.com/hengboy
3942
*/
4043
public class ApiBootMemoryResourcePusher extends ApiBootJdbcResourcePusher {
44+
/**
45+
* logger instance
46+
*/
47+
static Logger logger = LoggerFactory.getLogger(ApiBootMemoryResourcePusher.class);
4148
/**
4249
* memory resource urls
4350
*/
@@ -59,6 +66,10 @@ public ApiBootMemoryResourcePusher(ApiBootResourceStoreDelegate apiBootResourceS
5966
*/
6067
@Override
6168
public List<String> loadResourceUrl(Method declaredMethod, String sourceFieldValue, String resourceType) {
69+
if (ObjectUtils.isEmpty(sourceFieldValue) || ObjectUtils.isEmpty(resourceType)) {
70+
logger.warn("@ResourceField param [source]|[type]|[name] have empty value, load resource urls with memory fail.");
71+
return Collections.emptyList();
72+
}
6273
// formatter key
6374
String resourceKey = ApiBootResourceContext.formatterCacheKey(declaredMethod, sourceFieldValue, resourceType);
6475
// get resource urls
@@ -81,6 +92,10 @@ public List<String> loadResourceUrl(Method declaredMethod, String sourceFieldVal
8192
*/
8293
@Override
8394
public void deleteResourceUrl(Method declaredMethod, String sourceFieldValue, String resourceType) {
95+
if (ObjectUtils.isEmpty(sourceFieldValue) || ObjectUtils.isEmpty(resourceType)) {
96+
logger.warn("@ResourceField param [source]|[type]|[name] have empty value, delete resource urls with memory fail.");
97+
return;
98+
}
8499
// remove from jdbc
85100
super.deleteResourceUrl(declaredMethod, sourceFieldValue, resourceType);
86101
// formatter key
@@ -99,10 +114,37 @@ public void deleteResourceUrl(Method declaredMethod, String sourceFieldValue, St
99114
*/
100115
@Override
101116
public void insertResourceUrl(Method declaredMethod, String sourceFieldValue, String resourceType, List<String> resourceUrls) {
117+
if (ObjectUtils.isEmpty(sourceFieldValue) || ObjectUtils.isEmpty(resourceType) || ObjectUtils.isEmpty(resourceUrls)) {
118+
logger.warn("@ResourceField param [source]|[type]|[name] have empty value, insert resource urls with memory fail.");
119+
return;
120+
}
102121
super.insertResourceUrl(declaredMethod, sourceFieldValue, resourceType, resourceUrls);
103122
// formatter key
104123
String resourceKey = ApiBootResourceContext.formatterCacheKey(declaredMethod, sourceFieldValue, resourceType);
105124
// remove memory resource urls
106125
RESOURCE_URLS.put(resourceKey, resourceUrls);
107126
}
127+
128+
/**
129+
* update memory data
130+
*
131+
* @param declaredMethod declared method
132+
* @param sourceFieldValue sourceFieldValue
133+
* @param resourceType resourceType
134+
* @param resourceUrls resource urls
135+
*/
136+
@Override
137+
public void updateResourceUrl(Method declaredMethod, String sourceFieldValue, String resourceType, List<String> resourceUrls) {
138+
if (ObjectUtils.isEmpty(sourceFieldValue) || ObjectUtils.isEmpty(resourceType) || ObjectUtils.isEmpty(resourceUrls)) {
139+
logger.warn("@ResourceField param [source]|[type]|[name] have empty value, update resource urls with memory fail.");
140+
return;
141+
}
142+
super.updateResourceUrl(declaredMethod, sourceFieldValue, resourceType, resourceUrls);
143+
144+
// execute delete resource urls
145+
deleteResourceUrl(declaredMethod, sourceFieldValue, resourceType);
146+
147+
// execute insert resource urls
148+
insertResourceUrl(declaredMethod, sourceFieldValue, resourceType, resourceUrls);
149+
}
108150
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919

2020
import org.minbox.framework.api.boot.plugin.resource.load.ApiBootResourceStoreDelegate;
2121
import org.minbox.framework.api.boot.plugin.resource.load.context.ApiBootResourceContext;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
2224
import org.springframework.data.redis.core.RedisTemplate;
25+
import org.springframework.util.ObjectUtils;
2326

2427
import java.lang.reflect.Method;
28+
import java.util.Collections;
2529
import java.util.List;
2630

2731
/**
@@ -36,6 +40,11 @@
3640
* GitHub:https://github.com/hengboy
3741
*/
3842
public class ApiBootRedisResourcePusher extends ApiBootJdbcResourcePusher {
43+
/**
44+
* logger instance
45+
*/
46+
static Logger logger = LoggerFactory.getLogger(ApiBootRedisResourcePusher.class);
47+
3948
/**
4049
* Redis Template
4150
*/
@@ -58,6 +67,10 @@ public ApiBootRedisResourcePusher(ApiBootResourceStoreDelegate apiBootResourceSt
5867
*/
5968
@Override
6069
public List<String> loadResourceUrl(Method declaredMethod, String sourceFieldValue, String resourceType) {
70+
if (ObjectUtils.isEmpty(sourceFieldValue) || ObjectUtils.isEmpty(resourceType)) {
71+
logger.warn("@ResourceField param [source]|[type]|[name] have empty value, load resource urls with redis fail.");
72+
return Collections.emptyList();
73+
}
6174
// formatter redis key
6275
String resourceRedisKey = ApiBootResourceContext.formatterCacheKey(declaredMethod, sourceFieldValue, resourceType);
6376
// get resource size
@@ -81,6 +94,10 @@ public List<String> loadResourceUrl(Method declaredMethod, String sourceFieldVal
8194
*/
8295
@Override
8396
public void deleteResourceUrl(Method declaredMethod, String sourceFieldValue, String resourceType) {
97+
if (ObjectUtils.isEmpty(sourceFieldValue) || ObjectUtils.isEmpty(resourceType)) {
98+
logger.warn("@ResourceField param [source]|[type]|[name] have empty value, delete resource urls with redis fail.");
99+
return;
100+
}
84101
// formatter redis key
85102
String resourceRedisKey = ApiBootResourceContext.formatterCacheKey(declaredMethod, sourceFieldValue, resourceType);
86103

@@ -101,10 +118,37 @@ public void deleteResourceUrl(Method declaredMethod, String sourceFieldValue, St
101118
*/
102119
@Override
103120
public void insertResourceUrl(Method declaredMethod, String sourceFieldValue, String resourceType, List<String> resourceUrls) {
121+
if (ObjectUtils.isEmpty(sourceFieldValue) || ObjectUtils.isEmpty(resourceType) || ObjectUtils.isEmpty(resourceUrls)) {
122+
logger.warn("@ResourceField param [source]|[type]|[name] have empty value, insert resource urls with redis fail.");
123+
return;
124+
}
104125
super.insertResourceUrl(declaredMethod, sourceFieldValue, resourceType, resourceUrls);
105126
// formatter redis key
106127
String resourceRedisKey = ApiBootResourceContext.formatterCacheKey(declaredMethod, sourceFieldValue, resourceType);
107128
// push resource urls to redis
108129
redisTemplate.opsForList().rightPushAll(resourceRedisKey, resourceUrls);
109130
}
131+
132+
/**
133+
* update redis resource data
134+
*
135+
* @param declaredMethod declared method
136+
* @param sourceFieldValue sourceFieldValue
137+
* @param resourceType resourceType
138+
* @param resourceUrls resource urls
139+
*/
140+
@Override
141+
public void updateResourceUrl(Method declaredMethod, String sourceFieldValue, String resourceType, List<String> resourceUrls) {
142+
if (ObjectUtils.isEmpty(sourceFieldValue) || ObjectUtils.isEmpty(resourceType) || ObjectUtils.isEmpty(resourceUrls)) {
143+
logger.warn("@ResourceField param [source]|[type]|[name] have empty value, update resource urls with redis fail.");
144+
return;
145+
}
146+
super.updateResourceUrl(declaredMethod, sourceFieldValue, resourceType, resourceUrls);
147+
148+
// execute delete redis data
149+
deleteResourceUrl(declaredMethod, sourceFieldValue, resourceType);
150+
151+
// execute insert redis data
152+
insertResourceUrl(declaredMethod, sourceFieldValue, resourceType, resourceUrls);
153+
}
110154
}

0 commit comments

Comments
 (0)