洛阳信息港,艾斯奥特曼目录,kiss×sis
依赖如下:
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </dependency>
配置文件如下:
spring: redis: open: true # 是否开启redis缓存 true开启 false关闭 database: 0 host: 47.104.208.124 port: 6378 password: lf.1228 # 密码(默认为空) timeout: 6000 # 连接超时时长(毫秒) pool: max-active: 1000 # 连接池最大连接数(使用负值表示没有限制) max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) max-idle: 10 # 连接池中的最大空闲连接 min-idle: 5 # 连接池中的最小空闲连接
redisconfig类:
@configuration public class redisconfig { @autowired private redisconnectionfactory factory; @bean public redistemplate<string, object> redistemplate() { redistemplate<string, object> redistemplate = new redistemplate<>(); redistemplate.setkeyserializer(new stringredisserializer()); redistemplate.sethashkeyserializer(new stringredisserializer()); redistemplate.sethashvalueserializer(new stringredisserializer()); redistemplate.setvalueserializer(new stringredisserializer()); redistemplate.setconnectionfactory(factory); return redistemplate; } @bean public hashoperations<string, string, object> hashoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforhash(); } @bean public valueoperations<string, string> valueoperations(redistemplate<string, string> redistemplate) { return redistemplate.opsforvalue(); } @bean public listoperations<string, object> listoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforlist(); } @bean public setoperations<string, object> setoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforset(); } @bean public zsetoperations<string, object> zsetoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforzset(); } }
redisutils如下:
@component public class redisutils { @autowired private redistemplate<string, object> redistemplate; @autowired private valueoperations<string, string> valueoperations; @autowired private hashoperations<string, string, object> hashoperations; @autowired private listoperations<string, object> listoperations; @autowired private setoperations<string, object> setoperations; @autowired private zsetoperations<string, object> zsetoperations; /** 默认过期时长,单位:秒 */ public final static long default_expire = 60 * 60 * 24; /** 不设置过期时长 */ public final static long not_expire = -1; private final static gson gson = new gson(); public void set(string key, object value, long expire){ valueoperations.set(key, tojson(value)); if(expire != not_expire){ redistemplate.expire(key, expire, timeunit.seconds); } } public void set(string key, object value){ set(key, value, default_expire); } public <t> t get(string key, class<t> clazz, long expire) { string value = valueoperations.get(key); if(expire != not_expire){ redistemplate.expire(key, expire, timeunit.seconds); } return value == null ? null : fromjson(value, clazz); } public <t> t get(string key, class<t> clazz) { return get(key, clazz, not_expire); } public string get(string key, long expire) { string value = valueoperations.get(key); if(expire != not_expire){ redistemplate.expire(key, expire, timeunit.seconds); } return value; } public string get(string key) { return get(key, not_expire); } public void delete(string key) { redistemplate.delete(key); } /** * object转成json数据 */ private string tojson(object object){ if(object instanceof integer || object instanceof long || object instanceof float || object instanceof double || object instanceof boolean || object instanceof string){ return string.valueof(object); } return gson.tojson(object); } /** * json数据,转成object */ private <t> t fromjson(string json, class<t> clazz){ return gson.fromjson(json, clazz); } }
springboot如何封装redis:
redistemplate
所在包: org.springframework.data.redis.core
作用:redis模板,redis事务,序列化支持,操作redis方法
jedisconnectionfactory
所在包:org.springframework.data.redis.connection.jedis
作用:redis连接工厂类,创建redis连接池等
redisautoconfiguration
所在包:org.springframework.boot.autoconfigure.data.redis
作用:将redis配置文件相关信息注入工厂类
redisproperties
所在包:org.springframework.boot.autoconfigure.data.redis
作用:redis连接基础类通过@configurationproperties注解将配置信息注入属性
总结
以上所述是小编给大家介绍的redis在springboot中的使用教程,希望对大家有所帮助
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
浅析我对 String、StringBuilder、StringBuffer 的理解
使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)
Springboot整合freemarker 404问题解决方案
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
网友评论