JBoss Cache:企業級Java事務快取叢集系統

2014-11-08    分類:開源軟體、快取系統、程式設計開發、首頁精華1人評論發表於2014-11-08

本文由碼農網 – 小峰原創,轉載請看清文末的轉載要求,歡迎參與我們的付費投稿計劃

JBoss Cache是一款基於Java的事務處理快取系統,它的目標是構建一個以Java框架為基礎的叢集解決方案,可以是伺服器應用,也可以是Java SE應用。

叢集高可用性

JBoss Cache將會自動複製快取資料,並且在叢集中的伺服器之間進行快取資料的同步,這樣可以保證任何一臺伺服器重啟了都不會影響快取的可用性。

叢集快取可避免系統瓶頸

JBoss Cache顧名思義是利用快取來提高系統擴充套件性的,當我們的WEB系統遇到大量的資料庫讀寫時,系統的瓶頸將會出現在資料庫端,JBoss Cache正好可以解決資料庫的頻繁讀取問題,解決這個瓶頸。

另外,由於JBoss Cache的快取是在叢集中的每一個伺服器間同步的,因此也不會因為一臺快取伺服器遇到效能問題而影響整個系統。

JBoss Cache的standalone用法

首先是初始化TreeCache

TreeCache tree = new TreeCache();

然後是讀進配置檔案

PropertyConfigurator config = new PropertyConfigurator();
config.configure("配置檔案.xml");

然後開始服務

Tree.startService();

因為Tree的結構是用NODE來Access的,TreeCache這裡就很簡單的用:

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

現在我們新增幾個要Cache的物件。

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

大家可以看到,TreeCache裡面可以儲存任何種類的物件,包括所有複雜物件。

讀取物件就很方便了,

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

value1就讀出來了。

同理:

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

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

最後停止服務:

Tree.stopService();

JBoss Cache的FileCacheLoader示例

首先建立一個FileCache類封裝JBoss Cache的相關操作,如下:

package com.javaeye.terrencexu.jbosscache;  

import java.io.File;  
import java.util.Map;  

import org.jboss.cache.Cache;  
import org.jboss.cache.DefaultCacheFactory;  
import org.jboss.cache.Fqn;  
import org.jboss.cache.Node;  
import org.jboss.cache.config.CacheLoaderConfig;  
import org.jboss.cache.config.Configuration;  
import org.jboss.cache.loader.FileCacheLoader;  
import org.jboss.cache.loader.FileCacheLoaderConfig;  

/** 
 * <p> 
 * This is demo to illustrate how to use the JBoss Cache to cache your 
 * frequently accessed Java objects in order to dramatically improve 
 * the performance of your applications. This makes it easy to remove 
 * data access bottlenecks, such as connecting to a database. 
 * </p> 
 * <p> 
 * As a rule of thumb, it is recommended that the FileCacheLoader not  
 * be used in a highly concurrent, transactional or stressful environment, 
 * ant its use is restricted to testing. 
 * </p> 
 *  
 * @author TerrenceX 
 * 
 * @param <T> 
 */  
public class FileCache<T> {  

    /** 
     * The JBoss Cache, used to cache frequently accessed Java objects. 
     */  
    private Cache<String, T> cache;  

    /** 
     * @constructor 
     * @param fsCacheLoaderLocation The file system location to store the cache 
     */  
    public FileCache(File fsCacheLoaderLocation) {  
        cache = initCache(fsCacheLoaderLocation);  
    }  

