自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

JBoss Cache:企業(yè)級Java事務緩存集群系統(tǒng)

開發(fā) 后端
JBoss Cache是一款基于Java的事務處理緩存系統(tǒng),它的目標是構(gòu)建一個以Java框架為基礎的集群解決方案,可以是服務器應用,也可以是Java SE應用。

JBoss Cache是一款基于Java的事務處理緩存系統(tǒng),它的目標是構(gòu)建一個以Java框架為基礎的集群解決方案,可以是服務器應用,也可以是Java SE應用。

集群高可用性

JBoss Cache將會自動復制緩存數(shù)據(jù),并且在集群中的服務器之間進行緩存數(shù)據(jù)的同步,這樣可以保證任何一臺服務器重啟了都不會影響緩存的可用性。

集群緩存可避免系統(tǒng)瓶頸

JBoss Cache顧名思義是利用緩存來提高系統(tǒng)擴展性的,當我們的WEB系統(tǒng)遇到大量的數(shù)據(jù)庫讀寫時,系統(tǒng)的瓶頸將會出現(xiàn)在數(shù)據(jù)庫端,JBoss Cache正好可以解決數(shù)據(jù)庫的頻繁讀取問題,解決這個瓶頸。

另外,由于JBoss Cache的緩存是在集群中的每一個服務器間同步的,因此也不會因為一臺緩存服務器遇到性能問題而影響整個系統(tǒng)。

JBoss Cache的standalone用法

首先是初始化TreeCache

  1. TreeCache tree = new TreeCache(); 

然后是讀進配置文件

  1. PropertyConfigurator config = new PropertyConfigurator(); 
  2. config.configure("配置文件.xml"); 

然后開始服務

  1. Tree.startService(); 

因為Tree的結(jié)構(gòu)是用NODE來Access的,TreeCache這里就很簡單的用:

/level1/level2/node1 來表示兩級Tree下面的Node1。

現(xiàn)在我們添加幾個要Cache的對象。

  1. Tree.put("/level1/level2/node1""key1""value1"); 
  2. String[] array = { "1""2""3""4" } 
  3. Tree.put("/level3/array/""myarray", array); 

大家可以看到,TreeCache里面可以存儲任何種類的對象,包括所有復雜對象。

讀取對象就很方便了,

  1. String s = (String)Tree.get("/level1/level2/node1/""key1"); 

value1就讀出來了。

同理:

  1. String[] sarr = (String[]) Tree.get("/level3/array/","myarray"); 

System.out.println(sarr[1]) 會顯示2

最后停止服務:

  1. Tree.stopService(); 

JBoss Cache的FileCacheLoader示例

