栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Redis在springboot中的使用教程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Redis在springboot中的使用教程

依赖如下:


  org.springframework.boot
  spring-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    # 连接池中的最小空闲连接

RedisConfig类:

@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);
  }
  
  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);
  }
  
  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
作用:redis连接工厂类,创建redis连接池等

RedisAutoConfiguration

所在包:org.springframework.boot.autoconfigure.data.redis
作用:将redis配置文件相关信息注入工厂类

RedisProperties

所在包:org.springframework.boot.autoconfigure.data.redis
作用:redis连接基础类通过@ConfigurationProperties注解将配置信息注入属性

总结

以上所述是小编给大家介绍的Redis在springboot中的使用教程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/140704.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号