In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
In the last article, we used StringRedisTemplate, but there was a problem that forced me to rewrite the code. The problem is: when we put the digital String type in the cache, when we use Spring could to send the obtained data to another service, we find that the data has been strongly converted to Integer type, because the data we may transfer is large and diverse, in order to unify the type, and to facilitate development. So I changed the cache to RedisTemplate to add, delete, modify and check. There is no special example of update operation. The update operation is the same as the add operation. When key is the same, the original value value will be overwritten and the update will be completed. RedisTemplate requires us to configure it and instantiate it ourselves. Next, for example, go to the code:
First set up a Spring boot project to add Redis dependencies
Download and import IDE, and we observe the pom.xml file:
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
1. Configure application.properties
# redisspring.redis.host= Host address spring.redis.password=adminspring.redis.port=6379spring.redis.timeout=10000spring.redis.jedis.pool.max-idle=200 spring.redis.jedis.pool.min-idle=300000 spring.redis.jedis.pool.max-active=400spring.redis.jedis.pool.max-wait=10000
two。 We write the configuration class to instantiate the RedisTemplate
Package com.test.redis.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import org.springframework.data.redis.connection.RedisConnectionFactory @ Configurationpublic class RedisConfig {/ * instantiate RedisTemplate object * * @ return * / @ Bean public RedisTemplate functionDomainRedisTemplate (RedisConnectionFactory redisConnectionFactory) {RedisTemplate redisTemplate = new RedisTemplate (); initDomainRedisTemplate (redisTemplate, redisConnectionFactory); return redisTemplate } / * set the serialization mode in which the data is stored in redis, and open the transaction * * @ param redisTemplate * @ param factory * / private void initDomainRedisTemplate (RedisTemplate redisTemplate, RedisConnectionFactory factory) {/ / if Serializer is not configured, String is used by default when storing. If User is used, an error User can't cast to / / String will be prompted! RedisTemplate.setKeySerializer (new StringRedisSerializer ()); redisTemplate.setHashKeySerializer (new StringRedisSerializer ()); redisTemplate.setHashValueSerializer (new GenericJackson2JsonRedisSerializer ()); redisTemplate.setValueSerializer (new GenericJackson2JsonRedisSerializer ()); / / Open transaction redisTemplate.setEnableTransactionSupport (true); redisTemplate.setConnectionFactory (factory);}}
3. Write to the Service layer of cache operation, and define the method of adding, deleting, modifying and querying:
Package cn.com.dhcc.idatabus.admin.console.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.RedisTemplate;import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;import org.springframework.stereotype.Service;@Servicepublic class RedisService {@ Resource private RedisTemplate 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
4. The entity class of this example
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 + "]";}}
5. Write a Controller layer to implement caching operations
Package com.test.redis.web;import java.util.ArrayList;import java.util.HashMap;import java.util.List;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 ("/ templateAdd.do") @ ResponseBody public Map addUser (HttpServletRequest request) {Map modelMap=new HashMap (); User user=new User (); user.setName ("hehename"); user.setPassword ("hehePassword"); / / store hash 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 } @ GetMapping ("/ templateDelete.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;}}
Next, we visit the Controller path
(1) http://localhost:8081/templateAdd.do
Results:
(2) http://localhost:8081/templateDelete.do
Results:
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.