초기소스
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package com.wb.domainserver.security.config;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.CacheLoader;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.wb.domainserver.realm.RealmResolver;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.caffeine.CaffeineCacheManager;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class CacheConfig {
|
||||
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
public CacheConfig(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CaffeineCacheManager cacheManager() {
|
||||
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
|
||||
cacheManager.setCaffeine(caffeineCacheBuilder());
|
||||
cacheManager.setCacheLoader(cacheLoader());
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Caffeine<Object, Object> caffeineCacheBuilder() {
|
||||
return Caffeine.newBuilder()
|
||||
.expireAfterWrite(10, TimeUnit.MINUTES)
|
||||
.maximumSize(100);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CacheLoader<Object, Object> cacheLoader() {
|
||||
return new CacheLoader<>() {
|
||||
@Override
|
||||
public Object load(Object key) {
|
||||
return loadAsync(key).join();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Object> asyncReload(Object key, Object oldValue, Executor executor) {
|
||||
return loadAsync(key);
|
||||
}
|
||||
|
||||
private CompletableFuture<Object> loadAsync(Object key) {
|
||||
String hostUrl = (String) key;
|
||||
RealmResolver realmResolver = applicationContext.getBean(RealmResolver.class);
|
||||
return realmResolver.resolveRealmInternalDirect(hostUrl);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user