(Redis介绍:略)html
Redis-win-x64位程序的下载地址(缺分,分多的能够给我贡献点):java
http://download.csdn.net/download/qq_33601179/10165429linux
linux下的安装百度一大堆,也不贴出来了,毕竟我没用过,随便贴一篇也不太好。redis
很是简单,只须要cmd命令行工具运行代码,吧服务程序跑起来就OK,代码为:(亲测有效)windows
redis-server.exe redis.windows.conf性能优化
具体的安装步骤,请看博客:函数
http://blog.csdn.net/jinwufeiyang/article/details/52156817工具
这个我也不废话,直接看下面这个博客:(亲测有效)性能
http://www.runoob.com/redis/redis-java.html优化
Redis链接池,Redis自己就自带链接池。(亲测有效)
推荐将所须要的配置参数写到配置文件中,下面附上我封装的一个Redis链接池的类,由三个部分组成:类BaseRedis(使用Redis链接池:JedisPool)、类PropertiesUtils(用于读取配置文件)、redis.properties(参数配置文件)
import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * Created by Admin on 2017/12/19. */ public class BaseRedis { private static JedisPool pool;//redis链接池 /** * 从链接池中获取对象 * * @return */ public static Jedis getJedis() { /** * 断定链接池是否已经初始化 */ if (pool == null) { PropertiesUtils propertiesUtils=new PropertiesUtils("redis.properties"); /** * 从配置文件中读取配置参数 */ Integer maxTotal=propertiesUtils.getValueForInt("maxTotal"); if(maxTotal==null){ maxTotal=100; } Integer maxWaitMillis=propertiesUtils.getValueForInt("maxWaitMillis"); if(maxWaitMillis==null){ maxWaitMillis=1000; } Integer maxIdle=propertiesUtils.getValueForInt("maxIdle"); if(maxIdle==null){ maxIdle=10; } Integer port=propertiesUtils.getValueForInt("port"); if(port==null){ port=6379; } String redisUrl=propertiesUtils.getValue("redisUrl"); if(redisUrl==null){ redisUrl="localhost"; } // 创建链接池配置参数 JedisPoolConfig config = new JedisPoolConfig(); // 设置最大链接数 config.setMaxTotal(maxTotal); // 设置最大阻塞时间,记住是毫秒数milliseconds config.setMaxWaitMillis(maxWaitMillis); // 设置空间链接 config.setMaxIdle(maxIdle); // 建立链接池 pool = new JedisPool(config, redisUrl, port); } return pool.getResource(); } }
import java.io.InputStream; import java.util.*; /** * 读取配置文件中的属性 */ public class PropertiesUtils { private String filePath; /** * 构造函数须要传入待读取的配置文件的名称 * * @param propertiesFilePath */ public PropertiesUtils(String propertiesFilePath) { // ../../这个是根据当前类所在的目录相对定位到配置文件的路径,具体按需修改 this.filePath = "../../../../" + propertiesFilePath; } /** * 读取指定的配置参数 * * @param key * @return */ public String getValue(String key) { String str = ""; try { Properties pro = new Properties(); InputStream ins = this.getClass().getResourceAsStream(filePath); pro.load(ins); str = pro.getProperty(key); ins.close(); } catch (Exception e) { e.printStackTrace(); } return str; } /** * 读取指定的配置参数 * * @param key * @return */ public Integer getValueForInt(String key) { String str = getValue(key); try { return Integer.parseInt(str); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 读取全部的配置参数 * * @return */ public Map<String, String> getAllValue() { //读取属性文件a.properties Map<String, String> map = new HashMap<String, String>(); try { Properties prop = new Properties(); InputStream ins = this.getClass().getResourceAsStream(filePath); prop.load(ins); ///加载属性列表 Iterator<String> it = prop.stringPropertyNames().iterator(); while (it.hasNext()) { String key = it.next(); String value = prop.getProperty(key); map.put(key, value); } ins.close(); } catch (Exception e) { e.printStackTrace(); } return map; } }
#redis配置文件 #URL redisUrl=localhost #端口号 port=6379 #最大链接数 maxTotal=100 #超时时间,单位毫秒 maxWaitMillis=1000 #最大xxx maxIdle=10