fix:拓展可自定义时间缓存接口,app指数接口增加缓存1分钟
This commit is contained in:
@@ -66,6 +66,50 @@ public class RequestCacheUtils {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据时间创建缓存
|
||||||
|
* @param key key
|
||||||
|
* @param duration 时间(毫秒)
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Cache<String, ServerResponse<?>> cacheByMilliseconds(String key,long duration) {
|
||||||
|
if(! map.containsKey(key)) {
|
||||||
|
log.info("创建缓存: {}", key);
|
||||||
|
Cache<String, ServerResponse<?>> cache = CacheBuilder.newBuilder()
|
||||||
|
.maximumSize(1000)
|
||||||
|
.expireAfterWrite(duration,TimeUnit.MILLISECONDS)
|
||||||
|
.weakValues()
|
||||||
|
.recordStats()
|
||||||
|
.build(
|
||||||
|
// new CacheLoader<String, Object>() {
|
||||||
|
// @Override
|
||||||
|
// public Object load(String key) throws Exception {
|
||||||
|
// return func.apply(key);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
);
|
||||||
|
map.put(key, cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
return map.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
public static <T> ServerResponse<T> cache(String module, String key,long duration, Function<String, ServerResponse<?>> func) {
|
||||||
|
Cache<String, ServerResponse<?>> cache = cacheByMilliseconds(module,duration);
|
||||||
|
AtomicBoolean bool = new AtomicBoolean(true);
|
||||||
|
ServerResponse<T> response = (ServerResponse<T>) cache.get(key, () -> {
|
||||||
|
bool.set(false);
|
||||||
|
return func.apply(key);
|
||||||
|
});
|
||||||
|
|
||||||
|
if(bool.get()) {
|
||||||
|
log.info("命中缓存 module: {}, key: {}, 时间戳: {}", module, key, System.currentTimeMillis());
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws InterruptedException {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
ServerResponse<Object> response = null;
|
ServerResponse<Object> response = null;
|
||||||
Function<String, ServerResponse<?>> func = (string) -> {
|
Function<String, ServerResponse<?>> func = (string) -> {
|
||||||
|
|||||||
@@ -238,7 +238,7 @@ public class StockApiController {
|
|||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ServerResponse getIndiaIndexByToday() {
|
public ServerResponse getIndiaIndexByToday() {
|
||||||
String INDEX_CODE = "TODAY_INDEX";
|
String INDEX_CODE = "TODAY_INDEX";
|
||||||
return RequestCacheUtils.cache("getIndiaIndexByToday.do", INDEX_CODE, (string) -> {
|
return RequestCacheUtils.cache("getIndiaIndexByToday.do", INDEX_CODE,60000, (string) -> {
|
||||||
return this.stockService.getIndexByBtoday();
|
return this.stockService.getIndexByBtoday();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user