SpringBoot 程序启动时将数据库的字典表加载进内存中

众所周知,在使用字典表的时候,如果每次需要转换的时候都去MySQL数据库里面查询,是非常浪费性能的操作,所以可以把字典表的数据放到内存里面去。

实现的逻辑很简单,首先只需要在项目启动的时候去查询字典表,然后将其放入用静态变量(在项目启动的时候就会初始化)中,需要用的时候就可以直接去内存中取出来即可。

package com.chitic.supplywater.common.config;

import com.chitic.module.core.util.CopyUtil;
import com.chitic.module.core.util.SpringUtils;
import com.chitic.supplywater.common.api.response.DictionariesResponse;
import com.chitic.supplywater.common.repository.dao.DictionariesRepository;
import com.chitic.supplywater.common.repository.entity.Dictionaries;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description //TODO
 * @Author GaoX
 * @Date 2020/9/17 13:39
 */
@Slf4j
public  class DictCache {

    private static DictionariesRepository dictionariesRepository;

    public static Map<String, List<DictionariesResponse>> cache = new HashMap<>();

    static {
        //注意此处不能使用依赖注入,因为此时dictionariesRepository还没创建出来,可以获取上下文手动创建
        dictionariesRepository = SpringUtils.getBean(DictionariesRepository.class);
        toData();
    }

    public static void toData(){
        List<Dictionaries> all = dictionariesRepository.findAll();
        List<DictionariesResponse> responseList = CopyUtil.copyList(all, DictionariesResponse.class);
        cache = responseList.stream().collect(Collectors.groupingBy(DictionariesResponse::getFindtype));
        if(log.isInfoEnabled()){
            log.info("dict字典表缓存:[{}]", cache);
        }
    }

    /** 返回list数据格式 */
    public static List<DictionariesResponse> toDataList(String findType){
        List<DictionariesResponse> dictionariesList = cache.get(findType);
        if(CollectionUtils.isEmpty(dictionariesList)){
            return new ArrayList<>();
        }
        return cache.get(findType);
    }
    /** 返回map数据格式  */
    public static Map<String, String> toDataMap(String findType){
        List<DictionariesResponse> dictionariesList = toDataList(findType);
        return dictionariesList.stream().collect(Collectors.toMap(DictionariesResponse::getIdentification, DictionariesResponse::getContent, (a, b) -> b));
    }

}

使用时候直接取就可以了: 

DictCache.toDataList("")

如果字典表数据更新了,可以通过接口将此变量清除DictCache.cache.clear(); 然后重新加载即可DictCache.toData();    当然如果手动在数据库添加(不过一般项目上线后,是不让手动操作数据库的),那就只能重启程序了!

 

获取上下文工具类

package com.chitic.module.core.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

@SuppressWarnings("unchecked")
@Component
public class SpringUtils implements BeanFactoryPostProcessor {

    private static ConfigurableListableBeanFactory beanFactory; // Spring应用上下文环境

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        SpringUtils.beanFactory = beanFactory;
    }

    /**
     * 获取对象
     *
     * @param name
     * @return Object 一个以所给名字注册的bean的实例
     * @throws BeansException
     *
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        //首字母默认小写
        name=lowerCaseInit(name);
        if (containsBean(name)) {
            return (T) beanFactory.getBean(name);
        }else{
            return null;
        }
    }

    /**
     * 获取类型为requiredType的对象
     *
     * @param clz
     * @return
     * @throws BeansException
     *
     */
    public static <T> T getBean(Class<T> clz) throws BeansException {
        @SuppressWarnings("unchecked")
        T result = (T) beanFactory.getBean(clz);
        return result;
    }

    /**
     * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
     *
     * @param name
     * @return boolean
     */
    public static boolean containsBean(String name) {
        return beanFactory.containsBean(name);
    }

    /**
     * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
     * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
     *
     * @param name
     * @return boolean
     * @throws NoSuchBeanDefinitionException
     *
     */
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.isSingleton(name);
    }

    /**
     * @param name
     * @return Class 注册对象的类型
     * @throws NoSuchBeanDefinitionException
     *
     */
    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.getType(name);
    }

    /**
     * 如果给定的bean名字在bean定义中有别名,则返回这些别名
     *
     * @param name
     * @return
     * @throws NoSuchBeanDefinitionException
     *
     */
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.getAliases(name);
    }

    /**
     *首字母小写
     * @return:  小写的首字母
     */

    private static String lowerCaseInit(String str) {
        if (str.length()>0) {
            char c = str.charAt(0);
            if (c >= 65 && c <= 90) {
                int i = c + 32;
                return ((char)i)+str.substring(1);
            }else{
                return str;
            }
        }else{
            return  null;
        }
    }
}

 

posted @ 2020-09-17 16:44  高木子  阅读(4557)  评论(0编辑  收藏  举报