Lake's Blog Lake's Blog
首页
HCFrame
  • 博客搭建

    • 搜索引擎
    • SEO优化
    • 问题记录
  • Vue

    • 问题记录
  • uni-app
  • 开发

    • Spring
  • 数据库及中间件

    • Elasticsearch
    • SQL
  • 杂谈

    • 杂谈
  • 微服务

    • nacos
    • CAS
  • 算法说明

    • algorithm
  • leetCode

    • leetCode
  • 代理

    • Nginx
  • Linux

    • ubuntu
  • Docker
  • 数据库
  • 消息队列
  • openwrt
  • 友情链接
关于
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub

Lake Liu

很菜的程序员
首页
HCFrame
  • 博客搭建

    • 搜索引擎
    • SEO优化
    • 问题记录
  • Vue

    • 问题记录
  • uni-app
  • 开发

    • Spring
  • 数据库及中间件

    • Elasticsearch
    • SQL
  • 杂谈

    • 杂谈
  • 微服务

    • nacos
    • CAS
  • 算法说明

    • algorithm
  • leetCode

    • leetCode
  • 代理

    • Nginx
  • Linux

    • ubuntu
  • Docker
  • 数据库
  • 消息队列
  • openwrt
  • 友情链接
关于
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub
  • Spring

    • Spring通过URL传值不能携带"."???
    • Pathvariable注解允许参数为空吗?
    • Spring事务及隔离级别
    • Autowired实现多个实现类
    • SpringBoot集成EhCache
    • SpringBoot如何获取Bean列表
    • SpringBoot通过CacheManager集成redis做缓存
      • 1. 添加依赖
      • 2. application配置
      • 3.入口类配置
      • 4.redis配置类
      • 5.使用Cache
      • 结束语
    • Spring不能将包含key值为null的map集合转换成JSON
  • 数据库及中间件

  • 杂谈

  • 微服务

SpringBoot通过CacheManager集成redis做缓存

# SpringBoot通过CacheManager集成redis做缓存

SpringBoot如何通过 CacheManager 集成 redis做缓存

SpringBoot在annotation的层面实现了数据缓存的功能,基于Spring的AOP技术。所有的缓存配置只是在annotation层面配置,像声明式事务一样。

Spring定义了CacheManager 和Cache接口统一不同的缓存技术。其中CacheManager 是Spring提供的各种缓存技术的抽象接口。而Cache接口包含缓存的各种操作。

# 1. 添加依赖

引入springboot-cache和 spring-redis。

    <!-- 缓存 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <!-- redis -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
1
2
3
4
5
6
7
8
9
10

# 2. application配置

此处我选择使用 .yml 文件配置

spring:
  # 缓存设置
  cache:
    # redis缓存
    type: redis
    redis:
    	# 缓存超时默认时间,此处设置为一天
      time-to-live: 1d
      # 是否启用缓存key统一前缀,默认为true
      use-key-prefix: true
      # 是否保存null值,默认为true
      cache-null-values: true
      # 设置缓存key前缀
      key-prefix: cache.
  redis:
    database: 0
    host: 192.168.4.119
    port: 6379
    password:
    lettuce:
      pool:
        # 连接池中的最大空闲连接 默认8
        max-idle: 8
        # 连接池中的最小空闲连接 默认0
        min-idle: 0
        # 连接池最大连接数 默认8 ,负数表示没有限制
        max-active: 8
        # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认-1
        max-wait: -1
    timeout: 30000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# 3.入口类配置

加入注解 @EnableCaching

@SpringBootApplication
@EnableCaching
public class DemoApplication {
}
1
2
3
4

# 4.redis配置类

@Configuration
public class RedisConfig {

    /**
     * springboot2.x 使用LettuceConnectionFactory 代替 RedisConnectionFactory
     * application.yml配置基本信息后,springboot2.x  RedisAutoConfiguration能够自动装配
     * LettuceConnectionFactory 和 RedisConnectionFactory 及其 RedisTemplate
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# 5.使用Cache

注入SpringBoot自动配置的bean,org.springframework.cache.CacheManager。 一个简单的测试类:

package com.bbf.frame.test;

import com.bbf.frame.Application;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK)
public class TestCache {
  @Resource
  private CacheManager cacheManager;

  @Test
  public void cacheTest() {
    // 显示所有的Cache空间
    System.out.println(StringUtils.join(cacheManager.getCacheNames(), ","));
    Cache cache = cacheManager.getCache("userCache");
    cache.put("key", "123");
    System.out.println("缓存成功");
    String res = cache.get("key", String.class);
    System.out.println(res);
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# CacheManager转换

    // 获取EhCache的管理器
    org.springframework.cache.ehcache.EhCacheCacheManager cacheCacheManager = (EhCacheCacheManager) cacheManager;
    net.sf.ehcache.CacheManager ehCacheManager = cacheCacheManager.getCacheManager();
    net.sf.ehcache.Cache ehCache = ehCacheManager.getCache("userCache");
1
2
3
4

# 结束语

至于如何使用CacheManger的注解这里不再赘述,网上有大把的资料供大家参考了。

编辑
#Spring#redis#Cache
上次更新: 2020/12/31, 08:07:48
SpringBoot如何获取Bean列表
Spring不能将包含key值为null的map集合转换成JSON

← SpringBoot如何获取Bean列表 Spring不能将包含key值为null的map集合转换成JSON→

最近更新
01
IDEA行号太宽
03-11
02
uniapp中实现h5扫码功能(微信版)
08-12
03
Docker安装Rabbitmq
07-22
更多文章>
本站总访问量次 | 您是本站第位访问者
Theme by Vdoing | Copyright © 2020-2024 Lake Liu | MIT License | 背景图、Logo、头像设计@Drrizzee
  • 跟随系统
  • 深色模式
  • 浅色模式
  • 阅读模式