package com.vci.server.portal.cache; import java.util.List; import java.util.ArrayList; import java.util.Arrays; import java.util.Map; import java.util.HashMap; import org.apache.commons.lang3.StringUtils; import com.vci.server.cache.redis.RedisUtil; import com.vci.server.portal.entity.PLActionEntity; import com.vci.server.portal.entity.PLTypeActionEntity; import com.vci.server.portal.service.PLActionEntityService; import com.vci.server.portal.service.PLTypeActionEntityService; import com.vci.server.portal.tools.ServerTool; import com.alibaba.fastjson.JSONObject; import com.vci.corba.portal.data.PLAction; import com.vci.corba.portal.data.PLTypeAction; /** * action缓存 * @author * */ public class ActionCacheUtil extends UICacheBaseUtil{ private static volatile ActionCacheUtil instance = null; public static ActionCacheUtil getInstance() { if (instance == null) { synchronized (ActionCacheUtil.class) { if (instance == null) { instance = new ActionCacheUtil(); } } } return instance; } public static void initCache() { try { getInstance().initPLAction(); } catch (Throwable e) { e.printStackTrace(); } } private ActionCacheUtil() { super(UICacheNames.ACTION, PLAction.class); } @Override public void setObject(PLAction obj) { if (obj == null) return; String jsonObj = JSONObject.toJSONString(obj); RedisUtil.getInstance().hset(getName(), obj.plOId, jsonObj); } @Override public PLAction[] getObjects() { List list = getObjectList(); return list.toArray(new PLAction[0]); } @Override public void delObject(PLAction obj) { if (obj == null || StringUtils.isBlank(obj.plOId)) return; RedisUtil.getInstance().hdel(getName(), obj.plOId); } private void initPLAction() throws Throwable { RedisUtil.getInstance().del(getName()); List list = PLActionEntityService.getInstance().getAllPLActionEntity(); for(int i = 0; i < list.size(); i++){ PLActionEntity obj = list.get(i); PLAction action = ServerTool.getPLAction(obj); if(action != null){ setObject(action); } } initTypeActions(); } private void initTypeActions() throws Throwable { List list = PLTypeActionEntityService.getInstance().getAllTypeAction(); Map> mapTemp = new HashMap>(); for(PLTypeActionEntity ent : list){ List lstId = mapTemp.get(ent.getTypeName()); if (lstId == null) { lstId = new ArrayList(); mapTemp.put(ent.getTypeName(), lstId); } lstId.add(ent.getActionoId()); } for (String key : mapTemp.keySet()) { List lstTemp = mapTemp.get(key); RedisUtil.getInstance().hset(getName(), "MAP-" + key.toLowerCase(), String.join(";", lstTemp)); } } public PLAction[] getActionsByType(String typeName) { String temp = RedisUtil.getInstance().hget(getName(), "MAP-" + typeName.toLowerCase()); if (StringUtils.isBlank(temp)) return new PLAction[0]; String oids[] = temp.split(";"); List lstObj = new ArrayList(); for (String oid : oids) { PLAction ap = getObject(oid); if (ap != null) lstObj.add(ap); } return lstObj.toArray(new PLAction[0]); } public void delTypeAction(String typeName, String actionId) { String temp = RedisUtil.getInstance().hget(getName(), "MAP-" + typeName.toLowerCase()); if (StringUtils.isBlank(temp)) return; List lstOid = Arrays.asList(temp.split(";")); lstOid.remove(actionId); RedisUtil.getInstance().hset(getName(), "MAP-" + typeName.toLowerCase(), String.join(";", lstOid)); } public void delTypeAction(PLTypeAction typeAction) { delTypeAction(typeAction.typeName, typeAction.actionoId); } public void setTypeAction(PLTypeAction typeAction) { String temp = RedisUtil.getInstance().hget(getName(), typeAction.typeName.toLowerCase()); if (StringUtils.isBlank(temp)) return; List lstOid = Arrays.asList(temp.split(";")); if (!lstOid.contains(typeAction.plOId)) { lstOid.add(typeAction.plOId); RedisUtil.getInstance().hset(getName(), typeAction.typeName.toLowerCase(), String.join(";", lstOid)); } } }