In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Recently, I used redis cache in my work, so I share something I summarized. This article uses StringRedisTemplate to learn. The value here is: (1) when StringRedisTemplate carries out batch delete operation, we need to serialize the template. (2) the update operation is the same as the add operation. The following code:
1. Set up a Spring boot project and introduce Redis dependencies (pom.xml as follows):
4.0.0 com.test redis 0.0.1-SNAPSHOT jar redis Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-data-redis Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test org.springframework.boot spring-boot-maven-plugin
two。 Write the spring boot configuration file. The content of my configuration here is simple. If you need other configurations, you can check it on the official website.
# Redis spring.redis.host= Host address spring.redis.password=adminspring.redis.port=6379server.port=8081
3. Next, we start to write tests.
(1) create an 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;@Servicepublic class RedisService {@ Resource private StringRedisTemplate template / * storing or modifying data * * @ param modelMap * @ param mapName * / public void setKey (String mapName, Map modelMap) {HashOperations hps = template.opsForHash (); hps.putAll (mapName, modelMap) } / * get data Map * * @ param mapName * @ return * / public Map getMapValue (String mapName) {HashOperations hps = this.template.opsForHash (); return hps.entries (mapName) } / * get data value * * @ param mapName * @ param hashKey * @ return * / public Object getValue (String mapName, String hashKey) {HashOperations hps = this.template.opsForHash (); return hps.get (mapName, hashKey) } / * bulk delete cache data * * @ param keys * / public void deleteData (List keys) {/ / serialize template template.setKeySerializer (new JdkSerializationRedisSerializer ()); template.delete (keys);}} when performing bulk delete operation
(3) controller layer code to demonstrate operation (add and get 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;@Controllerpublic class UserController {private static final String mapName= "mapName"; @ Autowired private RedisService redisService @ GetMapping ("/ add.do") @ ResponseBody public Map addUser (HttpServletRequest request) {Map modelMap=new HashMap (); 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 modelMap1= redisService.getMapValue (mapName); Object value= redisService.getValue (mapName, "name"); System.out.println ("value:" + value); modelMap1.put ("value fetched from cache based on key", value); return modelMap1;}}
The front desk shows the results:
(4) delete and obtain values:
@ GetMapping ("/ delete.do") @ ResponseBody public Map deleteUser (HttpServletRequest request) {/ / get the key value to be deleted. Here we delete List keys=new ArrayList (); keys.add ("heheanme"); / / start the delete operation redisService.deleteData (keys); / / get the map collection Map modelMap1= redisService.getMapValue (mapName) Object value= redisService.getValue (mapName, "name"); System.out.println ("value:" + value); modelMap1.put ("value fetched from the cache based on key", value); return modelMap1;}
The front desk displays the results:
This shows that the operation is successful.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.