Caching 缓存消息源
对于消息源实现的每次调用,我希望在调用远程服务器之前进行缓存 这是伪代码Caching 缓存消息源,caching,spring-boot,Caching,Spring Boot,对于消息源实现的每次调用,我希望在调用远程服务器之前进行缓存 这是伪代码 @Configuration public class ConfigurationForMessageSource { @Bean public MessageSource messageSource() { return new AbstractMessageSource() { @Autowired RemoteMessageSour
@Configuration
public class ConfigurationForMessageSource {
@Bean
public MessageSource messageSource() {
return new AbstractMessageSource() {
@Autowired
RemoteMessageSource remoteMessageSource;
@Override
protected MessageFormat resolveCode(String code, Locale locale) {
String message = remoteMessageSource.translate(code);
return createMessageFormat(message, locale);
}
};
}
}
服务实现
@Service
public class RemoteMessageSource {
@Cacheable(value = "message", key = "#code")
public String translate(String code) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return dataBase.get(code);
}
}
github提供了完整的实现: