java 使用 jedis 连接 redis 并进行简单操作

1, 这里以 maven 项目为例

       <!-- Redis NoSQL 操作依赖 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

2, 会下载两个依赖包 

3, Redis 连接池帮助类

package redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUtil {

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Redis 服务器 IP</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> String address = "192.168.3.118"<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)"> Redis的端口号</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span> port = 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>
<span style="color: rgba(0, 0, 255, 1)">private</span> String password = "920619"<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)"> 连接 redis 等待时间</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span> timeOut = 10000<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)"> 可用连接实例的最大数目,默认值为8;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span> maxTotal = 1024<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)"> 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span> maxIdle = 200<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)"> 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span> maxWait = 10000<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)"> 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span> testOnBorrow = <span style="color: rgba(0, 0, 255, 1)">true</span><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>
<span style="color: rgba(0, 0, 255, 1)">private</span> JedisPool jedisPool = <span style="color: rgba(0, 0, 255, 1)">null</span><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>
<span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> RedisUtil() {
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        JedisPoolConfig config </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPoolConfig();
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMaxWaitMillis(maxWait);
        config.setTestOnBorrow(testOnBorrow);
        jedisPool </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPool(config, address, port, timeOut, password);
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(Exception e) {
        e.printStackTrace();
    }
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取 Jedis 实例</span>
<span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Jedis getJedis() {
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (jedisPool != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> jedisPool.getResource();
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;    
}</span><span style="color: rgba(0, 0, 0, 1)"> 

}

 4, 测试类

package test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import redis.RedisUtil;
import redis.clients.jedis.Jedis;

public class RedisTest {

</span><span style="color: rgba(0, 0, 255, 1)">private</span> RedisUtil redisUtil = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RedisUtil();

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 字符串操作</span>

@Test
public void testStr() {
Jedis jedis
= redisUtil.getJedis();
jedis.set(
"id", "15"); // 只能是字符串
String id = jedis.get("id");
System.out.println(id);
jedis.close();
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 操作 map</span>

@Test
public void testMap() {
Jedis jedis
= redisUtil.getJedis();
Map
<String, String> map = new HashMap<String, String>();
map.put(
"name", "xinxin");
map.put(
"age", "22");
map.put(
"qq", "123456");
jedis.hmset(
"user", map);
List
<String> rsmap = jedis.hmget("user", "name", "age", "qq");
System.out.println(rsmap);
jedis.hdel(
"user", "age");
Iterator
<String> iter = jedis.hkeys("user").iterator();
while (iter.hasNext()) {
String key
= iter.next();
System.out.println(key
+ ":" + jedis.hmget("user", key));
}
jedis.close();
}
  
// 操作 list
@Test
public void testList() {
Jedis jedis
= redisUtil.getJedis();
jedis.del(
"java framework");
System.out.println(jedis.lrange(
"java framework", 0, -1));
jedis.lpush(
"java framework", "spring");
jedis.lpush(
"java framework", "struts");
jedis.lpush(
"java framework", "hibernate");
System.out.println(jedis.lrange(
"java framework", 0, -1));
jedis.del(
"java framework");
jedis.rpush(
"java framework", "spring");
jedis.rpush(
"java framework", "struts");
jedis.rpush(
"java framework", "hibernate");
System.out.println(jedis.lrange(
"java framework", 0, -1));
jedis.close();
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 操作 set</span>

@Test
public void testSet() {
Jedis jedis
= redisUtil.getJedis();
jedis.sadd(
"user1", "liuling");
jedis.sadd(
"user1", "xinxin");
jedis.sadd(
"user1", "ling");
jedis.sadd(
"user1", "zhangxinxin");
jedis.sadd(
"user1", "who");
jedis.srem(
"user1", "who"); // 移除 noname
System.out.println(jedis.smembers("user1"));// 获取所有加入的 value
System.out.println(jedis.sismember("user1", "who"));// 判断 who
System.out.println(jedis.srandmember("user1")); // 是否是 user 集合的元素
System.out.println(jedis.scard("user1"));// 返回集合的元素个数
jedis.close();
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> jedis 排序</span>

@Test
public void testOrder() {
Jedis jedis
= redisUtil.getJedis();
jedis.del(
"a");
jedis.rpush(
"a", "1");
jedis.lpush(
"a", "6");
jedis.lpush(
"a", "3");
jedis.lpush(
"a", "9");
System.out.println(jedis.lrange(
"a", 0, -1));
System.out.println(jedis.sort(
"a"));
System.out.println(jedis.lrange(
"a", 0, -1));
jedis.close();
}

}