Spring Boot 2整合Redis做缓存
程序员文章站
2023-11-06 14:16:10
既然是要用Redis做缓存,自然少不了安装了。但是本文主要讲Spring Boot与Redis整合。安装教程请另行百度! 1.首先是我们的Redis配置类 到这就可以测试了。由于时间原因就先不贴测试截图了。 ......
既然是要用redis做缓存,自然少不了安装了。但是本文主要讲spring boot与redis整合。安装教程请另行百度!
1.首先是我们的redis配置类
1 package com.tyc; 2 3 import com.fasterxml.jackson.annotation.jsonautodetect; 4 import com.fasterxml.jackson.annotation.propertyaccessor; 5 import com.fasterxml.jackson.databind.objectmapper; 6 import org.springframework.cache.annotation.enablecaching; 7 import org.springframework.context.annotation.bean; 8 import org.springframework.context.annotation.configuration; 9 import org.springframework.data.redis.connection.redisconnectionfactory; 10 import org.springframework.data.redis.core.redistemplate; 11 import org.springframework.data.redis.serializer.jackson2jsonredisserializer; 12 import org.springframework.data.redis.serializer.stringredisserializer; 13 14 /** 15 * @description: redis配置类 16 * @author: wushumin 17 * @createdate: 2019/9/12 16:00 18 * @version: 1.0 19 */ 20 @configuration 21 public class redisconfig { 22 @bean 23 @suppresswarnings("all") 24 public redistemplate<string, object> redistemplate(redisconnectionfactory factory) { 25 redistemplate<string, object> template = new redistemplate<string, object>(); 26 template.setconnectionfactory(factory); 27 jackson2jsonredisserializer jackson2jsonredisserializer = new jackson2jsonredisserializer(object.class); 28 objectmapper om = new objectmapper(); 29 om.setvisibility(propertyaccessor.all, jsonautodetect.visibility.any); 30 om.enabledefaulttyping(objectmapper.defaulttyping.non_final); 31 jackson2jsonredisserializer.setobjectmapper(om); 32 stringredisserializer stringredisserializer = new stringredisserializer(); 33 // key采用string的序列化方式 34 template.setkeyserializer(stringredisserializer); 35 // hash的key也采用string的序列化方式 36 template.sethashkeyserializer(stringredisserializer); 37 // value序列化方式采用jackson 38 template.setvalueserializer(jackson2jsonredisserializer); 39 // hash的value序列化方式采用jackson 40 template.sethashvalueserializer(jackson2jsonredisserializer); 41 template.afterpropertiesset(); 42 return template; 43 } 44 }
package com.tyc.annotation; import org.springframework.boot.autoconfigure.condition.conditionalonclass; import org.springframework.boot.autoconfigure.condition.conditionalonmissingbean; import org.springframework.boot.autoconfigure.data.redis.redisproperties; import org.springframework.boot.context.properties.enableconfigurationproperties; import org.springframework.cache.cachemanager; import org.springframework.cache.annotation.cachingconfigurersupport; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.data.redis.cache.rediscacheconfiguration; import org.springframework.data.redis.cache.rediscachemanager; import org.springframework.data.redis.cache.rediscachewriter; import org.springframework.data.redis.connection.redisconnectionfactory; import org.springframework.data.redis.core.redisoperations; import org.springframework.data.redis.core.stringredistemplate; import java.net.unknownhostexception; import java.time.duration; import java.util.hashmap; import java.util.map; @configuration @conditionalonclass(redisoperations.class) @enableconfigurationproperties(redisproperties.class) public class redisautoconfiguration extends cachingconfigurersupport {//缓存管理器 @bean public cachemanager cachemanager(redisconnectionfactory connectionfactory) { //缓存配置 rediscacheconfiguration usercacheconfiguration = rediscacheconfiguration.defaultcacheconfig() .entryttl(duration.ofseconds(60)).disablecachingnullvalues(); map<string, rediscacheconfiguration> rediscacheconfigurationmap = new hashmap<>();
//手动指定缓存模块 rediscacheconfigurationmap.put("legal", usercacheconfiguration); rediscacheconfigurationmap.put("fuzzysearch", usercacheconfiguration); //初始化一个rediscachewriter rediscachewriter rediscachewriter = rediscachewriter.nonlockingrediscachewriter(connectionfactory); rediscacheconfiguration defaultcacheconfig = rediscacheconfiguration.defaultcacheconfig(); //设置默认超过期时间是半小时 defaultcacheconfig.entryttl(duration.ofseconds(60 * 30)); // //初始化rediscachemanager rediscachemanager cachemanager = new rediscachemanager(rediscachewriter, defaultcacheconfig, rediscacheconfigurationmap); return cachemanager; } }
@cacheable(cachenames = "fuzzysearch", key = "targetclass+methodname+#fuzzysearchname") public map fuzzysearch(string fuzzysearchname) { //返回结果集合 map resultmap = new linkedhashmap(); return resultmap }
到这就可以测试了。由于时间原因就先不贴测试截图了。
推荐阅读
-
Spring Boot2.X整合消息中间件RabbitMQ原理简浅探析
-
spring boot从redis取缓存发生java.lang.ClassCastException异常
-
Spring Boot 2整合Redis做缓存
-
十、Spring boot 简单优雅的整合 Swagger2
-
Spring Boot2(二):使用Spring Boot2集成Mybatis缓存机制
-
浅谈Spring Boot中Redis缓存还能这么用
-
spring-boot-2.0.3之redis缓存实现,不是你想的那样哦!
-
Spring Boot(十三):整合Redis哨兵,集群模式实践
-
Spring Boot2(三):使用Spring Boot2集成Redis缓存
-
spring boot2 redis使用