隨筆-72  評論-20  文章-0  trackbacks-1

               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目錄,如下各種配置參考也可見該目錄下的范例配置,以下不再強調),見下:

           1TreeCache tree = new TreeCache();
           2tree.setClusterProperties("treecache.xml"[img]/images/wink.gif[/img]; 
           3tree.createService(); 
           4tree.startService(); 
           5tree.put("/a/b/c""name""Ben"[img]/images/wink.gif[/img];
           6tree.put("/a/b/c/d""uid"new Integer(322649));
           7Integer tmp = (Integer) tree.get("/a/b/c/d""uid"[img]/images/wink.gif[/img];
           8tree.remove("/a/b"[img]/images/wink.gif[/img];
           9tree.stopService();
          10tree.destroyService(); 
          11

          treecache.xml:
           1<server>
           2    <mbean code="org.jboss.cache.TreeCache"
           3        name="jboss.cache:service=TreeCache">
           4        <depends>jboss:service=Naming</depends>
           5        <depends>jboss:service=TransactionManager</depends>
           6        <attribute name="ClusterName">TreeCache-Cluster</attribute>
           7        <attribute name="ClusterConfig">
           8            <config>
           9                <UDP mcast_addr="228.1.2.3" mcast_port="48866"
          10                    ip_ttl="64" ip_mcast="true" 
          11                    mcast_send_buf_size="150000" mcast_recv_buf_size="80000"
          12                    ucast_send_buf_size="150000" ucast_recv_buf_size="80000"
          13                    loopback="false"/>
          14                <PING timeout="2000" num_initial_members="3"
          15                    up_thread="false" down_thread="false"/>
          16                <MERGE2 min_interval="10000" max_interval="20000"/>
          17                <FD_SOCK/>
          18                <VERIFY_SUSPECT timeout="1500"
          19                    up_thread="false" down_thread="false"/>
          20                <pbcast.NAKACK gc_lag="50" retransmit_timeout="600,1200,2400,4800"
          21                    max_xmit_size="8192" up_thread="false" down_thread="false"/>
          22                <UNICAST timeout="600,1200,2400" window_size="100" min_threshold="10"
          23                    down_thread="false"/>
          24                <pbcast.STABLE desired_avg_gossip="20000"
          25                    up_thread="false" down_thread="false"/>
          26                <FRAG frag_size="8192"
          27                    down_thread="false" up_thread="false"/>
          28                <pbcast.GMS join_timeout="5000" join_retry_timeout="2000"
          29                    shun="true" print_local_addr="true"/>
          30                <pbcast.STATE_TRANSFER up_thread="true" down_thread="true"/>
          31            </config>
          32        </attribute>
          33    </mbean>
          34</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實例做修改時,即刻返回。其配置見下:
          1<!--
          2   Valid modes are LOCAL
          3                   REPL_ASYNC
          4                   REPL_SYNC
          5                   INVALIDATION_ASYNC
          6                   INVALIDATION_SYNC
          7-->
          8<attribute name="CacheMode">REPL_SYNC</attribute>
          9

               二、事務和并行(Transaction And Concurrent)
               TreeCache是一種事務Cache,與JDBC一樣,其包括兩方面內容:鎖和隔離級別。鎖分為悲觀鎖和樂觀鎖,當使用悲觀鎖時,分為五個隔離級別,分別是SERIALIZABLE、REPEATABLE_READ (default)、READ_COMMITTED、READ_UNCOMMITTED和NONE,隔離級別逐步減弱。樂觀鎖也叫版本鎖,其對數據進行操作時,將其復制到臨時區,操作之后將版本與原有數據比較,如果一致則將遞增版本并寫回,如果不一致則回滾,由于樂觀鎖僅在復制出數據和提交數據時對數據加鎖,所以并行度更高,但如果寫操作比較頻繁地話則容易出現沖突導致回滾。TreeCache默認使用悲觀鎖。使用TreeCache時,需要使用容器提供的事務管理器,一般使JBossTransactionManagerLookup和GenericTransactionManagerLookup,前者應用于JBOSS服務器,后者應用于其他服務器,也可使用DummyTransactionManagerLookup用于測試。如上介紹的配置如下:
          1<attribute name="NodeLockingScheme">OPTIMISTIC</attribute>
          2       <attribute name="IsolationLevel">REPEATABLE_READ</attribute>
          3      <attribute name="TransactionManagerLookupClass">org.jboss.cache.DummyTransactionManagerLookup</attribute>
          4 
          5

               三、逐出策略(Eviction Policy)
               由于內存數量的局限,不可能將所有的Cache數據存放在內存中,但使用內存達到一定極限時,會將部分數據清除出內存,保存到其它持久媒質中,定義的什么時候清除、如何清除的策略就是逐出策略。自定義一個逐出策略需要實現org.jboss.cache.eviction.EvictionPolicy、org.jboss.cache.eviction.EvictionAlgorithm、
          org.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包的源代碼。配置如下:
           1<attribute name="EvictionPolicyConfig">
           2   <config>
           3      <attribute name="wakeUpIntervalSeconds">5</attribute>
           4      <region name="/_default_">
           5        <attribute name="maxNodes">5000</attribute>
           6        <attribute name="timeToLiveSeconds">1000</attribute>
           7      </region>
           8      <region name="/org/jboss/data"
           9policyClass="org.jboss.cache.eviction.FIFOPolicy">
          10        <attribute name="maxNodes">5000</attribute>
          11      </region>
          12      <region name="/test/" policyClass="org.jboss.cache.eviction.MRUPolicy">
          13        <attribute name="maxNodes">10000</attribute>
          14      </region>
          15      <region name="/maxAgeTest/">
          16        <attribute name="maxNodes">10000</attribute>
          17        <attribute name="timeToLiveSeconds">8</attribute>
          18        <attribute name="maxAgeSeconds">10</attribute>
          19      </region>
          20   </config>
          21</attribute>
          22
               四、Cache加載
              由于逐出策略的存在,那么當我們重新需要獲得一個原來在緩存中但確由內存原因被逐出的數據時,就需要定義一種加載策略,使地可以重新找回數據,同時,Cache加載也肩負在將數據逐出時將數據保存到持久媒質的責任。
              根據將數據保存媒質的不同,Cache加載包括FileCacheLoader、JDBCCacheLoader等等,可以同時使用多種加載器來靈活定制加載策略。例見下:
           1<attribute name="CacheLoaderConfiguration">
           2    <config>
           3        <passivation>false</passivation>
           4        <preload>/</preload>
           5        <shared>true</shared>
           6        <cacheloader>
           7            <class>org.jboss.cache.loader.ClusteredCacheLoader</class>
           8            <properties>
           9                 timeout=1000
          10            </properties>
          11            <async>true</async>
          12            <fetchPersistentState>false</fetchPersistentState>
          13            ignoreModifications>false</ignoreModifications>
          14            <purgeOnStartup>false</purgeOnStartup>
          15        </cacheloader>
          16        <cacheloader>
          17            <class>org.jboss.cache.loader.JDBCCacheLoader</class>
          18            <properties>
          19                        cache.jdbc.table.name=jbosscache
          20                        cache.jdbc.table.create=true
          21                        cache.jdbc.table.drop=true
          22                        cache.jdbc.table.primarykey=jbosscache_pk
          23                        cache.jdbc.fqn.column=fqn
          24                        cache.jdbc.fqn.type=varchar(255)
          25                        cache.jdbc.node.column=node
          26                        cache.jdbc.node.type=longblob
          27                        cache.jdbc.parent.column=parent
          28                        cache.jdbc.driver=com.mysql.jdbc.Driver
          29                        cache.jdbc.url=jdbc:mysql://localhost:3306/jbossdb
          30                        cache.jdbc.user=root
          31                        cache.jdbc.password=
          32             </properties>
          33             <async>true</async>
          34             <fetchPersistentState>false</fetchPersistentState>
          35             <purgeOnStartup>false</purgeOnStartup>
          36         </cacheloader>
          37    </config>
          38</attribute>
          39

             我們將通過定制如上的配置信息以更有效地使用JBossCache。詳細情況可參考JBoss TreeCache參考文檔和范例。
          posted on 2007-07-03 15:20 前方的路 閱讀(2897) 評論(3)  編輯  收藏

          評論:
          # re: JBossCache-TreeCache體驗[未登錄] 2008-02-26 11:49 | test
          恨好  回復  更多評論
            
          # re: JBossCache-TreeCache體驗[未登錄] 2008-02-26 11:50 | test
          如果能夠更詳細的給出整個實現過程就再好不過,可以發個配置(二)嗎?
            回復  更多評論
            
          # re: JBossCache-TreeCache體驗 2008-02-27 17:37 | jute
          如果能夠更詳細的給出整個實現過程就再好不過  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 调兵山市| 兴国县| 会昌县| 大丰市| 盈江县| 泰安市| 延庆县| 邵武市| 应城市| 全州县| 巴东县| 巫山县| 温州市| 唐河县| 镇赉县| 承德县| 峨眉山市| 泽普县| 潼关县| 莱西市| 邮箱| 白朗县| 孝义市| 龙陵县| 罗甸县| 翼城县| 宿松县| 辽阳市| 南阳市| 泸西县| 山西省| 集贤县| 吉水县| 察隅县| 永昌县| 伊宁县| 花莲市| 乌鲁木齐县| 鱼台县| 普定县| 洪江市|