Java与redis交互、Jedis连接池JedisPool
Java 与 redis 交互比较常用的是 Jedis。
先导入 jar 包:
commons-pool2-2.3.jar
jedis-2.7.0.jar
基本使用:
public class RedisTest1 { public static void main(String[] args) { Jedis jedis = new Jedis("localhost",6379); jedis.set("username","chichung"); jedis.close();} }
Jedis 对象基本和 redis 的命令一模一样,这里不啰嗦了。
-
JedisPool 连接池
类似于 mysql 连接池,jedis 也有连接池。
基本使用如下:
public class RedisTest2 { public static void main(String[] args) { // 比较特殊的是,redis 连接池的配置首先要创建一个连接池配置对象 JedisPoolConfig config = new JedisPoolConfig(); // 当然这里还有设置属性的代码</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建Jedis连接池对象</span> JedisPool jedisPool = <span style="color: rgba(0, 0, 255, 1)">new</span> JedisPool(config,"localhost",6379<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取连接</span> Jedis jedis =<span style="color: rgba(0, 0, 0, 1)"> jedisPool.getResource(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 使用 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 关闭,归还连接到连接池</span>
jedis.close();
}
}
一般可以抽取出来作为一个工具类使用:
例如有一个配置文件 jedis.properties。
里面的内容如下:
host=127.0.0.1
port=6379
maxTotal=50
maxIdle=10
工具类代码如下:
package com.chichung.redis;import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;public class JedisPoolUtils {
private static JedisPool jedisPool;</span><span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> { InputStream is </span>= JedisPoolUtils.<span style="color: rgba(0, 0, 255, 1)">class</span>.getClassLoader().getResourceAsStream("jedis.properties"<span style="color: rgba(0, 0, 0, 1)">); Properties properties </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Properties(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { properties.load(is); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { e.printStackTrace(); } JedisPoolConfig config </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPoolConfig(); config.setMaxTotal(Integer.parseInt(properties.getProperty(</span>"maxTotal"<span style="color: rgba(0, 0, 0, 1)">))); config.setMaxIdle(Integer.parseInt(properties.getProperty(</span>"maxIdle"<span style="color: rgba(0, 0, 0, 1)">))); jedisPool </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPool(config, properties.getProperty(</span>"host"<span style="color: rgba(0, 0, 0, 1)">), Integer.parseInt(properties.getProperty(</span>"port"<span style="color: rgba(0, 0, 0, 1)">))); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Jedis getJedis(){ </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> jedisPool.getResource(); }
}