In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Xiaobian to share with you how to integrate Redis database cache management in SpringBoot2.0, I believe most people still do not know how to share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!
I. Introduction to Redis
In addition to providing excellent automation support for commonly used relational databases, Spring Boot also provides automated configuration support for many NoSQL databases, including Redis, MongoDB, Elasticsearch. After these cases are sorted out, they will be uploaded to Git one after another.
SpringBoot2 version, supporting more and more rich components, Redis support is not only to extend the API, but also to replace the underlying Jedis dependency, replaced by Lettup.
Spring 2.0 Integration Redis1 Core Dependency org.springframework.boot spring-boot-starter-data-redis2, config file #port server: port: 8008spring: application: #Application Name name: node08-boot-redis # redis Configuration redis: host: 127.0.0.1 #timeout connection timeout: 1000ms jedis: pool: #Maximum number of database connections, set 0 to no limit max-active: 8 #Maximum number of connections waiting, set 0 to no limit max-idle: 8 #Maximum connection setup wait time. If this time is exceeded, an exception will be received. Set to-1 for unlimited. max-wait: -1ms #Minimum number of connections waiting, set 0 to no limit min-idle: 0
In this way, the Redis environment has been successfully configured, and the packaged API can be used directly.
3. Simple test case import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.web.bind.annotation.RequestMapping;import org. springframework. web. bind.annotation.RestController;import javax.annotation.Resource;import java.util.concurrent.TimeUnit;@RestControllerpublic class RedisController { @Resource private StringRedisTemplate stringRedisTemplate ; @RequestMapping("/setGet") public String setGet (){ stringRedisTemplate.opsForValue().set("cicada","smile"); return stringRedisTemplate.opsForValue().get("cicada") ; } @Resource private RedisTemplate redisTemplate ; /** * Set Key validity period 10 seconds */ @RequestMapping("/setKeyTime") public String setKeyTime (){ redisTemplate.opsForValue().set("timeKey","timeValue",10, TimeUnit.SECONDS); return "success" ; } @RequestMapping("/getTimeKey") public String getTimeKey (){ //Here, after Key expires, the string returned is 'null' return String.valueOf(redisTemplate.opsForValue().get("timeKey")) ; import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.annotation.Bean;import org.springframework. context.annotation.Configuration;import org.springframework.data. reducis.connection.lettuce.LettuceConnectionFactory;import org.springframework. data. reducis.core.RedisTemplate;import org.springframework.data. redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data. reducis.serializer.StringRedisSerializer; import java.io.Serializable;/** * Redis Configuration */@Configurationpublic class RedisConfig { private static final Logger LOGGER = LoggerFactory.getLogger(RedisConfig.class) ; /** * serialization configuration */ @Bean public RedisTemplate redisTemplate (LettuceConnectionFactory redisConnectionFactory) { LOGGER.info("RedisConfig == >> redisTemplate "); RedisTemplate template = new RedisTemplate(); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setConnectionFactory(redisConnectionFactory); return template; import com.boot.redis.entity.User;import org.springframework.data.redis.core.RedisTemplate;import org. springframework.web.bind.annotation.RequestMapping;import org. springframework. web. bind. annotation.RestController;import javax.annotation.Resource;import java.util.ArrayList;import java. util.List;@RestControllerpublic class SerializeController { @Resource private RedisTemplate redisTemplate ; @RequestMapping("/setUser") public String setUser (){ User user = new User() ; user.setName("cicada"); user.setAge(22); List list = new ArrayList() ; list.add("Primary"); list.add(""); list.add("High School"); list.add("University"); user.setEducation(list); redisTemplate.opsForValue().set("userInfo",user); return "success" ; } @RequestMapping("/getUser") public User getUser (){ return (User)redisTemplate.opsForValue().get("userInfo") ; }} The above is "How to integrate Redis database to realize cache management in SpringBoot2.0". Thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
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.