在实际的开发中,会有这样的场景。有一个微服务需要提供一个查询的服务,但是需要查询的数据库表的数据量十分庞大,查询所需要的时间很长。 此时就可以考虑在项目中加入缓存。
引入依赖
在maven项目中引入如下依赖。并且需要在本地安装redis。
1 2 3 4 5 |
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> <version> 2.0 . 5 .release</version> </dependency> |
配置redis
在springboot的配置文件中添加如下代码。
1 2 3 4 5 6 7 8 9 10 |
redis: host: 127.0 . 0.1 port: 6379 timeout: 5000 database: 0 jedis: pool: max-idle: 8 max-wait: min-idle: 0 |
添加redis配置文件
新建名为redisconfig的配置类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import com.fasterxml.jackson.annotation.jsonautodetect; import com.fasterxml.jackson.annotation.propertyaccessor; import com.fasterxml.jackson.databind.objectmapper; import org.springframework.cache.cachemanager; import org.springframework.cache.annotation.cachingconfigurersupport; import org.springframework.cache.annotation.enablecaching; import org.springframework.cache.interceptor.keygenerator; 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.redistemplate; import org.springframework.data.redis.core.stringredistemplate; import org.springframework.data.redis.serializer.jackson2jsonredisserializer;
import java.time.duration;
/** * redisconfig * * @author detectivehlh * @date 2018-10-11 14:39 **/ @configuration @enablecaching public class redisconfig extends cachingconfigurersupport { @bean @override public keygenerator keygenerator() { return (target, method, params) -> { stringbuilder sb = new stringbuilder(); sb.append(target.getclass().getname()); sb.append(method.getname()); for (object obj : params) { sb.append(obj.tostring()); } return sb.tostring(); }; }
@bean public redistemplate<string, string> redistemplate(redisconnectionfactory factory) { objectmapper om = new objectmapper(); om.setvisibility(propertyaccessor.all, jsonautodetect.visibility.any); om.enabledefaulttyping(objectmapper.defaulttyping.non_final); //redis序列化 jackson2jsonredisserializer jackson2jsonredisserializer = new jackson2jsonredisserializer(object. class ); jackson2jsonredisserializer.setobjectmapper(om);
stringredistemplate template = new stringredistemplate(factory); template.setvalueserializer(jackson2jsonredisserializer); template.afterpropertiesset(); return template; }
/** * 自定义cachemanager */ @bean public cachemanager cachemanager(redistemplate redistemplate) { //全局redis缓存过期时间 rediscacheconfiguration rediscacheconfiguration = rediscacheconfiguration.defaultcacheconfig().entryttl(duration.ofdays( 1 )); rediscachewriter rediscachewriter = rediscachewriter.nonlockingrediscachewriter(redistemplate.getconnectionfactory()); return new rediscachemanager(rediscachewriter, rediscacheconfiguration); } } |
添加缓存配置
在项目的service层中的实现类中,添加@cacheable注解。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.hashmap;
/** * userloginserviceimpl * * @author detectivehlh * @date 2018-10-10 17:20 **/ @service public class userloginserviceimpl implements userloginservice { @autowired private userloginmapper userloginmapper;
@override @cacheable (value = "usercache" ) public hashmap getbyusername(string username) { system.out.println( "此时没有走缓存" ); return userloginmapper.getbyusername(username); } } |
然后调用一次该接口。就可以在redis中看到如下的key。
"usercache::com.detectivehlh.api.service.impl.userloginserviceimplgetbyusernamesolarfarm"
同时,可以在控制台中看到有"此时没有走缓存"的输出。然后再次调用该接口,就可以看到返回的速度明显变快,并且没有"此时没有走缓存"输出。说明 此时的接口走的是缓存。
总结
以上所述是小编给大家介绍的在springboot中添加redis及配置方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
原文链接:https://juejin.im/post/5bbf077d5188255c393f8c90
查看更多关于在SpringBoot中添加Redis及配置方法的详细内容...