首页

关于hibernate源码包中的TreeCache基于jboss的TreeCache结构化的、基于复制的事务缓存自定义实现

标签:hibernate,TreeCache,jboss,TreeCache,事务缓存     发布时间:2018-06-04   

一、前言

关于hibernate(3.0.1)源码包中org.hibernate.cache.TreeCache缓存类,基于org.jboss.cache.TreeCache结构化的、可复制的事务缓存(jboss-cache-1.2.2.jar),分布在分布式缓存间同步、分布式事务及同步锁控制、逐出策略(Eviction Policy) - LRU(Least recently used,最近最少使用)、LFU(Least Frequently Used最不经常使用)、FIFO(First In First Out先进先出)、MRU(Most Recently Used最近最经常使用)四种实现等场景使用。

二、源码说明 

package org.hibernate.cache;@b@@b@import java.util.HashMap;@b@import java.util.Iterator;@b@import java.util.Map;@b@import java.util.Set;@b@import javax.transaction.Transaction;@b@import javax.transaction.TransactionManager;@b@import org.jboss.cache.Fqn;@b@@b@public class TreeCache@b@  implements Cache@b@{@b@  private static final String ITEM = "item";@b@  private org.jboss.cache.TreeCache cache;@b@  private final String regionName;@b@  private final String userRegionName;@b@  private final TransactionManager transactionManager;@b@@b@  public TreeCache(org.jboss.cache.TreeCache cache, String regionName, TransactionManager transactionManager)@b@    throws CacheException@b@  {@b@    this.cache = cache;@b@    this.userRegionName = regionName;@b@    this.regionName = '/' + regionName.replace('.', '/');@b@    this.transactionManager = transactionManager;@b@  }@b@@b@  public Object get(Object key) throws CacheException {@b@    try {@b@      return this.cache.get(new Fqn(new Object[] { this.regionName, key }), "item");@b@    }@b@    catch (Exception e) {@b@      throw new CacheException(e);@b@    }@b@  }@b@@b@  public void update(Object key, Object value) throws CacheException {@b@    try {@b@      this.cache.put(new Fqn(new Object[] { this.regionName, key }), "item", value);@b@    }@b@    catch (Exception e) {@b@      throw new CacheException(e);@b@    }@b@  }@b@@b@  public void put(Object key, Object value) throws CacheException@b@  {@b@    try {@b@      Transaction tx = (this.transactionManager == null) ? null : this.transactionManager.suspend();@b@      this.cache.putFailFast(new Fqn(new Object[] { this.regionName, key }), "item", value, 0L);@b@      if (tx != null) this.transactionManager.resume(tx);@b@    }@b@    catch (Exception e) {@b@      throw new CacheException(e);@b@    }@b@  }@b@@b@  public void remove(Object key) throws CacheException {@b@    try {@b@      this.cache.remove(new Fqn(new Object[] { this.regionName, key }));@b@    }@b@    catch (Exception e) {@b@      throw new CacheException(e);@b@    }@b@  }@b@@b@  public void clear() throws CacheException {@b@    try {@b@      this.cache.remove(new Fqn(this.regionName));@b@    }@b@    catch (Exception e) {@b@      throw new CacheException(e);@b@    }@b@  }@b@@b@  public void destroy() throws CacheException {@b@    clear();@b@  }@b@@b@  public void lock(Object key) throws CacheException {@b@    throw new UnsupportedOperationException("TreeCache is a fully transactional cache" + this.regionName);@b@  }@b@@b@  public void unlock(Object key) throws CacheException {@b@    throw new UnsupportedOperationException("TreeCache is a fully transactional cache: " + this.regionName);@b@  }@b@@b@  public long nextTimestamp() {@b@    return (System.currentTimeMillis() / 100L);@b@  }@b@@b@  public int getTimeout() {@b@    return 600;@b@  }@b@@b@  public String getRegionName() {@b@    return this.userRegionName;@b@  }@b@@b@  public long getSizeInMemory() {@b@    return -1L;@b@  }@b@@b@  public long getElementCountInMemory() {@b@    try {@b@      return this.cache.getChildrenNames(new Fqn(this.regionName)).size();@b@    }@b@    catch (Exception e) {@b@      throw new CacheException(e);@b@    }@b@  }@b@@b@  public long getElementCountOnDisk() {@b@    return 0L; }@b@@b@  public Map toMap() {@b@    Map result;@b@    try {@b@      result = new HashMap();@b@      Iterator iter = this.cache.getChildrenNames(new Fqn(this.regionName)).iterator();@b@      while (iter.hasNext()) {@b@        Object key = iter.next();@b@        result.put(key, this.cache.get(new Fqn(new Object[] { this.regionName, key }), "item"));@b@      }@b@      return result;@b@    }@b@    catch (Exception e) {@b@      throw new CacheException(e);@b@    }@b@  }@b@@b@  public String toString() {@b@    return "TreeCache(" + this.userRegionName + ')';@b@  }@b@}