SpringBoot---Redis
Posted anpeiyong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot---Redis相关的知识,希望对你有一定的参考价值。
1、SpringBoot对Redis的支持
1.1、SpringBoot对Redis的支持 在org.springframework.boot.autoconfigure.redis包中:
a,RedisAutoConfiguration 为我们 默认自动配置了 JedisConnectionFactory、RedisTemplate、StringRedisTemplate;
b,RedisProperties 提供了 以“spring.redis”为前缀的属性配置;
2、案例
spring: profiles: active: test datasource: username: root password: 123456 url: jdbc:mysql://192.168.8.172:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT%2B8 redis: database: 0 host: 127.0.0.1 port: 6379 server: port : 9999
package com.an; import com.an.servletfilterlistener.MyFilter; import com.an.servletfilterlistener.MyListener; import com.an.servletfilterlistener.MyServlet; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.util.Objects; /** * @description: * @author: anpeiyong * @date: Created in 2019/11/14 19:56 * @since: */ @SpringBootApplication public class MyDemoApplication { public static void main(String[] args) { SpringApplication.run(MyDemoApplication.class, args); } @Bean public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){ RedisTemplate<Object,Object> redisTemplate=new RedisTemplate<Object, Object>(); redisTemplate.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper=new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); //设置value的序列化采用Jackson2JsonRedisSerializer redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); //设置key的序列化采用StringRedisSerializer redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } }
package com.an.cache.controller; import com.an.cache.dao.PersonDao; import com.an.cache.entity.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @description: * @author: anpeiyong * @date: Created in 2019/11/27 12:28 * @since: */ @RestController public class RedisController { @Autowired PersonDao personDao; /** * 存储字符串、对象 */ @RequestMapping(value = "/set") public void set(){ Person person=new Person("1","an",21); personDao.save(person); personDao.stringRedisTemplateDemo(); } /** * 获得字符串 * @return */ @RequestMapping(value = "/getString") public String getString(){ return personDao.getString(); } /** * 获得对象 * @return */ @RequestMapping(value = "/getPerson") public Person getPerson(){ return personDao.getPerson(); } }
package com.an.cache.dao; import com.an.cache.entity.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Repository; import javax.annotation.Resource; import java.util.concurrent.TimeUnit; /** * @description: * @author: anpeiyong * @date: Created in 2019/11/27 11:41 * @since: */ @Repository public class PersonDao { //SpringBoot为我们自动配置StringRedisTemplate,直接注入即可 @Autowired StringRedisTemplate stringRedisTemplate; //使用@Resource指定stringRedisTemplate,可注入基于字符串的简单属性操作方法 @Resource(name = "stringRedisTemplate") ValueOperations<String,String> stringRedisTemplateValueOperations; //SpringBoot为我们自动配置RedisTemplate,直接注入即可 @Autowired RedisTemplate<Object,Object> redisTemplate; //使用@Resource指定redisTemplate,可注入基于对象的简单属性操作方法 @Resource(name = "redisTemplate") ValueOperations<Object,Object> redisTemplateValueOperation; //使用StringRedisTemplate,存储字符串并设置超时时长20s public void stringRedisTemplateDemo(){ stringRedisTemplateValueOperations.set("xx","yy",20, TimeUnit.SECONDS); } //使用RedisTemplate,存储对象并设置超时时长20s public void save(Person person){ redisTemplateValueOperation.set(person.getId(),person,20, TimeUnit.SECONDS); } //使用StringRedisTemplate,获取字符串 public String getString(){ return stringRedisTemplateValueOperations.get("xx"); } //使用RedisTemplate,获取对象 public Person getPerson(){ return (Person) redisTemplateValueOperation.get("1"); } }
以上是关于SpringBoot---Redis的主要内容,如果未能解决你的问题,请参考以下文章
Springboot redis 缓存使用示例 | RedisTemplate
springboot Redis + HandlerInerceptor实现接口防刷