mirror of
https://gitee.com/nutz/nutzboot.git
synced 2024-12-02 11:48:09 +08:00
Merge branch 'dev' of github.com:nutzam/nutzboot into dev
This commit is contained in:
commit
98e7669780
@ -0,0 +1,17 @@
|
||||
package io.nutz.demo.simple;
|
||||
|
||||
import org.nutz.boot.starter.caffeine.UpdateStrategy;
|
||||
import org.nutz.ioc.loader.annotation.IocBean;
|
||||
import org.nutz.lang.Lang;
|
||||
import org.nutz.mvc.Mvcs;
|
||||
|
||||
@IocBean
|
||||
public class MvcUpdateStrategy implements UpdateStrategy {
|
||||
|
||||
@Override
|
||||
public boolean shouldUpdate(String key) {
|
||||
// 凡是request中携带update=true,都强制更新缓存
|
||||
return Lang.parseBoolean(Mvcs.getReq().getParameter("update"));
|
||||
}
|
||||
|
||||
}
|
@ -34,6 +34,8 @@ public class CaffeineInterceptor implements MethodInterceptor {
|
||||
|
||||
protected KeyStringifier stringifier;
|
||||
|
||||
protected UpdateStrategy updateStrategy;
|
||||
|
||||
@Inject("refer:$ioc")
|
||||
protected Ioc ioc;
|
||||
|
||||
@ -76,7 +78,7 @@ public class CaffeineInterceptor implements MethodInterceptor {
|
||||
Cache<String, Object> cache = getCache(strategy);
|
||||
String key = getKey(method, chain.getArgs());
|
||||
Object value = cache.getIfPresent(key);
|
||||
if (value == null) {
|
||||
if (value == null || updateStrategy.shouldUpdate(key)) {
|
||||
chain.doChain();
|
||||
cache.put(key, chain.getReturn());
|
||||
} else
|
||||
@ -87,18 +89,17 @@ public class CaffeineInterceptor implements MethodInterceptor {
|
||||
String fullName = String.format("%s.%s", method.getDeclaringClass().getName(), method.getName());
|
||||
if (args == null || args.length == 0)
|
||||
return fullName;
|
||||
return fullName + Arrays.stream(args).map(stringifier::stringify).collect(Collectors.joining("$"));
|
||||
return fullName + ":" + Arrays.stream(args).map(stringifier::stringify).collect(Collectors.joining("$"));
|
||||
}
|
||||
|
||||
public void init() {
|
||||
Map<String, CacheStrategy> map = new HashMap<>();
|
||||
String[] types = ioc.getNamesByType(KeyStringifier.class);
|
||||
if (Lang.isEmptyArray(types)) {
|
||||
this.stringifier = String::valueOf;
|
||||
} else {
|
||||
this.stringifier = ioc.get(KeyStringifier.class, types[0]);
|
||||
}
|
||||
String[] stringifierNames = ioc.getNamesByType(KeyStringifier.class);
|
||||
this.stringifier = Lang.isEmptyArray(stringifierNames) ? String::valueOf : ioc.get(KeyStringifier.class, stringifierNames[0]);
|
||||
log.debugf("use %s as KeyStringifier", this.stringifier);
|
||||
String[] updateStrategyNames = ioc.getNamesByType(UpdateStrategy.class);
|
||||
this.updateStrategy = Lang.isEmptyArray(updateStrategyNames) ? k -> false : ioc.get(UpdateStrategy.class, updateStrategyNames[0]);
|
||||
log.debugf("use %s as UpdateStrategy", this.updateStrategy);
|
||||
Map<String, CacheStrategy> map = new HashMap<>();
|
||||
conf.entrySet().stream().filter(entry -> entry.getKey().startsWith(CaffeineStarter.PRE)).forEach(entry -> {
|
||||
if (entry.getValue() == null)
|
||||
return;
|
||||
|
@ -0,0 +1,11 @@
|
||||
package org.nutz.boot.starter.caffeine;
|
||||
|
||||
public interface UpdateStrategy {
|
||||
|
||||
/**
|
||||
* 是否需要强制更新缓存
|
||||
* @param key 缓存键
|
||||
* @return should update ?
|
||||
*/
|
||||
boolean shouldUpdate(String key);
|
||||
}
|
Loading…
Reference in New Issue
Block a user