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+Spring注入配置参数,无需读取固定路径下的j2cache.properties配置文件相关推荐
- ClassLoad类加载器读取ClassPath路径下的配置文件(一)
ClassLoad类加载器读取ClassPath路径下的配置文件(一) 一.项目介绍 1.使用 com.test.test包下的ReadclassLoad 类去读取另一个包 com.test.util ...
- 在MATLAB中读取同一路径下多个txt或mat文件
1.matlab中给我们提供了dir这个函数,十分方便.这里是读取所有txt文件,所以是dir('父目录路径\*.txt'),读取其它格式文件一样,如读取该路径下的所有png图像文件,则为dir('父 ...
- matlab文件目录表示,Matlab - 文件目录路径操作_读取不同路径下的相同文件名表格...
Matlab - 文件目录路径操作_读取不同路径下的相同文件名表格 tr1 str1 for max imp mda eva 目录 xls clear;clc; clo ...
- 安卓加载asset中的json文件_Android中读取asset路径下本地json文件
最近自己写个小demo,用到了assets文件里面的内容.猛的没想起来怎么使用.就记录下来 移动端开发中,和后台的交互方式不外乎json和xml,由于json的轻量级以及易用性,所以现在的交互协议基本 ...
- spark读取hdfs路径下的数据_到底什么是数据湖
随着移动互联网,物联网技术的发展,数据的应用逐渐从 BI 报表可视化往机器学习.预测分析等方向发展,即 BI 到 AI 的转变. 数据的使用者也从传统的业务分析人员转为数据科学家,算法工程师. 此外对 ...
- java读取src路径下的txt文件_Java程序使用Maven后无法运行?
问:老师,同样的代码,为何我将Java工程改造成Maven后,始终无法运行? 实现的功能:加载abc.txt文件,代码是写在了Demo类中. 开发工具是Idea. 1.以下是普通Java工程的目录结构 ...
- java修改配置文件参数_在java类中获取在.properties配置文件中设置的参数
如何获取.properties配置文件中的参数,我在网上查了半天没弄明白,后来在以前的项目中找到了,就写下来,避免遗忘. 1.配置文件:message_product.properties total ...
- bean validation校验方法参数_项目启动时首先校验Spring Boot配置参数
1. 概述 在项目实际开发过程中,为了更好的复用,我们参考Spring Boot Starters,封装了许多企业内部中间件的starter.这些中间件的接入都需要申请并在项目中配置一些特定的参数.我 ...
- Spring Cloud 配置中心客户端读取配置
微服务连接配置中心来实现外部配置的读取. 引入依赖 <dependencies><dependency><groupId>org.springframework.c ...
- c打开指定路径文件_C++ 读取指定路径下所有的文件 (C++ get the list of files in a directory)...
电脑配置:window10, 64位操作系统,基于x64的处理器,Microsoft Visual Studio Community 2019 Version 16.4.5 实现方法:使用 boost ...
最新文章
- 推荐一款 Java 对象映射神器
- Opencv实战 | 用摄像头自动化跟踪特定颜色物体
- Max retries exceeded with URL in requests
- LTE voice centric和data centric
- flask和ajax,Flask flash和url_用于AJAX
- java高并发(三)并发编程的基础
- ZOJ 1161 Gone Fishing
- pandas的apply函数解析
- 10. Browser 对象 - Location 对象(2)
- IDEA安装mysql程序包,程序包的下载!以及程序包配置到项目详解(更适合英语小白)
- 软件设计的启发规则---层次图,HIPO图和结构图----画数据流图
- 运行spark及hadoop版本不一致解决方法
- ir2104s的自举电容_有关IR2104的自举电容和NMOS选择问题教程.docx
- nod32系统更新服务器,Nod32官方升级服务器列表
- 团队项目绩效考核方案表
- React.js介绍
- 魔方世界连服务器未响应,为什么魔方世界显示无法连接服务器 | 手游网游页游攻略大全...
- iPhone备忘录删了怎么恢复?恢复备忘录的两大方法!
- CAD中样条曲线Spline怎样才能分解成多段弧arc?
- [Matlab]二维隐函数绘图
热门文章
- Qcom ramdump 解析
- Visual Studio安装及无法打开包括文件: “crtdbg.h”问题解决
- visual studio 2015无法打开包括文件
- python怎么用到微信,用Python完转微信
- CAD转换高清图片该如何进行设置
- nc语法和nc木马远程控制主机
- opnet之变量类型
- JAVA上传smartupload_java使用smartupload组件实现文件上传的方法
- 翻译: 中国北斗卫星导航系统 全球导航卫星系统 (GNSS)
- 编译原理 实验1《词法分析程序设计与实现》