package com.vci.server.portal.cache; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; import com.vci.server.cache.redis.RedisUtil; import com.vci.server.portal.service.PortalVIEntityService; import com.alibaba.fastjson.JSONObject; import com.vci.corba.common.VCIError; import com.vci.corba.portal.data.PortalVI; /** * protalVI缓存 * @author zhangxg * */ public class PortalVICacheUtil extends UICacheBaseUtil{ private static volatile PortalVICacheUtil instance = null; public static void initCache() { try { getInstance().initPortalVI(); } catch (Throwable e) { e.printStackTrace(); } } private PortalVICacheUtil() { super(UICacheNames.PORTALVI, PortalVI.class); } public static PortalVICacheUtil getInstance() { if (instance == null) { synchronized (PortalVICacheUtil.class) { if (instance == null) { instance = new PortalVICacheUtil(); } } } return instance; } @Override public void setObject(PortalVI obj) { if (obj == null) return; String jsonObj = JSONObject.toJSONString(obj); RedisUtil.getInstance().hset(getName(), obj.id, jsonObj); } @Override public PortalVI[] getObjects() { List list = getObjectList(); return list.toArray(new PortalVI[0]); } @Override public void delObject(PortalVI obj) { if (obj == null || StringUtils.isBlank(obj.id)) return; RedisUtil.getInstance().hdel(getName(), obj.id); } // private void initPortalVI() throws Throwable { // lstPortalVis.clear(); // mapType2VIs.clear(); // mapID2VI.clear(); // mapName2VI.clear(); // // System.gc(); // PortalVI[] pvis = PortalVIEntityService.getInstance().getAllPortalVI(); // // // // //PortalVI[] pvis = lstPortalVis; // List pviList = null; // for (PortalVI pvi : pvis) { // lstPortalVis.add(pvi); // mapID2VI.put(pvi.id, pvi); // mapName2VI.put(pvi.viName, pvi); // // if (mapType2VIs.get(pvi.typeName) == null) { // pviList = new ArrayList(); // mapType2VIs.put(pvi.typeName, pviList); // } else { // pviList = mapType2VIs.get(pvi.typeName); // } // pviList.add(pvi); // } // } private void initPortalVI() throws Throwable{ RedisUtil.getInstance().del(getName()); PortalVI[] pvis = PortalVIEntityService.getInstance().getAllPortalVI(); Map> mapTemp = new HashMap>(); for(PortalVI pvi : pvis){ if(pvi != null){ setObject(pvi); } List lstTemp = mapTemp.get(pvi.typeName); if (lstTemp == null) { lstTemp = new ArrayList(); mapTemp.put(pvi.typeName, lstTemp); } lstTemp.add(pvi.id); } for (String key : mapTemp.keySet()) { List lstTemp = mapTemp.get(key); RedisUtil.getInstance().hset(getName(), TYPEMAP + key, String.join(";", lstTemp)); } } public PortalVI[] getPortalVIArrayByTypeName(String typeName) throws VCIError { String temp = RedisUtil.getInstance().hget(getName(), TYPEMAP + typeName); if (StringUtils.isBlank(temp)) return new PortalVI[0]; String oids[] = temp.split(";"); List lstObj = new ArrayList(); for (String oid : oids) { PortalVI ap = getObject(oid); if (ap != null) lstObj.add(ap); } return lstObj.toArray(new PortalVI[0]); } public PortalVI getObjectByName(String name) throws VCIError { return null; } // public PortalVI[] getPortalVIArrayByTypeName(String typeName) throws VCIError { // refreshCache(); // if (this.mapType2VIs.get(typeName) != null) { // return this.mapType2VIs.get(typeName).toArray(new PortalVI[0]); // } // return new PortalVI[0]; // } // public PortalVI[] getPagePortalVIArrayByCondition(String typeName, // short sheetType, String sheetName, long startPage, long endPage) // throws Throwable { // // if (this.mapType2VIs.get(typeName) != null) { // List list = new ArrayList(); // List allList = this.mapType2VIs.get(typeName); // List currentList = new ArrayList(); // int size = allList.size(); // for (int i = 0; i < size; i++) { // PortalVI vi = allList.get(i); // if (sheetType != -1 && vi.viType != sheetType) { // continue; // } // if (vi.viName.contains(sheetName)) { // currentList.add(vi); // } // } // size = currentList.size(); // for (int i = (int)startPage - 1; i < (int)endPage - 1; i++) { // if (i >= size) { // break; // } // list.add(currentList.get(i)); // } // return list.toArray(new PortalVI[0]); // } // return new PortalVI[0]; // } public int getPortalVICountByTypeName(String typeName) throws VCIError { String temp = RedisUtil.getInstance().hget(getName(), typeName); if (StringUtils.isBlank(temp)) return 0; String oids[] = temp.split(";"); return oids.length; } public PortalVI[] getPortalVIByObjTypeAndLikeName(String typeName, String viName) throws VCIError { String temp = RedisUtil.getInstance().hget(getName(), TYPEMAP + typeName); if (StringUtils.isBlank(temp)) return new PortalVI[0]; String oids[] = temp.split(";"); List lstObj = new ArrayList(); for (String oid : oids) { PortalVI vi = getObject(oid); if (vi != null && vi.viName.contains(viName)) lstObj.add(vi); } return lstObj.toArray(new PortalVI[0]); } public PortalVI getPortalVIByCondition(String typeName, int sheetType, String sheetName) throws VCIError { String temp = RedisUtil.getInstance().hget(getName(), TYPEMAP + typeName); if (StringUtils.isBlank(temp)) return null; String oids[] = temp.split(";"); //List lstObj = new ArrayList(); for (String oid : oids) { PortalVI vi = getObject(oid); if (vi != null && (sheetType == -1 || vi.viType == sheetType) && vi.viName.equals(sheetName)) return vi; } return null; } public int getPortalVICountByCondition(String typeName, int sheetType, String sheetName) throws VCIError { String temp = RedisUtil.getInstance().hget(getName(), TYPEMAP + typeName); if (StringUtils.isBlank(temp)) return 0; String oids[] = temp.split(";"); //List lstObj = new ArrayList(); int count = 0; for (String oid : oids) { PortalVI vi = getObject(oid); if (vi != null && (sheetType == -1 || vi.viType == sheetType) && vi.viName.contains(sheetName)) count++; } return count;//lstObj.toArray(new PortalVI[0]); // refreshCache(); // if (this.mapType2VIs.get(typeName) != null) { // List allList = this.mapType2VIs.get(typeName); // List currentList = new ArrayList(); // int size = allList.size(); // for (int i = 0; i < size; i++) { // PortalVI vi = allList.get(i); // if (sheetType != -1 && vi.viType != sheetType) { // continue; // } // if (vi.viName.contains(sheetName)) { // currentList.add(vi); // } // } // size = currentList.size(); // return size; // } // return 0; } // public PortalVI getPortalVIBySymbol(String symbol) throws VCIError { // refreshCache(); // if (this.mapName2VI.get(symbol) != null) { // return this.mapName2VI.get(symbol); // } else { // return ServerTool.getDefaultPortalVI(); // } // } }