眾所周知,在使用字典表的時候,如果每次需要轉換的時候都去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; } } }