这篇文章主要介绍了Redis在springboot中的使用教程,本文实例代码相结合的形式给大家介绍的非常详细,需要的朋友可以参考下
依赖如下:
org.springframework.bootspring-boot-starter-data-redis
配置文件如下:
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 # 连接池中的最小空闲连接
@Configuration public class RedisConfig { @Autowired private RedisConnectionFactory factory; @Bean public Redistemplate redistemplate() { Redistemplate 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 hashOperations(Redistemplate redistemplate) { return redistemplate.opsForHash(); } @Bean public ValueOperations valueOperations(Redistemplate redistemplate) { return redistemplate.opsForValue(); } @Bean public ListOperations listOperations(Redistemplate redistemplate) { return redistemplate.opsForList(); } @Bean public Setoperations setoperations(Redistemplate redistemplate) { return redistemplate.opsForSet(); } @Bean public ZSetoperations zSetoperations(Redistemplate redistemplate) { return redistemplate.opsForZSet(); } }
RedisUtils如下:
@Component public class RedisUtils { @Autowired private Redistemplate redistemplate; @Autowired private ValueOperations valueOperations; @Autowired private HashOperations hashOperations; @Autowired private ListOperations listOperations; @Autowired private Setoperations setoperations; @Autowired private ZSetoperations 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 get(String key, Class 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 get(String key, Class 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 fromJson(String json, Class clazz){ return gson.fromJson(json, clazz); } }
springboot如何封装redis:
Redistemplate
所在包: org.springframework.data.redis.core
作用:redis模板,redis事务,序列化支持,操作redis方法
JedisConnectionFactory
所在包:org.springframework.data.redis.connection.jedis
所在包:org.springframework.boot.autoconfigure.data.redis
RedisProperties
所在包:org.springframework.boot.autoconfigure.data.redis
作用:redis连接基础类通过@ConfigurationProperties注解将配置信息注入属性
总结
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。