Recently, redis cache has been used in our work, so we share something we have summarized. This article uses StringRedisTemplate for learning. The value here is: (1) StringRedisTemplate needs to be serialized when performing batch deletion operation, (2) the update operation is the same as the add operation, and the next code is as follows:
1. Create a Spring boot project and introduce Redis dependency (pom.xml is as follows):
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>redis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>redis</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2. Write the spring boot configuration file. The content of my configuration here is simple. If you need other configurations, you can check them on the official website
#Redis spring.redis.host=192.168.100.86 spring.redis.password=admin spring.redis.port=6379 server.port=8081
3. Next, we start to write tests
(1) establish entity class:
User:
package com.test.redis.entity; public class User { private Integer id; private String name; private String password; public User() { super(); } public User(Integer id, String name, String password) { super(); this.id = id; this.name = name; this.password = password; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", password=" + password + "]"; } }
(2) service layer, which mainly defines various operation methods of redis
RedisService:
package com.test.redis.service; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.stereotype.Service; @Service public class RedisService { @Resource private StringRedisTemplate template; /** * Store or modify data * * @param modelMap * @param mapName */ public void setKey(String mapName, Map<String, Object> modelMap) { HashOperations<String, Object, Object> hps = template.opsForHash(); hps.putAll(mapName, modelMap); } /** * Get data Map * * @param mapName * @return */ public Map<Object, Object> getMapValue(String mapName) { HashOperations<String, Object, Object> hps = this.template.opsForHash(); return hps.entries(mapName); } /** * Get data value * * @param mapName * @param hashKey * @return */ public Object getValue(String mapName, String hashKey) { HashOperations<String, Object, Object> hps = this.template.opsForHash(); return hps.get(mapName, hashKey); } /** * Bulk delete cached data * * @param keys */ public void deleteData(List<String> keys) { // Serialize template before bulk delete template.setKeySerializer(new JdkSerializationRedisSerializer()); template.delete(keys); } }
(3) controller layer code, demonstrating the operation (adding and obtaining values):
package com.test.redis.web; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.test.redis.entity.User; import com.test.redis.service.RedisService; @Controller public class UserController { private static final String mapName="mapName"; @Autowired private RedisService redisService; @GetMapping( "/add.do") @ResponseBody public Map<Object, Object> addUser(HttpServletRequest request){ Map<String, Object> modelMap=new HashMap<String,Object>(); User user=new User(); user.setName("hehename"); user.setPassword("hehePassword"); //Store hash value modelMap.put("name", user.getName()); modelMap.put("password", user.getPassword()); redisService.setKey(mapName, modelMap); //Get map collection Map<Object, Object> modelMap1= redisService.getMapValue(mapName); Object value= redisService.getValue(mapName, "name"); System.out.println(" value : "+value); modelMap1.put("From cache by key Obtained value", value); return modelMap1; } }
Presentation results at the front desk:
(4) delete and get value operation:
@GetMapping( "/delete.do") @ResponseBody public Map<Object, Object> deleteUser(HttpServletRequest request){ //Get the key value to be deleted. Here we do the bulk deletion List<String> keys=new ArrayList<>(); keys.add("heheanme"); //Start delete operation redisService.deleteData(keys); //Get map collection Map<Object, Object> modelMap1= redisService.getMapValue(mapName); Object value= redisService.getValue(mapName, "name"); System.out.println(" value : "+value); modelMap1.put("From cache by key Obtained value", value); return modelMap1; }
Results displayed in the foreground:
This shows that the operation is successful