黑马程序
Jedis–快速入门
@Test
public void test1(){
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.set("name","张三");
jedis.close();
}
Jedis–操作String
@Test
public void test1(){
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.set("name","张三");
String name = jedis.get("name");
System.out.println(name);
//存储指定时间过期的键值对
jedis.setex("activecode",10,"fade");
jedis.close();
}
Jedis–操作hash
@Test
public void test2(){
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.hset("user","name","tom");
jedis.hset("user","age","22");
jedis.hset("user","sex","man");
String hget = jedis.hget("user", "name");
System.out.println(hget);
Map<String, String> user = jedis.hgetAll("user");
for (String s : user.keySet()) {
System.out.println(s+":"+user.get(s));
}
jedis.close();
}
Jedis–操作list
@Test
public void test3(){
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.lpush("mylist","a","b","c");
jedis.rpush("mylist","a","b","c");
List<String> mylist = jedis.lrange("mylist", 0, -1);
System.out.println(mylist);
//弹出集合
jedis.lpop("mylist");
jedis.rpop("mylist");
mylist = jedis.lrange("mylist", 0, -1);
System.out.println(mylist);
jedis.close();
}
Jedis–操作set&shortedset
@Test
public void test4(){
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.sadd("myset","手鞠","我爱罗","勘九郎");
Set<String> myset = jedis.smembers("myset");
System.out.println(myset);
jedis.close();
}
@Test
public void test5(){
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.zadd("myzset",2,"手鞠");
jedis.zadd("myzset",1,"我爱罗");
jedis.zadd("myzset",3,"勘九郎");
Set<String> myzset = jedis.zrange("myzset", 0, -1);
System.out.println(myzset);
jedis.close();
}