首先創(chuàng)建一個FileCache類封裝JBoss Cache的相關操作,如下:

  1. package com.javaeye.terrencexu.jbosscache;   
  2.  
  3. import java.io.File;   
  4. import java.util.Map;   
  5.  
  6. import org.jboss.cache.Cache;   
  7. import org.jboss.cache.DefaultCacheFactory;   
  8. import org.jboss.cache.Fqn;   
  9. import org.jboss.cache.Node;   
  10. import org.jboss.cache.config.CacheLoaderConfig;   
  11. import org.jboss.cache.config.Configuration;   
  12. import org.jboss.cache.loader.FileCacheLoader;   
  13. import org.jboss.cache.loader.FileCacheLoaderConfig;   
  14.  
  15. /**  
  16.  * <p>  
  17.  * This is demo to illustrate how to use the JBoss Cache to cache your  
  18.  * frequently accessed Java objects in order to dramatically improve  
  19.  * the performance of your applications. This makes it easy to remove  
  20.  * data access bottlenecks, such as connecting to a database.  
  21.  * </p>  
  22.  * <p>  
  23.  * As a rule of thumb, it is recommended that the FileCacheLoader not   
  24.  * be used in a highly concurrent, transactional or stressful environment,  
  25.  * ant its use is restricted to testing.  
  26.  * </p>  
  27.  *   
  28.  * @author TerrenceX  
  29.  *  
  30.  * @param <T>  
  31.  */   
  32. public class FileCache<T> {   
  33.  
  34.     /**  
  35.      * The JBoss Cache, used to cache frequently accessed Java objects.  
  36.      */   
  37.     private Cache<String, T> cache;   
  38.  
  39.     /**  
  40.      * @constructor  
  41.      * @param fsCacheLoaderLocation The file system location to store the cache  
  42.      */   
  43.     public FileCache(File fsCacheLoaderLocation) {   
  44.         cache = initCache(fsCacheLoaderLocation);   
  45.     }   
  46.  
  47.     /**  
  48.      * Create a Cache and whose cache loader type is File Cache Loader  
  49.      *   
  50.      * @param fsCacheLoaderLocation The file position used to store the cache.  
  51.      *   
  52.      * @return Cache  
  53.      */   
  54.     public Cache<String, T> initCache(File fsCacheLoaderLocation) {   
  55.         // initiate a FileCacheLoader instance   
  56.         FileCacheLoader fsCacheLoader = new FileCacheLoader();   
  57.  
  58.         // prepare the file cache loader configuration file for File Cache Loader   
  59.         FileCacheLoaderConfig fsCacheLoaderConfig = new FileCacheLoaderConfig();   
  60.         fsCacheLoaderConfig.setLocation(fsCacheLoaderLocation.toString());   
  61.         fsCacheLoaderConfig.setCacheLoader(fsCacheLoader);   
  62.  
  63.         // set configuration to File Cache Loader   
  64.         fsCacheLoader.setConfig(fsCacheLoaderConfig);   
  65.  
  66.         // prepare the configuration for Cache   
  67.         Configuration config = new Configuration();   
  68.         config.setCacheLoaderConfig(new CacheLoaderConfig());   
  69.         config.getCacheLoaderConfig().addIndividualCacheLoaderConfig(fsCacheLoaderConfig);   
  70.  
  71.         // create a Cache through the default cache factory   
  72.         return new DefaultCacheFactory<String, T>().createCache(config);   
  73.     }   
  74.  
  75.     /**  
  76.      * Add a new node into the tree-node hierarchy  
  77.      *   
  78.      * @param fqn Full Qualified Name for the new node  
  79.      * @return  
  80.      */   
  81.     public Node<String, T> addNode(Fqn<String> fqn) {   
  82.         return cache.getRoot().addChild(fqn);   
  83.     }   
  84.  
  85.     /**  
  86.      * Remove a specified node from the tree-node hierarchy  
  87.      *   
  88.      * @param fqn Full Qualified Name for the specified node  
  89.      */   
  90.     public void removeNode(Fqn<String> fqn) {   
  91.         cache.removeNode(fqn);   
  92.     }   
  93.  
  94.     /**  
  95.      * Add node information to the specified node.  
  96.      *   
  97.      * @param fqn Full Qualified Name for the specified node  
  98.      * @param key The key of the node information  
  99.      * @param value The value of the node information  
  100.      */   
  101.     public void addNodeInfo(Fqn<String> fqn, String key, T value) {   
  102.         cache.put(fqn, key, value);   
  103.     }   
  104.  
  105.     /**  
  106.      * Batch add node information to the specified node.  
  107.      *   
  108.      * @param fqn Full Qualified Name for the specified node  
  109.      * @param infos Node informations map  
  110.      */   
  111.     public void addNodeInfos(Fqn<String> fqn, Map<String, T> infos) {   
  112.         cache.put(fqn, infos);   
  113.     }   
  114.  
  115.     /**  
  116.      * Get node information from the specified node.  
  117.      *   
  118.      * @param fqn Full Qualified Name for the specified node  
  119.      * @param key The key of the node information  
  120.      * @return  
  121.      */   
  122.     public T getNodeInfo(Fqn<String> fqn, String key) {   
  123.         return cache.get(fqn, key);   
  124.     }   
  125.  
  126.     /**  
  127.      * Remove node information from the specified node.  
  128.      *   
  129.      * @param fqn Full Qualified Name for the specified node  
  130.      * @param key The key of the node information  
  131.      */   
  132.     public void removeNodeInfo(Fqn<String> fqn, String key) {   
  133.         cache.remove(fqn, key);   
  134.     }   

下面是一個測試案例:

  1. package com.javaeye.terrencexu.jbosscache;   
  2.  
  3. import java.io.File;   
  4.  
  5. import org.jboss.cache.Fqn;   
  6.  
  7. public class Main {   
  8.  
  9.     public static void main(String[] args) {   
  10.         FileCache<String> fileCache = new FileCache<String>(new File("d:\\tmp"));   
  11.  
  12.         Fqn<String> jimmyFqn = Fqn.fromString("/com/manager/jimmy");   
  13.         Fqn<String> hansonFqn = Fqn.fromString("/com/developer/hanson");   
  14.  
  15.         fileCache.addNode(jimmyFqn);   
  16.         fileCache.addNode(hansonFqn);   
  17.  
  18.         fileCache.addNodeInfo(jimmyFqn, "en-name""Jimmy Zhang");   
  19.         fileCache.addNodeInfo(jimmyFqn, "zh-name""Zhang Ji");   
  20.         fileCache.addNodeInfo(hansonFqn, "en-name""Hanson Yang");   
  21.         fileCache.addNodeInfo(hansonFqn, "zh-name""Yang Kuo");   
  22.  
  23.         String enName = fileCache.getNodeInfo(hansonFqn, "en-name");   
  24.         System.out.println(enName);   
  25.     }   
  26.  

運行結(jié)果如下:

  1. - JBossCache MBeans were successfully registered to the platform mbean server.   
  2. - JBoss Cache version: JBossCache 'Malagueta' 3.2.5.GA   
  3. Hanson Yang 

生成的緩存文件目錄結(jié)構(gòu)如下:

  1. D:/tmp/com.fdb/manage.fdb/jimmy.fdb/data.dat 
  2. D:/tmp/com.fdb/developer.fdb/hanson.fdb/data.dat 

總結(jié)

JBoss Cache還有更多的用法,如果你的系統(tǒng)遇到數(shù)據(jù)庫瓶頸問題,可以考慮使用JBoss Cache來解決。

責任編輯:張偉 來源: 碼農(nóng)網(wǎng)
相關推薦

2010-03-23 14:41:13

JBossSOA

2012-03-20 14:23:48

JBoss紅帽

2009-12-03 13:51:51

JRubyJBossTorqueBox

2010-10-19 08:59:40

PHP緩存技術(shù)

2011-01-14 16:04:01

Linux集群系統(tǒng)

2016-02-23 13:16:08

網(wǎng)絡監(jiān)控網(wǎng)絡可用性監(jiān)控系統(tǒng)

2011-01-28 09:29:51

PHPWeb

2018-11-20 09:35:42

開源技術(shù) 數(shù)據(jù)

2012-08-22 15:25:43

Linux集群

2009-09-22 11:59:19

2013-03-28 09:35:31

企業(yè)級系統(tǒng)

2009-06-15 17:44:38

JBoss Cache

2009-03-02 09:22:39

OSGiJ2EEEclipse

2012-09-05 17:29:32

存儲系統(tǒng)華為

2011-05-19 10:57:47

架構(gòu)

2020-07-31 07:45:43

架構(gòu)系統(tǒng)企業(yè)級

2010-08-04 15:20:15

Flex企業(yè)級開發(fā)

2012-06-14 13:26:22

2010-04-22 14:19:21

LVS負載均衡集群

2012-02-20 10:28:03

JBossJava
點贊
收藏

51CTO技術(shù)棧公眾號