Jedis操作hash和Jedis操作List
Jedis操作hash和Jedis操作List
- 哈希类型 hash :map个时间哦
- hset
- hget
- hgetAll
package com.ailyt.jiedis.test;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import java.util.Map;
import java.util.Set;
public class JedisTest {
/**
* 快速入门
*/
@Test
public void MyTest01() {
//获取连接
Jedis jedis = new Jedis("localhost",6379);
//操作
// jedis.set("username", "zhangsan");
// String set = jedis.set("password", "root");
// System.out.println(set);
String username = jedis.get("username");
String password = jedis.get("password");
System.out.println(username);//zhangsan
System.out.println(password);//root
long del1 = jedis.del("username");//1
long del2 = jedis.del("password");//1
System.out.println(del1);
System.out.println(del2);
Set<String> keys = jedis.keys("*");
System.out.println(keys.toString());
//关闭连接
jedis.close();
}
/**
* 哈希类型
*/
@Test
public void MyTest02() {
Jedis jedis = new Jedis("localhost",6379);
long hset1 = jedis.hset("user", "name", "张三");
long hset2 = jedis.hset("user", "age", "21");
long hset3 = jedis.hset("user", "gender", "男");
System.out.println(hset1);
System.out.println(hset2);
System.out.println(hset3);
String hget = jedis.hget("user", "name");
System.out.println(hget);
Map<String, String> user = jedis.hgetAll("user");
user.forEach((s, s2) -> {
System.out.println(s+"--->"+s2);
});
}
}
- 列表类型list:linkedlist格式,支持重复元素
- lpush/rpush
- lpop/rpop
package com.ailyt.jiedis.test;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class JedisTest {
/**
* 快速入门
*/
@Test
public void MyTest01() {
//获取连接
Jedis jedis = new Jedis("localhost",6379);
//操作
// jedis.set("username", "zhangsan");
// String set = jedis.set("password", "root");
// System.out.println(set);
String username = jedis.get("username");
String password = jedis.get("password");
System.out.println(username);//zhangsan
System.out.println(password);//root
long del1 = jedis.del("username");//1
long del2 = jedis.del("password");//1
System.out.println(del1);
System.out.println(del2);
Set<String> keys = jedis.keys("*");
System.out.println(keys.toString());
//关闭连接
jedis.close();
}
/**
* 哈希类型
*/
@Test
public void MyTest02() {
Jedis jedis = new Jedis("localhost",6379);
long hset1 = jedis.hset("user", "name", "张三");
long hset2 = jedis.hset("user", "age", "21");
long hset3 = jedis.hset("user", "gender", "男");
System.out.println(hset1);
System.out.println(hset2);
System.out.println(hset3);
String hget = jedis.hget("user", "name");
System.out.println(hget);
Map<String, String> user = jedis.hgetAll("user");
user.forEach((s, s2) -> {
System.out.println(s+"--->"+s2);
});
jedis.close();
}
@Test
public void MyTest03() {
Jedis jedis = new Jedis("localhost", 6379);
// long lpush1 = jedis.lpush("user", "zs", "ls", "ww");
// System.out.println(lpush1);
// long rpush1 = jedis.rpush("user", "zl", "tq");
// System.out.println(rpush1);
String del1 = jedis.lpop("user");
System.out.println("移除了->" + del1);
String del2 = jedis.rpop("user");
System.out.println("移除了->" + del2);
System.out.println("------------------");
List<String> user = jedis.lrange("user", 0, -1);
user.forEach(s -> System.out.println(s));
jedis.close();
}
}
运行结果: