----------------------------导航目录--------------------------------

一、简单使用,序列化开启缓存机制使用

二、lua脚本实现Reids分布式锁

三、reids客户端管道使用

四、reids订阅发布应用

-------------------------------------------------------------------

1、引入pom文件

  注意版本号的区别哦~,小心采坑,根据需要选型版本号springboot客户端reids版本选择

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.1.11.RELEASE</version>
</dependency>

2、添加配置文件application.yml或者properties

spring:
  redis:
    database: 0
    host: localhost
    port: 6379
   # 连接超时时间 单位 ms(毫秒)
    timeout: 3000
    password:
    lettuce:
      pool:
        #连接池中的最大空闲连接,默认值也是0。
        max-idle: 8
        #连接池中的最小空闲连接,默认值也是0。
        min-idle: 0
        # 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
        max-active: 8
        # 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出异常
        max-wait: -1

单纯使用的话到这里就可以直接注入RedisTemplate对象使用了,我这里对接下来的使用序列化和缓存注解的使用进行了配置

3、创建Reids客户端,并指定序列化方式并且开启缓存注解的使用

  第一种,使用springboot封装的RedisTemplate(推荐)

  这里引入了3个类如下:

  CacheTtl 枚举类型用来定义缓存的时间

  FastJsonRedisSerializer指定序列化方式,注意引入的包用户排除一下问题

  RedisConfig 配置RedisTemplate对象的序列化和cache管理对象 。注解使用:SpringBoot进阶教程(五十三)整合Redis之@Cacheable、@CachePut、@CacheEvict的应用

package com.niu.reids;

/**
 * @author niunafei
 * @function
 * @email niunafei0315@163.com
 * @date 2020/5/19  3:26 PM
 */
public enum CacheTtl {

    /**
     * 1小时
     */
    ONE_HOUR("oneHour", 1),
    /**
     * 1天
     */
    ONE_DAY("oneDay", 24),
    /**
     * 2天
     */
    TWO_DAYS("twoDays", 48),
    /**
     * 1周
     */
    ONE_WEEK("oneWeek", 168);

    private String value;

    private Integer time;

    CacheTtl(String value, Integer time) {
        this.value = value;
        this.time = time;
    }

    public String getValue() {
        return value;
    }

    public Integer getTime() {
        return time;
    }
}
View Code

相关文章: