Sealyu

--- 博客已迁移至: http://www.sealyu.com/blog

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  618 随笔 :: 87 文章 :: 225 评论 :: 0 Trackbacks
TreeCache是一种结构化的、基于复制的事务缓存。TreeCache是JBoss应用服务器中集群服务—包括JNDI集群、HTTP和EJB的 Sesssion集群、JMS集群—的基础框架。其可以单独使用,可以集成到JBossAS应用,也可以集成到其他的应用服务器上。TreeCache是 一种树状结构,每个节点拥有一个名字和多个或者没有子节点,除跟节点没有子节点其他节点有且只有一个父母节点,可以通过路径名来访问子节点 (FQN:Full Qualified Name),在一个TreeCache中可以存在多棵树,,即可以有多个根节点。当应用于分布式环境时,由于TreeCache是基于复制的,每个子节点 的值必须是可序列化的。
     在下面中,将通过例子来了解TreeCache的功能及其配置,使用JBossCache1.4和JDK5.0。首先是一个最基本使用TreeCache 的程序例子并配置一个TreeCache的配置骨架(各种常用的配置可参见jboss-cache-dist-1.4.0.CR1版本的etc目录,如下 各种配置参考也可见该目录下的范例配置,以下不再强调),见下:

treecache.xml:


<server>
    <mbean code="org.jboss.cache.TreeCache"
        name="jboss.cache:service=TreeCache">
        <depends>jboss:service=Naming</depends>
        <depends>jboss:service=TransactionManager</depends>
        <attribute name="ClusterName">TreeCache-Cluster</attribute>
        <attribute name="ClusterConfig">
            <config>
                <UDP mcast_addr="228.1.2.3" mcast_port="48866"
                    ip_ttl="64" ip_mcast="true"
                    mcast_send_buf_size="150000" mcast_recv_buf_size="80000"
                    ucast_send_buf_size="150000" ucast_recv_buf_size="80000"
                    loopback="false"/>
                <PING timeout="2000" num_initial_members="3"
                    up_thread="false" down_thread="false"/>
                <MERGE2 min_interval="10000" max_interval="20000"/>
                <FD_SOCK/>
                <VERIFY_SUSPECT timeout="1500"
                    up_thread="false" down_thread="false"/>
                <pbcast.NAKACK gc_lag="50" retransmit_timeout="600,1200,2400,4800"
                    max_xmit_size="8192" up_thread="false" down_thread="false"/>
                <UNICAST timeout="600,1200,2400" window_size="100" min_threshold="10"
                    down_thread="false"/>
                <pbcast.STABLE desired_avg_gossip="20000"
                    up_thread="false" down_thread="false"/>
                <FRAG frag_size="8192"
                    down_thread="false" up_thread="false"/>
                <pbcast.GMS join_timeout="5000" join_retry_timeout="2000"
                    shun="true" print_local_addr="true"/>
                <pbcast.STATE_TRANSFER up_thread="true" down_thread="true"/>
            </config>
        </attribute>
    </mbean>
</server>
   其中ClusterConfig配置在前面JavaGroups的介绍详细介绍,其它配置比较简单,需要进一步了解请参见TreeCache文档。
   一、Cache分类
   TreeCache按功能分为三类:本地(Local)Cache、复制(Replication)Cache和失效 (Invalidation)Cache。本地Cache只应用于本地环境,后两个Cache可应用于分布式环境,其中,在分布式环境中,复制Cache 当一个Cache实例的一个节点值发生变化时会将变化复制到其它实例中,而失效Cache是当一个Cache实例的一个节点值发生变化时会将其它实例的相 应节点的值设为空,让其重新去获得该值,可通过这种方式缓存大对象以减少在实例中复制对象的代价。分布式Cache(复制和失效Cache)又分为两种, 同步(REPL_ASYNC)和异步(REPL_SYNC),同步Cache是在一个Cache实例做修改时,等待变化应用到其它实例后才返回,而异步 Cache是在一个Cache实例做修改时,即刻返回。其配置见下:
!--
  Valid modes are LOCAL
                  REPL_ASYNC
                  REPL_SYNC
                  INVALIDATION_ASYNC
                  INVALIDATION_SYNC
->
attribute name="CacheMode">REPL_SYNC</attribute>

   二、事务和并行(Transaction And Concurrent)
   TreeCache是一种事务Cache,与JDBC一样,其包括两方面内容:锁和隔离级别。锁分为悲观锁和乐观锁,当使用悲观锁时,分为五个隔离级别, 分别是SERIALIZABLE、REPEATABLE_READ (default)、READ_COMMITTED、READ_UNCOMMITTED和NONE,隔离级别逐步减弱。乐观锁也叫版本锁,其对数据进行操 作时,将其复制到临时区,操作之后将版本与原有数据比较,如果一致则将递增版本并写回,如果不一致则回滚,由于乐观锁仅在复制出数据和提交数据时对数据加 锁,所以并行度更高,但如果写操作比较频繁地话则容易出现冲突导致回滚。TreeCache默认使用悲观锁。使用TreeCache时,需要使用容器提供 的事务管理器,一般使JBossTransactionManagerLookup和GenericTransactionManagerLookup, 前者应用于JBOSS服务器,后者应用于其他服务器,也可使用DummyTransactionManagerLookup用于测试。如上介绍的配置如 下:
