|
|
@ -3,9 +3,11 @@ package com.hfkj.service.impl; |
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.alicp.jetcache.Cache; |
|
|
|
import com.alicp.jetcache.Cache; |
|
|
|
|
|
|
|
import com.alicp.jetcache.RefreshPolicy; |
|
|
|
import com.alicp.jetcache.anno.CacheType; |
|
|
|
import com.alicp.jetcache.anno.CacheType; |
|
|
|
import com.alicp.jetcache.anno.Cached; |
|
|
|
import com.alicp.jetcache.anno.Cached; |
|
|
|
import com.alicp.jetcache.anno.CreateCache; |
|
|
|
import com.alicp.jetcache.anno.CreateCache; |
|
|
|
|
|
|
|
import com.alicp.jetcache.embedded.LinkedHashMapCacheBuilder; |
|
|
|
import com.hfkj.common.exception.ErrorCode; |
|
|
|
import com.hfkj.common.exception.ErrorCode; |
|
|
|
import com.hfkj.common.exception.ErrorHelp; |
|
|
|
import com.hfkj.common.exception.ErrorHelp; |
|
|
|
import com.hfkj.common.exception.SysCode; |
|
|
|
import com.hfkj.common.exception.SysCode; |
|
|
@ -20,6 +22,7 @@ import com.hfkj.service.CommonService; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
import javax.annotation.Resource; |
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.util.*; |
|
|
|
import java.util.*; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
@ -48,19 +51,18 @@ public class CommonServiceImpl implements CommonService { |
|
|
|
@Resource |
|
|
|
@Resource |
|
|
|
private SecDictionaryMapper dicMapper; |
|
|
|
private SecDictionaryMapper dicMapper; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Map<String, Map<String, SecDictionary>> dicCache = new HashMap<String,Map<String, SecDictionary>>(); |
|
|
|
private Map<String, Map<String, SecDictionary>> dicCache = new HashMap<String,Map<String, SecDictionary>>(); |
|
|
|
|
|
|
|
|
|
|
|
private List<SecRegion> citiesCache = new ArrayList<>(); |
|
|
|
@CreateCache(name = "cities:", cacheType = CacheType.REMOTE) |
|
|
|
|
|
|
|
private Cache<Long, SecRegion> citiesCache; |
|
|
|
private Map<Long,List<SecRegion>> regionsCache = new HashMap<>(); |
|
|
|
@CreateCache(name = "region:", cacheType = CacheType.REMOTE) |
|
|
|
|
|
|
|
private Cache<Long,List<SecRegion>> regionsCache; |
|
|
|
private Map<Long,List<SecRegion>> streetCache = new HashMap<>(); |
|
|
|
@CreateCache(name = "regionStreet:", cacheType = CacheType.REMOTE) |
|
|
|
|
|
|
|
private Cache<Long,List<SecRegion>> streetCache; |
|
|
|
private Map<Long,List<SecRegion>> communityCache = new HashMap<>(); |
|
|
|
@CreateCache(name = "regionCommunity:", cacheType = CacheType.REMOTE) |
|
|
|
|
|
|
|
private Cache<Long,List<SecRegion>> communityCache; |
|
|
|
private Map<Long, SecRegion> singleRegionCache = new HashMap<>(); |
|
|
|
@CreateCache(name = "regionSingle:", cacheType = CacheType.REMOTE) |
|
|
|
|
|
|
|
private Cache<Long, SecRegion> singleRegionCache; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public Map<String, List<SecDictionary>> getDictionaries() { |
|
|
|
public Map<String, List<SecDictionary>> getDictionaries() { |
|
|
@ -276,17 +278,20 @@ public class CommonServiceImpl implements CommonService { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public List<SecRegion> getCities() { |
|
|
|
public List<SecRegion> getCities() { |
|
|
|
refreshRegion(); |
|
|
|
refreshRegion(); |
|
|
|
return citiesCache; |
|
|
|
SecRegionExample example = new SecRegionExample(); |
|
|
|
|
|
|
|
example.createCriteria().andStatusEqualTo(1).andParentIdIsNull(); |
|
|
|
|
|
|
|
return regionMapper.selectByExample(example); |
|
|
|
} |
|
|
|
} |
|
|
|
private void refreshRegion(){ |
|
|
|
|
|
|
|
if (System.currentTimeMillis() - regionLastReadTime < READ_STEP) { |
|
|
|
private void refreshRegion() { |
|
|
|
return; |
|
|
|
if (regionsCache.get(500000L) != null) { |
|
|
|
} |
|
|
|
return; |
|
|
|
if (regionLock.isLocked()) { //说明有线程已经在刷数据了
|
|
|
|
} |
|
|
|
if (null != dicCache && dicCache.size() > 0) { //如果有数据说明已经初始化过了,直接返回老数据
|
|
|
|
if (regionLock.isLocked()) { // 说明有线程已经在刷数据了
|
|
|
|
|
|
|
|
if (null != regionsCache) { //如果有数据说明已经初始化过了,直接返回老数据
|
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
while (null == dicCache || dicCache.size()<1) { //说明初始化刷数据 等待10毫秒
|
|
|
|
while (null == regionsCache) { //说明初始化刷数据 等待10毫秒
|
|
|
|
try { |
|
|
|
try { |
|
|
|
Thread.sleep(10); |
|
|
|
Thread.sleep(10); |
|
|
|
//此处是否需要添加一个超时机制?
|
|
|
|
//此处是否需要添加一个超时机制?
|
|
|
@ -311,8 +316,9 @@ public class CommonServiceImpl implements CommonService { |
|
|
|
private void getRegionFromDB(){ |
|
|
|
private void getRegionFromDB(){ |
|
|
|
SecRegionExample example = new SecRegionExample(); |
|
|
|
SecRegionExample example = new SecRegionExample(); |
|
|
|
example.createCriteria().andStatusEqualTo(1).andParentIdIsNull(); |
|
|
|
example.createCriteria().andStatusEqualTo(1).andParentIdIsNull(); |
|
|
|
citiesCache = regionMapper.selectByExample(example); |
|
|
|
List<SecRegion> citiesList = regionMapper.selectByExample(example); |
|
|
|
for(SecRegion city : citiesCache){ // 省
|
|
|
|
for(SecRegion city : citiesList){ // 省
|
|
|
|
|
|
|
|
citiesCache.put(city.getRegionId(), city); |
|
|
|
singleRegionCache.put(city.getRegionId(), city); |
|
|
|
singleRegionCache.put(city.getRegionId(), city); |
|
|
|
List<SecRegion> regions = getRegions(city.getRegionId()); |
|
|
|
List<SecRegion> regions = getRegions(city.getRegionId()); |
|
|
|
regionsCache.put(city.getRegionId(),regions); |
|
|
|
regionsCache.put(city.getRegionId(),regions); |
|
|
|