jboss cache:企业级java事务缓存集群系统-亚博电竞手机版
本文由码农网 – 小峰原创,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划!
jboss cache是一款基于java的事务处理缓存系统,它的目标是构建一个以java框架为基础的集群亚博vip888的解决方案,可以是服务器应用,也可以是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; /** ** 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. *
** 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. *
* * @author terrencex * * @param*/ public class filecache { /** * the jboss cache, used to cache frequently accessed java objects. */ private cache 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 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 ().createcache(config); } /** * add a new node into the tree-node hierarchy * * @param fqn full qualified name for the new node * @return */ public node addnode(fqn 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 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 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 fqn, map 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 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 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) { filecachefilecache = new filecache (new file("d:\\tmp")); fqn jimmyfqn = fqn.fromstring("/com/manager/jimmy"); fqn 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来解决。
软件亚博vip888首页 软件文档 软件下载
本文链接:http://www.codeceo.com/article/jboss-cache-java.html
本文作者:码农网 – 小峰
[ 原创作品,转载必须在正文中标注并保留原文链接和作者等信息。]