Jedis操作hash和Jedis操作list
Jedis操作hash
/** * hash */ @Test public void test3(){ //1.获取连接 Jedis jedis = new Jedis();//如果使用空参构造,默认值"localhost",6379端口 //存储hash jedis.hset("user","name","lisi"); jedis.hset("user","age","23"); jedis.hset("user","gender","male"); //获取hash String name = jedis.hget("user", "name"); System.out.println(name); //获取hash所有map中的数据 Map<String, String> user = jedis.hgetAll("user"); Set<String> keySet = user.keySet(); for (String key : keySet) { //获取value String value = user.get(key); System.out.println(key+":"+value); } //关闭连接 jedis.close(); }
Jedis操作list
/** * list */ @Test public void test4(){ //1.获取连接 Jedis jedis = new Jedis();//如果使用空参构造,默认值"localhost",6379端口 //存储list jedis.lpush("mylist","a","b","c");//从左边存 jedis.rpush("mylist","a","b","c");//从右边存 //list 范围获取 List<String> mylist = jedis.lrange("mylist", 0, -1); System.out.println(mylist); //弹出 String element1 = jedis.lpop("mylist"); System.out.println(element1); String element2 = jedis.rpop("mylist"); System.out.println(element2); //list 范围获取 List<String> mylist2 = jedis.lrange("mylist", 0, -1); System.out.println(mylist2); //关闭连接 jedis.close(); }