Springboot 单例-将数据库的数据缓存在内存中
准备配置
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import javax.servlet.ServletContext;
import java.util.Locale;
@Component
public class SpringContextHolder implements BeanFactoryPostProcessor, ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringContextHolder.class);
private static ApplicationContext context;
private static ServletContext servletContext;
private static Environment environment;
private static MessageSourceAccessor messages;
private SpringContextHolder() {
}
public static ApplicationContext getSpringContext() {
return context;
}
public static ApplicationContext get() {
return context;
}
public static ServletContext getServletContext() {
return servletContext;
}
public static Environment getEnvironment() {
return environment;
}
public static Object getBean(String name) {
return context.getBean(name);
}
public static <T> T getBean(Class<T> requiredType) {
return context.getBean(requiredType);
}
public static <T> T getBean(String name, Class<T> requiredType) {
return context.getBean(name, requiredType);
}
public static String getProperty(String key) {
return environment.getProperty(key);
}
public static String getRequiredProperty(String key) {
return environment.getRequiredProperty(key);
}
public static void setServletContext(ServletContext sc) {
servletContext = sc;
}
public static MessageSourceAccessor getMessageSourceAccessor() {
return messages;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
init(applicationContext);
}
private static void init(ApplicationContext applicationContext) {
context = applicationContext;
environment = context.getEnvironment();
if (context instanceof WebApplicationContext) {
servletContext = ((WebApplicationContext) context).getServletContext();
}
messages = new MessageSourceAccessor(context, Locale.SIMPLIFIED_CHINESE);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
LOGGER.info("Spring context holder initialized successful");
}
}
单例实现
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.hico.projects.yige.moapi.visitor.resp.Respondent;
import com.hico.projects.yige.mocallback.web.SpringContextHolder;
import com.hico.projects.yige.service.guest.GuestService;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class RespondentCacheUtil {
private static RespondentCacheUtil INSTANCE;
private List<Respondent> respondentList = new ArrayList<>();
private Map<Long, Respondent> respondentMapByPersonId = new HashMap<>();
private Map<String, Respondent> respondentMapByMobile = new HashMap<>();
private long synchTime = 0L;
private static final long EXPIRES_TIME = 1800000L;
public static RespondentCacheUtil getInstance() {
if (ObjectUtil.isNull(INSTANCE)) {
INSTANCE = new RespondentCacheUtil();
INSTANCE.synchTime = Instant.now().toEpochMilli();
}
return INSTANCE;
}
private RespondentCacheUtil() {
init();
}
public void init() {
synchRespondentList();
}
public void synchRespondentList() {
GuestService guestService = SpringContextHolder.getBean(GuestService.class);
this.respondentList = guestService.getRespondentList();
this.respondentMapByPersonId =
this.respondentList.stream()
.collect(Collectors.toMap(Respondent::getPersonId, Function.identity()));
this.respondentMapByMobile =
this.respondentList.stream()
.filter(member -> StrUtil.isNotBlank(member.getMobile()))
.collect(Collectors.toMap(Respondent::getMobile, Function.identity()));
this.synchTime = Instant.now().toEpochMilli();
}
public void updateRespondentList() {
long nowTime = Instant.now().toEpochMilli();
if ((nowTime - this.synchTime) > EXPIRES_TIME) {
this.synchRespondentList();
}
}
public Respondent getRespondentInfoByMobile(String mobile) {
this.updateRespondentList();
Respondent respondent = this.respondentMapByMobile.get(mobile);
if (ObjectUtil.isNull(respondent)) {
this.synchRespondentList();
respondent = this.respondentMapByMobile.get(mobile);
}
return respondent;
}
public Respondent getRespondentInfoByMoId(Long moId) {
this.updateRespondentList();
Respondent respondent = this.respondentMapByPersonId.get(moId);
if (ObjectUtil.isNull(respondent)) {
this.synchRespondentList();
respondent = this.respondentMapByPersonId.get(moId);
}
return respondent;
}
public List<Respondent> getRespondentList() {
this.updateRespondentList();
if (CollectionUtil.isEmpty(this.respondentList)) {
this.synchRespondentList();
}
return this.respondentList;
}
}