    /** 
     * Create a Cache and whose cache loader type is File Cache Loader 
     *  
     * @param fsCacheLoaderLocation The file position used to store the cache. 
     *  
     * @return Cache 
     */  
    public Cache<String, T> initCache(File fsCacheLoaderLocation) {  
        // initiate a FileCacheLoader instance  
        FileCacheLoader fsCacheLoader = new FileCacheLoader();  

        // prepare the file cache loader configuration file for File Cache Loader  
        FileCacheLoaderConfig fsCacheLoaderConfig = new FileCacheLoaderConfig();  
        fsCacheLoaderConfig.setLocation(fsCacheLoaderLocation.toString());  
        fsCacheLoaderConfig.setCacheLoader(fsCacheLoader);  

        // set configuration to File Cache Loader  
        fsCacheLoader.setConfig(fsCacheLoaderConfig);  

        // prepare the configuration for Cache  
        Configuration config = new Configuration();  
        config.setCacheLoaderConfig(new CacheLoaderConfig());  
        config.getCacheLoaderConfig().addIndividualCacheLoaderConfig(fsCacheLoaderConfig);  

        // create a Cache through the default cache factory  
        return new DefaultCacheFactory<String, T>().createCache(config);  
    }  

    /** 
     * Add a new node into the tree-node hierarchy 
     *  
     * @param fqn Full Qualified Name for the new node 
     * @return 
     */  
    public Node<String, T> addNode(Fqn<String> fqn) {  
        return cache.getRoot().addChild(fqn);  
    }  

    /** 
     * Remove a specified node from the tree-node hierarchy 
     *  
     * @param fqn Full Qualified Name for the specified node 
     */  
    public void removeNode(Fqn<String> fqn) {  
        cache.removeNode(fqn);  
    }  

    /** 
     * Add node information to the specified node. 
     *  
     * @param fqn Full Qualified Name for the specified node 
     * @param key The key of the node information 
     * @param value The value of the node information 
     */  
    public void addNodeInfo(Fqn<String> fqn, String key, T value) {  
        cache.put(fqn, key, value);  
    }  

    /** 
     * Batch add node information to the specified node. 
     *  
     * @param fqn Full Qualified Name for the specified node 
     * @param infos Node informations map 
     */  
    public void addNodeInfos(Fqn<String> fqn, Map<String, T> infos) {  
        cache.put(fqn, infos);  
    }  

    /** 
     * Get node information from the specified node. 
     *  
     * @param fqn Full Qualified Name for the specified node 
     * @param key The key of the node information 
     * @return 
     */  
    public T getNodeInfo(Fqn<String> fqn, String key) {  
        return cache.get(fqn, key);  
    }  

    /** 
     * Remove node information from the specified node. 
     *  
     * @param fqn Full Qualified Name for the specified node 
     * @param key The key of the node information 
     */  
    public void removeNodeInfo(Fqn<String> fqn, String key) {  
        cache.remove(fqn, key);  
    }  
}

下面是一個測試案例:

package com.javaeye.terrencexu.jbosscache;  

import java.io.File;  

import org.jboss.cache.Fqn;  

public class Main {  

    public static void main(String[] args) {  
        FileCache<String> fileCache = new FileCache<String>(new File("d:\\tmp"));  

        Fqn<String> jimmyFqn = Fqn.fromString("/com/manager/jimmy");  
        Fqn<String> hansonFqn = Fqn.fromString("/com/developer/hanson");  

        fileCache.addNode(jimmyFqn);  
        fileCache.addNode(hansonFqn);  

        fileCache.addNodeInfo(jimmyFqn, "en-name", "Jimmy Zhang");  
        fileCache.addNodeInfo(jimmyFqn, "zh-name", "Zhang Ji");  
        fileCache.addNodeInfo(hansonFqn, "en-name", "Hanson Yang");  
        fileCache.addNodeInfo(hansonFqn, "zh-name", "Yang Kuo");  

        String enName = fileCache.getNodeInfo(hansonFqn, "en-name");  
        System.out.println(enName);  
    }  

}

執行結果如下:

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

生成的快取檔案目錄結構如下:

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

總結

JBoss Cache還有更多的用法,如果你的系統遇到資料庫瓶頸問題,可以考慮使用JBoss Cache來解決。

本文連結:http://www.codeceo.com/article/jboss-cache-java.html
本文作者:碼農網 – 小峰
原創作品,轉載必須在正文中標註並保留原文連結和作者等資訊。]

相關文章