JBoss Cache:企業級Java事務緩存集群系統
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://原網站已經失效/article/jboss-cache-java.html
本文作者:碼農網 – 小峰
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!