/** * 更新节点级联关系 * * @param parent * @param cacheKeys */ private void updateCacheTypeTree(CacheNode parent, String[] cacheKeys, String cacheKey) { String nodeId = parent.getId() + "_" + cacheKeys[0]; // 检查是否已经有指定子节点,无就创建 CacheNode node = parent.getCacheNodes().get(nodeId); if (node == null) { node = new CacheNode(nodeId); parent.getCacheNodes().put(nodeId, node); } if (cacheKeys.length > 1) updateCacheTypeTree( node, (String[]) ArrayUtils.subarray(cacheKeys, 1, cacheKeys.length), cacheKey); else node.getCacheKeys().add(cacheKey); }
/** * 查找对应类的节点 * * @param cacheTypeNode * @param cacheKeys * @return */ private CacheNode searchCacheTypeNode(CacheNode cacheTypeNode, String[] cacheKeys) { String nodeId = cacheTypeNode.getId() + "_" + cacheKeys[0]; CacheNode node = cacheTypeNode.getCacheNodes().get(nodeId); if (node == null || cacheKeys.length <= 1) return node; else return searchCacheTypeNode( node, (String[]) ArrayUtils.subarray(cacheKeys, 1, cacheKeys.length)); }
/** * 删除节点及其子节点所有缓存和缓存记录 * * @param cacheName * @param cacheNode * @param cascadeCacheNode */ private void removeAllCache(String cacheName, CacheNode cacheNode, boolean cascadeCacheNode) { synchronized (cacheNode) { // 清空自己的缓存记录 for (String cacheKey : cacheNode.getCacheKeys()) { try { getCache(cacheName).remove(cacheKey); } catch (Throwable t) { log.error(t); } } cacheNode.getCacheKeys().clear(); // 级联操作 if (cascadeCacheNode) { for (CacheNode node : cacheNode.getCacheNodes().values()) removeAllCache(cacheName, node, cascadeCacheNode); cacheNode.getCacheNodes().clear(); } } }
/** * 删除缓存 * * @param cacheName * @param cacheNode * @param cacheKey * @param cascadeCacheNode */ private void removeCache( String cacheName, CacheNode cacheNode, String cacheKey, boolean cascadeCacheNode) { // 删除缓存数据 synchronized (cacheNode) { try { getCache(cacheName).remove(cacheKey); } catch (Throwable t) { log.error(t); } // 删除节点缓存引用 cacheNode.getCacheKeys().remove(cacheKey); // 级联子级节点操作 if (cascadeCacheNode) { for (CacheNode node : cacheNode.getCacheNodes().values()) removeCache(cacheName, node, cacheKey, cascadeCacheNode); } } }