pom.xml
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
RedisTest.java
public static void main(String[] args) {
//连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
// 如果 Redis 服务设置了密码,需要下面这行,没有就不需要
// jedis.auth("123456");
System.out.println("连接成功");
//查看服务是否运行
System.out.println("服务正在运行: "+jedis.ping());
//设置 redis 字符串数据
jedis.set("runoobkey", "www.runoob.com");
// 获取存储的数据并输出
System.out.println("redis 存储的字符串为: "+ jedis.get("runoobkey"));
//存储数据到列表中
jedis.lpush("site-list", "Runoob");
jedis.lpush("site-list", "Google");
jedis.lpush("site-list", "Taobao");
// 获取存储的数据并输出
List<String> list = jedis.lrange("site-list", 0 ,2);
for(int i=0; i<list.size(); i++) {
System.out.println("列表项为: "+list.get(i));
}
// 获取数据并输出
Set<String> keys = jedis.keys("*");
Iterator<String> it=keys.iterator() ;
while(it.hasNext()){
String key = it.next();
System.out.println(key);
}
System.out.println("----------测试sadd smembers-------------");
//测试sadd smembers
jedis.sadd("language1","java","c","python","matlab") ;
Set<String> language1 = jedis.smembers("language1");
System.out.println(language1);
System.out.println("-----------测试srem------------");
//测试srem
jedis.srem("language1","c") ;
Set<String> language11 = jedis.smembers("language1");
System.out.println(language11);
System.out.println("-----------测试sdiff------------");
//测试sdiff
jedis.sadd("l1","java","c","IOS","android") ;
jedis.sadd("l2","java","python","c++") ;
Set<String> sdiff = jedis.sdiff("l1", "l2");
System.out.println(sdiff);
System.out.println("-----------测试sinter------------");
//测试sinter
Set<String> sinter = jedis.sinter("l1", "l2");
System.out.println(sinter);
System.out.println("-----------测试sunion------------");
//测试sunion
Set<String> sunion = jedis.sunion("l1", "l2");
System.out.println(sunion);
}