Commit 024b3d6a by 陶光胜

门店控件增加接口

parent 960a7ac6
......@@ -14,6 +14,9 @@ import com.gic.store.entity.TabProvince;
import com.gic.store.service.ProvincesApiService;
import com.gic.store.service.ProvincesService;
import com.gic.store.service.StoreService;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
......@@ -21,6 +24,7 @@ import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
......@@ -31,30 +35,55 @@ import java.util.stream.Collectors;
@Service("provincesApiService")
public class ProvincesApiServiceImpl implements ProvincesApiService {
private static final Logger log = LogManager.getLogger(ProvincesApiServiceImpl.class);
private LoadingCache<String, List<ProvinceDTO>> provinceCache = null;
private LoadingCache<String, List<CityDTO>> cityCache = null;
private LoadingCache<String, List<CountyDTO>> countyCache = null;
private static final int EXPIRE_SECONDS = 7;
@Autowired
private ProvincesService provincesService;
@Autowired
private StoreService storeService;
@PostConstruct
public void init(){
provinceCache = CacheBuilder.newBuilder().maximumSize(10).expireAfterAccess(EXPIRE_SECONDS, TimeUnit.DAYS)
.build(new CacheLoader<String, List<ProvinceDTO>>() {
@Override
public List<ProvinceDTO> load(String key) {
return EntityUtil.changeEntityListByJSON(ProvinceDTO.class, provincesService.selectAllProvince());
}
});
cityCache = CacheBuilder.newBuilder().maximumSize(10).expireAfterAccess(EXPIRE_SECONDS, TimeUnit.DAYS)
.build(new CacheLoader<String, List<CityDTO>>() {
@Override
public List<CityDTO> load(String key) {
return EntityUtil.changeEntityListByJSON(CityDTO.class, provincesService.selectAllCity());
}
});
countyCache = CacheBuilder.newBuilder().maximumSize(10).expireAfterAccess(EXPIRE_SECONDS, TimeUnit.DAYS)
.build(new CacheLoader<String, List<CountyDTO>>() {
@Override
public List<CountyDTO> load(String key) {
return EntityUtil.changeEntityListByJSON(CountyDTO.class, provincesService.selectAllCounty());
}
});
}
@Override
public ServiceResponse<List<ProvinceDTO>> selectAllProvince() {
String key = "enterprise:province";
List<ProvinceDTO> list = (List<ProvinceDTO>)RedisUtil.getCache(key);
if(CollectionUtils.isEmpty(list)){
list = EntityUtil.changeEntityListByJSON(ProvinceDTO.class, this.provincesService.selectAllProvince());
RedisUtil.setCache(key, list, 6*30l, TimeUnit.DAYS);
}
List<ProvinceDTO> list = provinceCache.getUnchecked(key);
return ServiceResponse.success(list);
}
@Override
public ServiceResponse<List<CityDTO>> selectAllCity() {
String key = "enterprise:city";
List<CityDTO> list = (List<CityDTO>)RedisUtil.getCache(key);
if(CollectionUtils.isEmpty(list)){
list = EntityUtil.changeEntityListByJSON(CityDTO.class, this.provincesService.selectAllCity());
RedisUtil.setCache(key, list, 6*30l, TimeUnit.DAYS);
}
List<CityDTO> list = cityCache.getUnchecked(key);
log.info("allcity:{}", list.size());
return ServiceResponse.success(list);
}
......@@ -95,11 +124,7 @@ public class ProvincesApiServiceImpl implements ProvincesApiService {
@Override
public ServiceResponse<List<CountyDTO>> selectAllCounty() {
String key = "enterprise:county";
List<CountyDTO> list = (List<CountyDTO>)RedisUtil.getCache(key);
if(CollectionUtils.isEmpty(list)){
list = EntityUtil.changeEntityListByJSON(CountyDTO.class, this.provincesService.selectAllCounty());
RedisUtil.setCache(key, list, 6*30l, TimeUnit.DAYS);
}
List<CountyDTO> list = countyCache.getUnchecked(key);
return ServiceResponse.success(list);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment