|
| 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 | +} |
0 commit comments