package com.vci.server.framework.cache;
|
|
import java.util.List;
|
|
import org.apache.commons.lang3.StringUtils;
|
import org.hibernate.HibernateException;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.vci.common.log.ServerWithLog4j;
|
import com.vci.corba.framework.data.PvolumeInfo;
|
import com.vci.server.base.persistence.dao.HibernateCallback;
|
import com.vci.server.base.persistence.dao.HibernateTemplate;
|
import com.vci.server.cache.CacheNames;
|
import com.vci.server.cache.VolumeCacheProvider;
|
import com.vci.server.cache.redis.RedisUtil;
|
import com.vci.server.framework.volume.dao.Pvolume;
|
import com.vci.server.framework.volume.dao.PvolumeDaoImp;
|
|
|
public class VolumeCatch {
|
private static VolumeCatch _instance = null;
|
//
|
// private static String CACHENAME = "VOLUME";
|
//
|
private static VolumeCatch getInstance(){
|
if (_instance == null){
|
_instance = new VolumeCatch();
|
}
|
|
return _instance;
|
}
|
|
//
|
// private VolumeCatch(){
|
// super(CACHENAME);
|
// InitCatch();
|
// }
|
|
|
public synchronized static void InitCatch()
|
{
|
clearCatch();
|
|
getInstance().InitVolumes();
|
}
|
|
|
public static void clearCatch() {
|
RedisUtil.getInstance().del(CacheNames.VOLUMES);
|
}
|
|
// public synchronized static void refreshCache() {
|
// try {
|
// clearCatch();
|
// InitCatch();
|
// }catch (Throwable e) {
|
// ServerWithLog4j.logger.error("VolumeCatch-refreshCache", e);
|
// //throw getLocalVciError("P0010PLMAP-00012", e);
|
// }
|
// }
|
|
public static void setVolume(PvolumeInfo item) {
|
if (item == null)
|
return;
|
|
String jsonObj = JSONObject.toJSONString(item);
|
|
RedisUtil.getInstance().hset(CacheNames.VOLUMES, item.name.toLowerCase(), jsonObj);
|
}
|
|
|
public static void delVolume(String vol) {
|
if (StringUtils.isBlank(vol))
|
return;
|
|
RedisUtil.getInstance().hdel(CacheNames.VOLUMES, vol.toLowerCase());
|
}
|
|
|
@SuppressWarnings("unchecked")
|
private void InitVolumes() {
|
List<Pvolume> lstVolume = (List<Pvolume>) new HibernateTemplate().run(new HibernateCallback() {
|
public Object execute() throws HibernateException {
|
PvolumeDaoImp impl = new PvolumeDaoImp();
|
String hsql = "from Pvolume p order by p.name";
|
return impl.findEntities(hsql);
|
}
|
});
|
|
for (Pvolume item : lstVolume){
|
setVolume(changePvolumeToPvolumeInfo(item));
|
}
|
}
|
|
private PvolumeInfo changePvolumeToPvolumeInfo(Pvolume pvolume) {
|
PvolumeInfo pvoInfo = new PvolumeInfo();
|
pvoInfo.id = pvolume.getId() == null ? "" : pvolume.getId();
|
pvoInfo.name = pvolume.getName() == null ? "" : pvolume.getName();
|
pvoInfo.service = pvolume.getService() == null ? "" : pvolume.getService();
|
pvoInfo.host = pvolume.getHost() == null ? "" : pvolume.getHost();
|
pvoInfo.type = (short)(pvolume.getType() == 0 ? 0 : 1);
|
pvoInfo.path = pvolume.getPath() == null ? "" : pvolume.getPath();
|
pvoInfo.isvalid = pvolume.getIsvalid();
|
return pvoInfo;
|
}
|
}
|