Redis中推荐的Java使用jar为jedis。
相关依赖:
<!-- redis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
两个jar包都需要,缺少common-pool2会导致连接池缺失方法报错。
redis的获取与使用:
public Jedis jedis;
@org.junit.Before
public void getJedis(){
jedis = new Jedis("localhost",6379);
System.out.println("链接成功!");
}
@Test
public void testStr(){
jedis.set("sleep","huloulou");
System.out.println(jedis.get("sleep"));
jedis.append("sleep"," wo X!");
System.out.println(jedis.get("sleep"));
// jedis.del("sleep");
jedis.mset("num","1","age","20");
jedis.incr("num");
jedis.incrBy("num",4);
System.out.println(jedis.mget("num","age","sleep"));
}
Redis连接池的获取与使用:
public class RedisPool {
private static String ADDR = "localhost";
private static Integer PORT = 6379;
//可用连接实例的最大数目,默认为8;
//如果赋值为-1,则表示不限制,如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)
private static Integer MAX_TOTAL = 1024;
//控制一个pool最多有多少个状态为idle(空闲)的jedis实例,默认值是8
private static Integer MAX_IDLE = 200;
//等待可用连接的最大时间,单位是毫秒,默认值为-1,表示永不超时。
//如果超过等待时间,则直接抛出JedisConnectionException
private static Integer MAX_WAIT_MILLIS = 10000;
private static Integer TIMEOUT = 10000;
//在borrow(用)一个jedis实例时,是否提前进行validate(验证)操作;
//如果为true,则得到的jedis实例均是可用的
private static Boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_TOTAL);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT_MILLIS);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取jedis
*
* @return
*/
public static Jedis getJedis() {
Jedis jedis = null;
try {
if (jedisPool != null)
jedis = jedisPool.getResource();
} catch (Exception e) {
e.printStackTrace();
}
return jedis;
}
/**
* 释放jedis资源
* @param jedis
*/
public static void returnResource(Jedis jedis) {
if (jedis != null)
jedis.close();
}
}
连接池测试:
public class RedisTest {
public static void main(String[] args) {
Jedis jedis =
RedisPool.getJedis();
jedis.select(2);
System.out.println("链接成功!");
}
}
网友评论