优化基础数据放入redis缓存。

This commit is contained in:
zhouxiunai
2019-02-22 13:46:00 +08:00
parent 410e2ee745
commit aaba5924bd
@@ -1,11 +1,13 @@
package com.gzzn.omms.msgexchangeapi.basicdata.manager;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.gzzn.omms.msgexchangeapi.basicdata.entity.OrmsStand;
import com.gzzn.omms.msgexchangeapi.basicdata.service.IOrmsStandService;
import com.gzzn.omms.msgexchangeapi.redis.RedisService;
import com.gzzn.omms.msgexchangeapi.utils.SpringUtil;
/**
@@ -14,8 +16,7 @@ import com.gzzn.omms.msgexchangeapi.utils.SpringUtil;
*
*/
public class OrmsStandManager {
private static Hashtable dataCache = null;
private static final String KEY_ORMSSTAND = "orms_stand";
private static OrmsStandManager instance = new OrmsStandManager();
private OrmsStandManager()
@@ -32,23 +33,28 @@ public class OrmsStandManager {
{
OrmsStand ormsStand = null;
synchronized (OrmsStandManager.class) {
if(null == dataCache)
//缓存获取
RedisService redisService = (RedisService) SpringUtil
.getBean("redisService");
Boolean keyExists = redisService.hasKey(KEY_ORMSSTAND);
if(!keyExists)
{
//远程获取
IOrmsStandService ormsStandService = (IOrmsStandService) SpringUtil
.getBean("ormsStandService");
List<OrmsStand> lsOrmsStand = ormsStandService.findAll();
dataCache = new Hashtable<>();
Map mapOrmsStand = new HashMap<String, OrmsStand>();
for(OrmsStand node: lsOrmsStand)
{
dataCache.put(node.getStandCode(), node);
mapOrmsStand.put(node.getStandCode(), node);
}
redisService.hmset(KEY_ORMSSTAND, mapOrmsStand, 3600);
}
//本地获取
ormsStand = (OrmsStand) dataCache.get(standCode);
ormsStand = (OrmsStand) redisService.hget(KEY_ORMSSTAND, standCode);
}
return ormsStand;