数据加载到缓存

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;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值