map缓存
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; /** * 使用Map缓存(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配送地址转换前的数据:" + 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())) { //如果通过旧区县代码查出的是乡镇级别的数据,则把新代码赋值到乡镇;区县的值置为空 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配送地址转换后的数据:" + newMap); return newMap;*/ } /** * 从缓存中取数据 * @param code * @return */ public String getDeliByCode(String code) { String dto = getDeliDtoByCode(code); if(null == dto) return null; //String newCode = dto.getDeliCityCode(); return dto; } /** * 未超出时间间隔,不需要重新加载,超出需要重新加载 * @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); } //更新缓存 private void updateCache() { loadData(); } /** * 是否经过配置的间隔时间了 */ 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("时间间隔为:" + reloadInterval + "分钟。"); } catch (Exception e) { logger.info("判断是否重新更新配送地址映射缓存异常,使用最小间隔时间", e); reloadInterval = minReloadInterval; } return timePassed >= reloadInterval * timeUnit; } private void loadData() { try { logger.info("deliverAddrMappingCache开始加载!"); /*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加载结束,总共加载" + list.size() + "条记录。"); }*/ 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加载失败:", 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)); } }