redis是著名的缓存数据库,功能非常强大,使用也比较简单,我设计的RedisFile项目是用来模拟上传文件数据到缓存中。项目放在oschina中,下载地址是:https://git.oschina.net/pandason125/RedisFile.git 可以去下载,接下来的几篇博客将讲解里面的代码:所需的jar包有:commons-pool2-2.2.jar,dom4j-1.6.1.jar,jedis-2.4.1.jar。我就按包顺序来讲解代码吧,先从com.ylzinfo.redis.config包讲起,里面有Configure.java和SysConstants.java两个类,是读取配置文件,和redis参数的,分别读取cache-online.properties和system.properties参数:
package com.ylzinfo.redis.config; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; import org.dom4j.DocumentException; import org.dom4j.Node; import org.dom4j.io.SAXReader; /** * @author asartear * */ public class Configure { public Properties systemConfig; public Properties cacheConfig; public Configure() { systemConfig = new Properties(); cacheConfig = new Properties(); try { loadConfigs(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Properties getSystemConfig() { return systemConfig; } public Properties getCacheConfig() { return cacheConfig; } /** * 加载缓存配置 * * @throws Exception */ public void loadConfigs() throws Exception { loadSystemConfig(); loadCacheConfig(); } /* * 获取system.properties */ public void loadSystemConfig() { propertyConfigLoader_sys("/system.properties", systemConfig); } public void propertyConfigLoader_sys(String configFilePath, Properties config) { InputStream configFileStream = this.getClass().getResourceAsStream(configFilePath); try { config.load(configFileStream); } catch (IOException e) { } } /* * 获取cache-online.properties */ public void loadCacheConfig() { String path = systemConfig.getProperty("com.ylzinfo.config.file.cache"); propertyConfigLoader(path, cacheConfig); } public void propertyConfigLoader(String configFilePath, Properties config) { if (configFilePath.length() == 0) { return; } InputStream configFileStream = this.getClass().getResourceAsStream(configFilePath); //File file = new File(configFilePath); //if (file.exists()) { try { // InputStream configFileStream = new FileInputStream(file); config.load(configFileStream); } catch (IOException e) { // } } } }上段代码就是加载配置文件的代码
package com.ylzinfo.redis.config; public class SysConstants { // --------------- redis pool --------------- // 从数据库连接池中取得连接时,对其的有效性进行检查 public static final boolean TEST_ON_BORROW = true; // 最大连接数 public static final int MAX_ACTIVE = 36; // 最大闲置的连接数 public static final int MAX_IDLE = 20; // 最小..... public static final int MIN_IDLE = 5; // 请求最长等待时间/毫秒 public static final int MAX_WAIT = 1000; // 闲置时测试 public static final boolean TEST_WHILE_IDLE = true; }上面那段代码是一些redis连接池参数
cache-online.properties内容:
instances=upload,unread,group_counter,business #\u4E0A\u4F20\u6587\u4EF6\u7684redis upload_host=192.168.44.56 upload_port=6379 upload_db=0 #\u672A\u8BFB\u6D88\u606F\u8BA1\u6570\u5668\u7684redis unread_host=192.168.44.56 unread_port=6379 #unread_port=11365 unread_db=1 #\u7FA4\u6D88\u606F\u8BA1\u6570\uFF0C\u672A\u8BFB\u6D88\u606F\u548C\u603B\u8BA1\u6570\u90FD\u5728\u8FD9\u91CC group_counter_host=192.168.44.56 #group_counter_port=11365 group_counter_port=6379 group_counter_db=2 #IM \u4E1A\u52A1 cache business_host=192.168.44.56 #business_port=11365 business_port=6379 business_db=3system.properties内容:
#com.ylzinfo.config.file.db=${config.file.db} com.ylzinfo.config.file.cache=/cache-online.properties
原文地址:http://blog.csdn.net/asartear/article/details/42107909