import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import weather.model.WeatherInfo;
import weather.service.impl.GetWeatherServiceImpl;
import weather.util.LogService;
import com.huisa.common.cache.jvmcache.ConcurrentMapImpl;
public class WeatherInfoCache {
/**
* 实时天气信息缓存;key:cityid
*/
private static Map<String, List<WeatherInfo>> cache_weatherInfo = new ConcurrentMapImpl<String, List<WeatherInfo>>();
private static GetWeatherServiceImpl service = new GetWeatherServiceImpl();
// load缓存
public static void loadAll() {
try {
List<WeatherInfo> weatherInfoList = service.weatherInfofindAll();
// 清空缓存
if (weatherInfoList.size() > 0) {
cache_weatherInfo.clear();
}
// 处理缓存数据
if (weatherInfoList != null && weatherInfoList.size() > 0) {
for (WeatherInfo weatherInfo : weatherInfoList) {
if (weatherInfo.getCityid() != null) {
List<WeatherInfo> weatherInfoList_temp = cache_weatherInfo.get(weatherInfo.getCityid());
if (weatherInfoList_temp == null) {
weatherInfoList_temp = new ArrayList<WeatherInfo>();
}
weatherInfoList_temp.add(weatherInfo);
cache_weatherInfo.put(weatherInfo.getCityid(),weatherInfoList_temp);
}
}
}
} catch (Exception e) {
LogService.error("WeatherInfoCache.loadAll faild!", e);
}
}
/** 从缓存weatherInfoList 缓存池 中取,没有 就从数据库中取 */
public static List<WeatherInfo> getWeatherInfoList(String cityid) {
if (cityid != null) {
}
List<WeatherInfo> WeatherInfoList = cache_weatherInfo.get(cityid);
if (WeatherInfoList == null) {
try {
WeatherInfoList = service.findWeatherInfoByID(cityid);
} catch (Exception e) {
LogService.error("WeatherInfoCache.getWeatherInfoList faild!",e);
}
if (WeatherInfoList != null) {
cache_weatherInfo.put(cityid, WeatherInfoList);
}
}
return WeatherInfoList;
}
}
数据加载到缓存
最新推荐文章于 2021-05-28 11:38:07 发布