1. Create project
1.1 creation steps
1.2 add pom dependency
<dependencies> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <!--test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>2.2.4.RELEASE</version> </dependency> </dependencies>
Open the redis visualization tool, and you can see that there is no data. Next, we will operate the data
New demo class
2. Operation string
/** * String operation */ @Test public void testString(){ //Connect to redis Jedis jedis = new Jedis("127.0.0.1",6379); //Add data jedis.set("name", "manlu"); //get data System.out.println(jedis.get("name")); //Concatenate string jedis.append("name", ".com"); System.out.println(jedis.get("name")); //Delete data jedis.del("name"); System.out.println(jedis.get("name")); //Set multiple key value pairs jedis.mset("name","Diffuse Road","age","18","qq","550945825"); jedis.incr("age");//Add 1 operation System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("qq")); }
Look at the visualizer after execution
3. Operation hash(map)
/** * redis Operation map set */ @Test public void testMap() { //Connect to redis Jedis jedis = new Jedis("127.0.0.1",6379); //Create a new map Map<String,String> map = new HashMap<String,String>(); map.put("name", "Diffuse Road"); map.put("age", "18"); map.put("qq", "550945825"); //1. Add data jedis.hmset("user", map); //2. Access to data //Take the name from the user and execute the result: [minxr] -- > note that the result is a generic List //The first parameter is the key stored in the map object in redis, followed by the key placed in the map object. The following keys can be multiple and variable List<String> rsmap = jedis.hmget("user", "name","age","qq"); System.out.println(rsmap); //3. Delete data //Delete a key value in the map jedis.hdel("user", "age"); System.out.println(jedis.hmget("user", "age"));//Null returned because deleted System.out.println(jedis.hlen("user"));//Returns the number of values stored in the key with key as user 2 System.out.println(jedis.exists("user"));//Whether there is a record with key as user, return true System.out.println(jedis.hkeys("user"));//Return all key s in the map object System.out.println(jedis.hvals("user"));//Returns all value s in the map object //Get the user and get every data through iterator Iterator<String> iter = jedis.hkeys("user").iterator(); while(iter.hasNext()) { String key = iter.next(); System.out.println(key+":" + jedis.hmget("user", key)); } }
Look at the visualizer after execution
3. Operation list
/** * redis Action List collection */ @Test public void testList() { //Connect to redis Jedis jedis = new Jedis("127.0.0.1",6379); //1. Get the data in the list. It must be empty here, because no data has been added yet //. lrange is taken out by range //The first is the key, the second is the start position, and the third is the end position. Jedis.len gets the length - 1 to get all System.out.println(jedis.lrange("list", 0, -1)); //2. Add data //First, three pieces of data are stored in the key list, and lpush is added to the header jedis.lpush("list","spring"); jedis.lpush("list", "springboot"); jedis.lpush("list", "springcloud"); //[springcloud, springboot, spring] System.out.println(jedis.lrange("list", 0, -1)); //3. Delete data jedis.del("list"); //2. Add data to the tail jedis.rpush("list", "spring"); jedis.rpush("list", "springboot"); jedis.rpush("list","springcloud"); //[spring, springboot, springcloud] System.out.println(jedis.lrange("list", 0, -1)); }
Look at the visualizer after execution
4. Operation set
/** * redis Operation set set */ @Test public void testSet() { //Connect to redis Jedis jedis = new Jedis("127.0.0.1",6379); //You have used user to operate the map. First, delete user jedis.del("user"); //1. Add jedis.sadd("user", "Xiao Zhang"); jedis.sadd("user", "Zhang Zhong"); jedis.sadd("user", "Zhang Zhang"); jedis.sadd("user", "Lao Zhang"); jedis.sadd("user", "Diffuse Road"); //2. Delete jedis.srem("user", "Xiao Zhang"); //3. Access System.out.println(jedis.smembers("user"));//Get all added value s System.out.println(jedis.sismember("user", "Diffuse Road"));//Determine if the "manway" is an element of the user set System.out.println(jedis.srandmember("user"));//Get all the data in the user collection System.out.println(jedis.scard("user"));//Returns the number of elements in the collection }
Look at the visualizer after execution
5.redis sorting
/** * redis sort */ @Test public void testSort() { //Connect to redis Jedis jedis = new Jedis("127.0.0.1",6379); //jedis sorting //Note that rpush and lpush here are List operations. It's a two-way List (but in terms of performance) jedis.del("a");//Clear the data first, then add the data for testing jedis.rpush("a", "1"); jedis.lpush("a", "6"); jedis.lpush("a", "3"); jedis.lpush("a", "9"); System.out.println("Before sorting: "+jedis.lrange("a", 0, -1)); System.out.println("Sort by: "+jedis.sort("a"));//[1,3,6,9] / / enter the result after sorting System.out.println("After sorting: "+jedis.lrange("a", 0, -1)); }
Perform rear console
6. Query all key s and delete them
/** * Query all key s and delete data iteratively */ @Test public void getKeys(){ //Connect to redis Jedis jedis = new Jedis("127.0.0.1",6379); Set<String> keys = jedis.keys("*"); if (keys.size()!=0){ Iterator<String> iterator = keys.iterator(); while (iterator.hasNext()){ String next = iterator.next(); System.out.println(next); jedis.del(next); } }else { System.out.println("No data to delete~"); } }
Look at the visualizer after execution