JBoss Cache:企业级Java事务缓存集群系统

开发 后端
JBoss Cache是一款基于Java的事务处理缓存系统,它的目标是构建一个以Java框架为基础的集群解决方案,可以是服务器应用,也可以是Java SE应用。

JBoss Cache是一款基于Java的事务处理缓存系统,它的目标是构建一个以Java框架为基础的集群解决方案,可以是服务器应用,也可以是Java SE应用。

集群高可用性

JBoss Cache将会自动复制缓存数据,并且在集群中的服务器之间进行缓存数据的同步,这样可以保证任何一台服务器重启了都不会影响缓存的可用性。

集群缓存可避免系统瓶颈

JBoss Cache顾名思义是利用缓存来提高系统扩展性的,当我们的WEB系统遇到大量的数据库读写时,系统的瓶颈将会出现在数据库端,JBoss Cache正好可以解决数据库的频繁读取问题,解决这个瓶颈。

另外,由于JBoss Cache的缓存是在集群中的每一个服务器间同步的,因此也不会因为一台缓存服务器遇到性能问题而影响整个系统。

JBoss Cache的standalone用法

首先是初始化TreeCache

  1. TreeCache tree = new TreeCache(); 

然后是读进配置文件

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

然后开始服务

  1. Tree.startService(); 

因为Tree的结构是用NODE来Access的,TreeCache这里就很简单的用:

/level1/level2/node1 来表示两级Tree下面的Node1。

现在我们添加几个要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示例

首先创建一个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.  

运行结果如下:

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

生成的缓存文件目录结构如下:

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

总结

JBoss Cache还有更多的用法,如果你的系统遇到数据库瓶颈问题,可以考虑使用JBoss Cache来解决。

责任编辑:张伟 来源: 码农网
相关推荐

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缓存技术

2011-01-14 16:04:01

Linux集群系统

2016-02-23 13:16:08

网络监控网络可用性监控系统

2011-01-28 09:29:51

PHPWeb

2018-11-20 09:35:42

开源技术 数据

2009-03-02 09:22:39

OSGiJ2EEEclipse

2012-08-22 15:25:43

Linux集群

2009-09-22 11:59:19

2013-03-28 09:35:31

企业级系统

2011-05-19 10:57:47

架构

2012-09-05 17:29:32

存储系统华为

2020-07-31 07:45:43

架构系统企业级

2009-06-15 17:44:38

JBoss Cache

2012-03-14 09:22:24

MagnoliaJava

2010-08-04 15:20:15

Flex企业级开发

2012-06-14 13:26:22

2012-02-20 10:28:03

JBossJava
点赞
收藏

51CTO技术栈公众号