关闭 x
IT技术网
    技 采 号
    ITJS.cn - 技术改变世界
    • 实用工具
    • 菜鸟教程
    IT采购网 中国存储网 科技号 CIO智库

    IT技术网

    IT采购网
    • 首页
    • 行业资讯
    • 系统运维
      • 操作系统
        • Windows
        • Linux
        • Mac OS
      • 数据库
        • MySQL
        • Oracle
        • SQL Server
      • 网站建设
    • 人工智能
    • 半导体芯片
    • 笔记本电脑
    • 智能手机
    • 智能汽车
    • 编程语言
    IT技术网 - ITJS.CN
    首页 » JavaScript »JBoss Cache:企业级Java事务缓存集群系统

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

    2014-11-08 00:00:00 出处:ITJS
    分享

    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来解决。

    上一篇返回首页 下一篇

    声明: 此文观点不代表本站立场;转载务必保留本文链接;版权疑问请联系我们。

    别人在看

    hiberfil.sys文件可以删除吗?了解该文件并手把手教你删除C盘的hiberfil.sys文件

    Window 10和 Windows 11哪个好?答案是:看你自己的需求

    盗版软件成公司里的“隐形炸弹”?老板们的“法务噩梦” 有救了!

    帝国CMS7.5编辑器上传图片取消宽高的三种方法

    帝国cms如何自动生成缩略图的实现方法

    Windows 12即将到来,将彻底改变人机交互

    帝国CMS 7.5忘记登陆账号密码怎么办?可以phpmyadmin中重置管理员密码

    帝国CMS 7.5 后台编辑器换行,修改回车键br换行为p标签

    Windows 11 版本与 Windows 10比较,新功能一览

    Windows 11激活产品密钥收集及专业版激活方法

    IT头条

    智能手机市场风云:iPhone领跑销量榜,华为缺席引争议

    15:43

    大数据算法和“老师傅”经验叠加 智慧化收储粮食尽显“科技范”

    15:17

    严重缩水!NVIDIA将推中国特供RTX 5090 DD:只剩24GB显存

    00:17

    无线路由大厂 TP-Link突然大裁员:补偿N+3

    02:39

    Meta 千万美金招募AI高级人才

    00:22

    技术热点

    微软已修复windows 7/windows 8.1媒体中心严重漏洞 用户可下载安

    卸载MySQL数据库,用rpm如何实现

    windows 7中使用网上银行或支付宝支付时总是打不开支付页面

    一致性哈希算法原理设计

    MySQL数字类型中的三种常用种类

    如何解决SQL Server中传入select语句in范围参数

      友情链接:
    • IT采购网
    • 科技号
    • 中国存储网
    • 存储网
    • 半导体联盟
    • 医疗软件网
    • 软件中国
    • ITbrand
    • 采购中国
    • CIO智库
    • 考研题库
    • 法务网
    • AI工具网
    • 电子芯片网
    • 安全库
    • 隐私保护
    • 版权申明
    • 联系我们
    IT技术网 版权所有 © 2020-2025,京ICP备14047533号-20,Power by OK设计网

    在上方输入关键词后,回车键 开始搜索。Esc键 取消该搜索窗口。