1.增加依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
创建Util类(需要消化)
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public final class RedisUtil {
//Redis服务器IP ,端口,密码
private static String ADDR = "192.168.1.112";
private static int PORT = 6379;
private static String AUTH="123456";
//可连接的最大数目,默认值是8
//如果赋值是-1,代表不限制,如果pool已经分配了maxActive个jedis实例
//则此时pool的状态Wieexhausted(耗尽)
private static int MAX_ACTIVE = 1024;
//控制一个pool最多有多少个状态为idle(空闲的)jedis实例,默认值也是8
private static int MAX_IDLE = 200;
//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待
//时间,则直接抛出 JedisConnectionException;
private static int MAX_WAIT = 10000;
private static int TIMEOUT = 10000;
// 在 borrow 一个 jedis 实例时,是否提前进行 validate 操作;如果为 true,则
// 得到的 jedis 实例均是可用的;
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
//初始化Redis连接池
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT,
AUTH);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取实例
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 释放 jedis 资源
* @param jedis
*/
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.close();
}
}
}
使用单元测试
package com.shsxt;
import static org.junit.Assert.assertTrue;
import com.shsxt.Utils.RedisUtil;
import com.sun.org.apache.regexp.internal.REUtil;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
//新建redis客户端连接
Jedis jedis = new Jedis("192.168.1.112",6379);
//设置认证密码
jedis.auth("123456");
//通过set方法,添加字符串类型数据
jedis.set("sh","I win");
//获取缓存数据并输出
System.out.println(jedis.get("sh"));
}
@Test
//通过redis连接池获取连接对象并操作服务器
public void test02(){
//初始化redis客户端
JedisPool jedisPool = new JedisPool(new JedisPoolConfig(),"192.168.1.112",6379,10000,"123456");
//从连接池中获取连接
Jedis jedis = jedisPool.getResource();
jedis.set("redis","redis is good");
System.out.println("redis:"+jedis.get("redis"));
}
private Jedis jedis;
@Before
public void init(){
jedis= RedisUtil.getJedis();
}
/**
* 字符串操作
*/
@Test
public void test03(){
jedis.set("张不帅","其实是很帅!");
jedis.append("张不帅","I want");
System.out.println(jedis.get("张不帅"));
jedis.mset("name","zyd","age","20","addr","songjiang");
List<String> list = jedis.mget("name","age","addr");
if (null!=list&&list.size()>0){
for (String str : list){
System.out.println(str);
}
}
jedis.incr("age");
System.out.println(jedis.get("age"));
jedis.incrBy("age",20);
System.out.println(jedis.get("age"));
RedisUtil.returnResource(jedis);
}
/**
* hash的操作
*/
@Test
public void test04(){
Map<String,String> map = new HashMap<>();
map.put("id","1");
map.put("username","zyd");
map.put("addr","songjiang");
System.out.println("result:"+jedis.hmset("user:1",map));
//遍历
Iterator<String> iterator = jedis.hkeys("user:1").iterator();
while (iterator.hasNext()){
String key=iterator.next();
System.out.println("key:"+key+"value:"+jedis.hget("user:1",key));
RedisUtil.returnResource(jedis);
}
}
/**
* list 操作
*/
@Test
public void test05(){
jedis.del("java");
jedis.lpush("java","spring");
jedis.lpush("java","springBoot");
jedis.lpush("java","springMVC");
System.out.println(jedis.lrange("java",0,-1));
jedis.rpush("java","haha");
System.out.println(jedis.lrange("java",0,-1));
RedisUtil.returnResource(jedis);
}
/**
* set 操作
*/
@Test
public void test06(){
jedis.sadd("user","z");
jedis.sadd("user","y");
jedis.sadd("user","d");
jedis.sadd("user","z");
System.out.println(jedis.smembers("user"));//[y, d, z]
System.out.println(jedis.sismember("user","a"));//false
System.out.println(jedis.srandmember("user")); //d 随机一个元素
System.out.println(jedis.spop("user"));//z 取一个元素并删除
System.out.println(jedis.scard("user"));//2统计个数
RedisUtil.returnResource(jedis);
}
/**
* sort set 有序的set
*/
@Test
public void test07(){
jedis.zadd("shanghai",10,"z");
jedis.zadd("shanghai",12,"x");
jedis.zadd("shanghai",56,"y");
jedis.zadd("shanghai",60,"d");
System.out.println(jedis.zrange("shanghai",0,-1)); //[z, x, y, d]
System.out.println(jedis.zrevrange("shanghai",0,-1)); //[d, y, x, z]
System.out.println(jedis.zrangeByScore("shanghai",40,59)); //[y]
RedisUtil.returnResource(jedis);
}
}