Ejemplo n.º 1
0
 /**
  * 查找对应类的节点
  *
  * @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));
 }
Ejemplo n.º 2
0
 /**
  * 更新节点级联关系
  *
  * @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);
 }
Ejemplo n.º 3
0
 private void moveToHead(CacheNode node) {
   if (node == first) return;
   if (node.prev != null) node.prev.next = node.next;
   if (node.next != null) node.next.prev = node.prev;
   if (last == node) last = node.prev;
   if (first != null) {
     node.next = first;
     first.prev = node;
   }
   first = node;
   node.prev = null;
   if (last == null) last = first;
 }
Ejemplo n.º 4
0
 /**
  * 更新缓存类型记录数
  *
  * @param cacheNodes
  * @param cacheKey
  */
 private void updateCacheNode(String cacheName, String[] cacheNodes, String cacheKey) {
   // 获取缓存树
   Map<String, CacheNode> tree = getCacheNodeTree(cacheName);
   // 获取缓存节点树
   String rootCacheNodeId = null;
   if (cacheNodes != null && cacheNodes.length > 0) rootCacheNodeId = cacheNodes[0];
   // 获取缓存树根节点
   if (!tree.containsKey(rootCacheNodeId))
     tree.put(rootCacheNodeId, new CacheNode(rootCacheNodeId));
   CacheNode root = tree.get(rootCacheNodeId);
   // 更新节点级联关系
   if (cacheNodes != null && cacheNodes.length > 1)
     updateCacheTypeTree(
         root, (String[]) ArrayUtils.subarray(cacheNodes, 1, cacheNodes.length), cacheKey);
   else root.getCacheKeys().add(cacheKey);
 }
Ejemplo n.º 5
0
 /**
  * 删除缓存
  *
  * @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);
     }
   }
 }
Ejemplo n.º 6
0
 /**
  * 删除节点及其子节点所有缓存和缓存记录
  *
  * @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();
     }
   }
 }
Ejemplo n.º 7
0
 public Object put(Object key, DataEntry value) {
   CacheNode node = (CacheNode) nodes.get(key);
   DataEntry remove = null;
   if (node == null) {
     if (currentSize >= cacheSize) {
       if (last != null) {
         CacheNode temp = (CacheNode) nodes.get(last.key);
         remove = temp.value;
         nodes.remove(last.key);
       }
       removeLast();
     } else {
       currentSize++;
     }
     node = new CacheNode();
   }
   node.value = value;
   node.key = key;
   moveToHead(node);
   nodes.put(key, node);
   return remove;
 }