Springboot集成redis

作者: 好多可乐 | 来源:发表于2021-02-23 23:44 被阅读0次

    1,导入依赖

     <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-cache</artifactId>
            </dependency>
    

    2,application.properties配置文件redis参数

    spring.redis.host=localhost
    spring.redis.password=
    spring.redis.port=6379
    

    3,springboot application启动类和要加入缓存的类加上对应注解

    @SpringBootApplication
    // @MapperScan写在SpringBoot的启动类上(xxxApplication.java),作用是扫描Mapper接口类。
    @MapperScan(basePackages = "com.imooc.mall.model.dao")
    // 开启swagger服务
    @EnableSwagger2
    // 打开缓存功能
    @EnableCaching
    public class MallApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MallApplication.class, args);
        }
    
    }
    
    
        @Override
        // 对要进行缓存的类进行缓存配置
        @Cacheable(value = "listForCustomer")
        public List<CategoryVo> listForCustomer() {
            List<CategoryVo> categoryVoList = new ArrayList<CategoryVo>();
            recursivelyFindCategories(categoryVoList, 0);
            return categoryVoList;
        }
    

    4,对应的类继承Serializable接口

    / 必须实现序列化才能实现缓存功能
    public class CategoryVo implements Serializable {
        private List<CategoryVo> childCategory = new ArrayList<>();
        private Integer id;
    
        private String name;
    
        private Integer type;
    
        private Integer parentId;
    
        private Integer orderNum;
    
        private Date createTime;
    
        private Date updateTime;
    
        public List<CategoryVo> getChildCategory() {
            return childCategory;
        }
    
        public void setChildCategory(List<CategoryVo> childCategory) {
            this.childCategory = childCategory;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name == null ? null : name.trim();
        }
    
        public Integer getType() {
            return type;
        }
    
        public void setType(Integer type) {
            this.type = type;
        }
    
        public Integer getParentId() {
            return parentId;
        }
    
        public void setParentId(Integer parentId) {
            this.parentId = parentId;
        }
    
        public Integer getOrderNum() {
            return orderNum;
        }
    
        public void setOrderNum(Integer orderNum) {
            this.orderNum = orderNum;
        }
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    
        public Date getUpdateTime() {
            return updateTime;
        }
    
        public void setUpdateTime(Date updateTime) {
            this.updateTime = updateTime;
        }
    }
    
    

    5,配置缓存的配置类

    
    /**
     * 描述:     缓存的配置类
     */
    @Configuration
    @EnableCaching
    public class CachingConfig {
    
        @Bean
        public RedisCacheManager redisCacheManager(RedisConnectionFactory connectionFactory) {
    
            RedisCacheWriter redisCacheWriter = RedisCacheWriter
                    .lockingRedisCacheWriter(connectionFactory);
            RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
            cacheConfiguration = cacheConfiguration.entryTtl(Duration.ofSeconds(30));
    
            RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter,
                    cacheConfiguration);
            return redisCacheManager;
        }
    }
    
    

    相关文章

      网友评论

        本文标题:Springboot集成redis

        本文链接:https://www.haomeiwen.com/subject/tehjfltx.html