J2Cache+Spring注入配置参数,无需读取固定路径下的j2cache.properties配置文件

J2Cache最初的版本是在源码中写死的读取配置文件路径,这个在使用上就非常不灵活,无论是用配置中心管理还是其他都不好用,而2.X之后的版本增加了一项功能:动态构建J2Cache实例

J2CacheConfig config = new J2CacheConfig();
//填充 config 变量所需的配置信息
J2CacheBuilder builder = J2CacheBuilder.init(config);
CacheChannel channel = builder.getChannel();
//进行缓存的操作
channel.close();

我的项目中一级缓存用了ehcache,二级缓存用redis,配置如下:

             

            J2CacheConfig config = new J2CacheConfig();
            config.setBroadcast("redis");
            config.setL1CacheName("ehcache");
            config.setL2CacheName("redis");
            config.setSerialization("fst");
            //基本配置
            Properties redisProperties = new Properties();
            redisProperties.setProperty("channel", "life_j2cache");
            redisProperties.setProperty("hosts", "127.0.0.1:6379");
            redisProperties.setProperty("timeout", "10000");
            redisProperties.setProperty("password", "123456");
            redisProperties.setProperty("database", "1");
            
            redisProperties.setProperty("maxTotal", "100");
            redisProperties.setProperty("maxIdle", "10");
            redisProperties.setProperty("maxWaitMillis", "5000");
            redisProperties.setProperty("minEvictableIdleTimeMillis", "60000");
            redisProperties.setProperty("minIdle", "1");
            redisProperties.setProperty("numTestsPerEvictionRun", "10");
            redisProperties.setProperty("lifo", "false");
            redisProperties.setProperty("softMinEvictableIdleTimeMillis", "10");
            redisProperties.setProperty("testOnBorrow", "true");
            redisProperties.setProperty("testOnReturn", "false");
            redisProperties.setProperty("testWhileIdle", "true");
            redisProperties.setProperty("timeBetweenEvictionRunsMillis", "300000");
            redisProperties.setProperty("blockWhenExhausted", "false");
            redisProperties.setProperty("jmxEnabled", "false");
            redisProperties.setProperty("usePool", "true");
            config.setBroadcastProperties(redisProperties);
            /**
         * 一级缓存配置
         * */
            Properties l1CacheProperties = new Properties();
            l1CacheProperties.setProperty("name", "self_name_j2cache");
            l1CacheProperties.setProperty("configXml", "/ehcache.xml");
            config.setL1CacheProperties(l1CacheProperties);
         /**
         * 二级缓存配置(基础缓存配置和二级缓存配置有许多参数是通用的,所有我使用了同一个对象去设置参数)
         * */
            redisProperties.setProperty("mode", "single");
            redisProperties.setProperty("storage", "hash");
            redisProperties.setProperty("cluster_name", "self_cluster_name");
            redisProperties.setProperty("namespace", "abcdefg");
            redisProperties.setProperty("channel_name", "self_channel");
            config.setL2CacheProperties(redisProperties);
            
            //填充 config 变量所需的配置信息
            J2CacheBuilder builder = J2CacheBuilder.init(config);
            CacheChannel cache = builder.getChannel();

上面配置中的参数可以用spring注入的参数来代替,这样就可以通过配置中心来管理j2cache的配置文件了。

J2Cache是OSChina目前正在使用的两级缓存框架(要求至少 Java 8)。第一级缓存使用内存(同时支持 Ehcache 2.x、Ehcache 3.x 和 Caffeine),第二级缓存使用 Redis(推荐)/Memcached。由于大量的缓存读取会导致L2的网络成为整个系统的瓶颈,因此L1的目标是降低对L2读取次数。该缓存框架主要用于集群环境中。单机也可使用,用于避免应用重启导致的缓存冷启动后对后端业务的冲击。 数据读取读取顺序 -> L1 -> L2 -> DB 数据更新 1、从数据库中读取最新数据,依次更新L1 -> L2 ,发送广播清除某个缓存信息 2、接收到广播(手工清除缓存 & 一级缓存自动失效),从L1中清除指定的缓存信息 J2Cache配置配置文件位于core/resources目录下,包含三个文件: j2cache.properties J2Cache核心配置文件,可配置两级的缓存,Redis服务器、连接池以及缓存广播的方式 caffeine.properties如果一级缓存选用Caffeine ,那么该文件用来配置缓存信息 ehcache.xml Ehcache配置文件配置说明请参考Ehcache文档 ehcache3.xml Ehcache3的配置文件配置说明请参考Ehcache文档 network.xml JGroups网络配置,如果使用JGroups组播的话需要这个文件,一般无需修改 实际使用过程需要将所需的配置文件复制到应用类路径中,如WEB-INF/classes目录。 J2Cache运行时所需jar包请查看core/pom.xml 测试方法: 1、安装Redis 2、git clone https://gitee.com/ld/J2Cache 3、修改core/resource/j2cache.properties配置使用已安装的Redis服务器 4、在命令行中执行mvn package -DskipTests=true进行项目编译 5、打开多个命令行窗口,同时运行runtest.sh 6、在 > 提示符后输入help查看命令,并进行测试
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值