Skip to content

Commit 041a499

Browse files
committed
ApiBoot Resource Load 资源使用内存缓存
1 parent b185963 commit 041a499

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

api-boot-project/api-boot-autoconfigure/src/main/java/org/minbox/framework/api/boot/autoconfigure/resource/ApiBootResourceLoadAutoConfiguration.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.minbox.framework.api.boot.plugin.resource.load.aop.advistor.ApiBootResourceLoadAdvisor;
2121
import org.minbox.framework.api.boot.plugin.resource.load.aop.interceptor.ApiBootResourceLoadMethodInterceptor;
2222
import org.minbox.framework.api.boot.plugin.resource.load.pusher.ApiBootResourcePusher;
23-
import org.minbox.framework.api.boot.plugin.resource.load.pusher.support.ApiBootJdbcResourcePusher;
23+
import org.minbox.framework.api.boot.plugin.resource.load.pusher.support.ApiBootMemoryResourcePusher;
2424
import org.springframework.beans.factory.ObjectProvider;
2525
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -79,13 +79,14 @@ ApiBootResourceLoadMethodInterceptor resourceLoadMethodInterceptor(ApiBootResour
7979

8080
/**
8181
* ApiBoot Jdbc Resource Pusher
82+
* extends from ApiBootJdbcResourcePusher
8283
*
8384
* @return ApiBootJdbcResourcePusher
8485
*/
8586
@Bean
8687
@ConditionalOnMissingBean
8788
@ConditionalOnMissingClass("org.springframework.data.redis.core.RedisTemplate")
88-
ApiBootJdbcResourcePusher apiBootJdbcResourcePusher() {
89-
return new ApiBootJdbcResourcePusher(resourceStoreDelegate);
89+
ApiBootMemoryResourcePusher apiBootMemoryResourcePusher() {
90+
return new ApiBootMemoryResourcePusher(resourceStoreDelegate);
9091
}
9192
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.plugin.resource.load.ApiBootResourceStoreDelegate;
21+
import org.minbox.framework.api.boot.plugin.resource.load.context.ApiBootResourceContext;
22+
import org.springframework.util.ObjectUtils;
23+
24+
import java.lang.reflect.Method;
25+
import java.util.HashMap;
26+
import java.util.List;
27+
import java.util.Map;
28+
29+
/**
30+
* ApiBoot Resource Memory Pusher
31+
*
32+
* @author:恒宇少年 - 于起宇
33+
* <p>
34+
* DateTime:2019-04-19 11:16
35+
* Blog:http://blog.yuqiyu.com
36+
* WebSite:http://www.jianshu.com/u/092df3f77bca
37+
* Gitee:https://gitee.com/hengboy
38+
* GitHub:https://github.com/hengboy
39+
*/
40+
public class ApiBootMemoryResourcePusher extends ApiBootJdbcResourcePusher {
41+
/**
42+
* memory resource urls
43+
*/
44+
private Map<String, List<String>> RESOURCE_URLS = new HashMap();
45+
46+
public ApiBootMemoryResourcePusher(ApiBootResourceStoreDelegate apiBootResourceStoreDelegate) {
47+
super(apiBootResourceStoreDelegate);
48+
}
49+
50+
/**
51+
* Check whether the data is cached in memory
52+
* If not cached, query from the database
53+
* If cached , query from the memory
54+
*
55+
* @param declaredMethod declared method
56+
* @param sourceFieldValue sourceFieldValue
57+
* @param resourceType resourceType
58+
* @return
59+
*/
60+
@Override
61+
public List<String> loadResourceUrl(Method declaredMethod, String sourceFieldValue, String resourceType) {
62+
// formatter key
63+
String resourceKey = ApiBootResourceContext.formatterCacheKey(declaredMethod, sourceFieldValue, resourceType);
64+
// get resource urls
65+
List<String> resourceUrls = RESOURCE_URLS.get(resourceKey);
66+
if (ObjectUtils.isEmpty(resourceUrls)) {
67+
// get resource urls from jdbc
68+
resourceUrls = super.loadResourceUrl(declaredMethod, sourceFieldValue, resourceType);
69+
// put resource urls
70+
RESOURCE_URLS.put(resourceKey, resourceUrls);
71+
}
72+
return resourceUrls;
73+
}
74+
}

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public ApiBootRedisResourcePusher(ApiBootResourceStoreDelegate apiBootResourceSt
5959
@Override
6060
public List<String> loadResourceUrl(Method declaredMethod, String sourceFieldValue, String resourceType) {
6161
// formatter redis key
62-
String resourceRedisKey = ApiBootResourceContext.formatterRedisCacheKey(declaredMethod, sourceFieldValue, resourceType);
62+
String resourceRedisKey = ApiBootResourceContext.formatterCacheKey(declaredMethod, sourceFieldValue, resourceType);
6363
// get resource size
6464
long resourceSize = redisTemplate.opsForList().size(resourceRedisKey);
6565
if (resourceSize <= 0) {

0 commit comments

Comments
 (0)