attribute name="NodeLockingScheme">OPTIMISTIC</attribute>
      <attribute name="IsolationLevel">REPEATABLE_READ</attribute>
     <attribute name="TransactionManagerLookupClass">org.jboss.cache.DummyTransactionManagerLookup</attribute>


   三、逐出策略(Eviction Policy)
   由于内存数量的局限,不可能将所有的Cache数据存放在内存中,但使用内存达到一定极限时,会将部分数据清除出内存,保存到其它持久媒质中,定义的什么 时候清除、如何清除的策略就是逐出策略。自定义一个逐出策略需要实现org.jboss.cache.eviction.EvictionPolicy、 org.jboss.cache.eviction.EvictionAlgorithm、
g.jboss.cache.eviction.EvictionQueue 和org.jboss.cache.eviction.EvictionConfiguration四个接口,系统提供了LRU(Least recently used,最近最少使用)、LFU(Least Frequently Used最不经常使用)、FIFO(First In First Out先进先出)、MRU(Most Recently Used最近最经常使用)四种实现,详细参见org.jboss.cache.eviction包的源代码。配置如下:
<attribute name="EvictionPolicyConfig">
   <config>
      <attribute name="wakeUpIntervalSeconds">5</attribute>
      <region name="/_default_">
        <attribute name="maxNodes">5000</attribute>
        <attribute name="timeToLiveSeconds">1000</attribute>
      </region>
      <region name="/org/jboss/data"
policyClass="org.jboss.cache.eviction.FIFOPolicy">
        <attribute name="maxNodes">5000</attribute>
      </region>
      <region name="/test/" policyClass="org.jboss.cache.eviction.MRUPolicy">
        <attribute name="maxNodes">10000</attribute>
      </region>
      <region name="/maxAgeTest/">
        <attribute name="maxNodes">10000</attribute>
        <attribute name="timeToLiveSeconds">8</attribute>
        <attribute name="maxAgeSeconds">10</attribute>
      </region>
   </config>
</attribute>
     四、Cache加载
  由于逐出策略的存在,那么当我们重新需要获得一个原来在缓存中但确由内存原因被逐出的数据时,就需要定义一种加载策略,使地可以重新找回数据,同 时,Cache加载也肩负在将数据逐出时将数据保存到持久媒质的责任。
  根据将数据保存媒质的不同,Cache加载包括FileCacheLoader、JDBCCacheLoader等等,可以同时使用多种加载器来灵活定制 加载策略。例见下:
<attribute name="CacheLoaderConfiguration">
    <config>
        <passivation>false</passivation>
        <preload>/</preload>
        <shared>true</shared>
        <cacheloader>
            <class>org.jboss.cache.loader.ClusteredCacheLoader</class>
            <properties>
                 timeout=1000
            </properties>
            <async>true</async>
            <fetchPersistentState>false</fetchPersistentState>
            ignoreModifications>false</ignoreModifications>
            <purgeOnStartup>false</purgeOnStartup>
        </cacheloader>
        <cacheloader>
            <class>org.jboss.cache.loader.JDBCCacheLoader</class>
            <properties>
                        cache.jdbc.table.name=jbosscache
                        cache.jdbc.table.create=true
                        cache.jdbc.table.drop=true
                        cache.jdbc.table.primarykey=jbosscache_pk
                        cache.jdbc.fqn.column=fqn
                        cache.jdbc.fqn.type=varchar(255)
                        cache.jdbc.node.column=node
                        cache.jdbc.node.type=longblob
                        cache.jdbc.parent.column=parent
                        cache.jdbc.driver=com.mysql.jdbc.Driver
                        cache.jdbc.url=jdbc:mysql://localhost:3306/jbossdb
                        cache.jdbc.user=root
                        cache.jdbc.password=
             </properties>
             <async>true</async>
             <fetchPersistentState>false</fetchPersistentState>
             <purgeOnStartup>false</purgeOnStartup>
         </cacheloader>
    </config>
</attribute>

我们将通过定制如上的配置信息以更有效地使用JBossCache。详细情况可参考JBoss TreeCache参考文档和范例。

TreeCache tree = new TreeCache();
tree.setClusterProperties("treecache.xml";
tree.createService();
tree.startService();
tree.put("/a/b/c", "name", "Ben";
tree.put("/a/b/c/d", "uid", new Integer(322649));
Integer tmp = (Integer) tree.get("/a/b/c/d", "uid";
tree.remove("/a/b";
tree.stopService();
tree.destroyService();
posted on 2010-05-18 04:19 seal 阅读(371) 评论(0)  编辑  收藏 所属分类: web服务器

只有注册用户登录后才能发表评论。


网站导航: