package cacheMoBan; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.InitializingBean; import com.paic.pafa.app.lwc.core.util.StringUtils; /** * Use Map cache (ConcurrentHashMap) * @author YANGYONGFANG562 * */ public class CacheMoBan implements BeanFactoryAware,InitializingBean { private static Map<String, Object> cache = new ConcurrentHashMap<String, Object>(); private Log logger = LogFactory.getLog(CacheMoBan.class); //private DeliveryMappingAddressDAO dao; private long reloadInterval; private long lastUpdateTime; private long minReloadInterval = 10; private long timeUnit = 60 * 1000L; private BeanFactory context; private static CacheMoBan instance; private CacheMoBan(){} @Override public void afterPropertiesSet() throws Exception { instance = this; loadData(); } @Override public void setBeanFactory(BeanFactory paramBeanFactory) throws BeansException { this.context = paramBeanFactory; } public static CacheMoBan getInstance(){ return instance ; } public Map<String, String> getMappingCode(Map<String, Object> oldMap) { if(null == oldMap) return null; Map<String, String> newMap = new HashMap<String, String>(); String newSafc = getDeliByCode((String) oldMap.get("a")); //key1 newMap.put("safc", newSafc); return newMap; /*logger.info("DeliveryMappingAddressUitl.getMappingCode Delivery address data before conversion: " + oldMap); Map<String, String> newMap = new HashMap<String, String>(); String newProvinceId = getDeliByCode((String) oldMap.get("provinceId")); newMap.put("provinceId", newProvinceId); String newCityId = getDeliByCode((String) oldMap.get("cityId")); newMap.put("cityId", newCityId); DeliveryMappingAddressInfoDTO areaDto = getDeliDtoByCode((String) oldMap.get("areaId")); String newTownId = null; if(null != areaDto) { if(GlobalConstants.FOUR_LEVEL.equals(areaDto.getCityLevel())) { //If the data at the township level is found through the old district/county code, assign the new code to the township; the district/county value is set to null newTownId = areaDto.getDeliCityCode(); newMap.put("areaId", null); newMap.put("townId", newTownId); } else { newMap.put("areaId", areaDto.getDeliCityCode()); newMap.put("townId", (String) oldMap.get("townId")); } } else { newMap.put("areaId", null); newMap.put("townId", (String) oldMap.get("townId")); } logger.info("DeliveryMappingAddressUitl.getMappingCode Delivery address converted data: " + newMap); return newMap;*/ } /** * Get data from cache * @param code * @return */ public String getDeliByCode(String code) { String dto = getDeliDtoByCode(code); if(null == dto) return null; //String newCode = dto.getDeliCityCode(); return dto; } /** * If the time interval is not exceeded, no reload is required, and if it exceeds the time interval, reload is required * @param code * @return */ public String getDeliDtoByCode(String code) { if(!StringUtils.hasText(code)) return null; if(updateIntervalPassed()) { synchronized (CacheMoBan.class) { if(updateIntervalPassed()) { updateCache(); } } } return (String) cache.get(code); } //refresh cache private void updateCache() { loadData(); } /** * Whether the configured interval has elapsed */ private boolean updateIntervalPassed() { long timePassed = System.currentTimeMillis() - lastUpdateTime; try { //reloadInterval = Long.valueOf(Configuration.getValue("deliveryAddress.mapping.reloadCache.interval", String.valueOf(minReloadInterval))); reloadInterval = 60*12L; if (reloadInterval < minReloadInterval) { reloadInterval = minReloadInterval; } logger.debug("The time interval is: " + reloadInterval + "minutes."); } catch (Exception e) { logger.info("Judging whether to re-update the delivery address mapping cache is abnormal, use the minimum interval", e); reloadInterval = minReloadInterval; } return timePassed >= reloadInterval * timeUnit; } private void loadData() { try { logger.info("deliverAddrMappingCache starts loading!"); /*dao = (DeliveryMappingAddressDAO) context .getBean("deliveryMappingAddressDAO"); List<DeliveryMappingAddressInfoDTO> list = dao.queryAll(); if(null != list && !list.isEmpty()) { for (int i = 0; i < list.size(); i++) { DeliveryMappingAddressInfoDTO dto = list.get(i); if(null != dto && StringUtils.hasText(dto.getCityCode()) && StringUtils.hasText(dto.getDeliCityCode())) { cache.put(dto.getCityCode(), dto); } } logger.info("deliverAddrMappingCache is loaded, a total of " + list.size() + " records are loaded."); }*/ cache.put("key1", "accident"); cache.put("key2", "property"); cache.put("key3", "force"); cache.put("key4", "biz"); lastUpdateTime = System.currentTimeMillis(); } catch (Exception e) { logger.info("deliverAddrMappingCache failed to load:", e); } } public static void main(String[] args) { Map<String, Object> map = new HashMap<String, Object>(); CacheMoBan cache = new CacheMoBan(); map.put("a", "key1"); map.put("b", "key2"); map.put("c", "key3"); System.out.println(cache.getMappingCode(map)); } }