暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

springboot缓存

dean技术分享 2019-05-11
264

如果没有引入其他缓存依赖时,springboot默认使用ConcurrenMapCacheManager作为缓存管理器
本文介绍使用redis缓存

支持的缓存类型

  • Generic

  • JCache (JSR-107) (EhCache 3, Hazelcast, Infinispan, and others)

  • EhCache 2.x

  • Hazelcast

  • Infinispan

  • Couchbase

  • Redis

  • Caffeine

  • Simple
    也可以自己实现CacheManager


  1. 引入依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <!-- 缓存依赖 -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <!-- redis依赖,不引入则默认使用ConcurrenMapCacheManager -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>


  2. 修改配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    spring:
    redis:
    #地址
    host: localhost
    #端口
    port: 6379
    #索引库
    database: 1
    #密码
    password:
    #超时时间
    timeout: 5000ms
    cache:
    #设置缓存类型
    type: redis
    redis:
    #缓存存活时间,不设置则没有过期时间
    time-to-live: 1800000ms


  3. 开启缓存

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @SpringBootApplication
    //开启缓存
    @EnableCaching
    public class Cache8Application {

    public static void main(String[] args) {
    SpringApplication.run(Cache8Application.class, args);
    }

    }


  4. service

    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
    33
    34
    35
    36
    37
    38
    39
    40
    @Service
    public class CacheService {

    /**
    * 如果缓存中有则直接走缓存,如果没有则执行方法,将方法的返回值作为缓存
    * @param id
    * @return
    */
    @Cacheable(value = "cache-test", key = "'getName:' + #p0")
    public String getName(long id){
    System.out.println("等待3秒。。。。");
    try {
    Thread.sleep(3000L);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    return id + ":name";
    }

    /**
    * 将方法的返回值更新到缓存
    * @param id
    * @return
    */
    @CachePut(value = "cache-test", key = "'getName:' + #p0")
    public String updateName(long id){
    System.out.println("更新名称");
    return id + ":nickname";
    }

    /**
    * 删除缓存
    * @param id
    */
    @CacheEvict(value = "cache-test", key = "'getName:' + #p0")
    public void deleteName(long id){
    System.out.println("删除名称");
    }
    }
  • @Cacheable:获取缓存

    • value:缓存名称,必须有,spring根据value进行分组

    • key: 缓存的key,支持spel表达式,如果没有则按照方法的所有参数进行组合

  • @CachePut:更新缓存

    • value和key与@Cacheabl的value和key相同

  • @CacheEvict:删除缓存

    • value和key与@Cacheabl的value和key相同


  1. 测试

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class Cache8ApplicationTests {
    @Autowired
    private CacheService cacheService;

    @Test
    public void contextLoads() {
    System.out.println(cacheService.getName(1L));
    System.out.println(cacheService.getName(1L));
    cacheService.updateName(1L);
    System.out.println(cacheService.getName(1L));
    cacheService.deleteName(1L);
    System.out.println(cacheService.getName(1L));
    }

    }

    控制台输出:

    等待3秒。。。。
    1:name
    1:name
    更新名称
    1:nickname
    删除名称
    等待3秒。。。。
    1:name

    第一次获取name时缓存中没有数据,则直接走方法并将返回值存储到缓存。
    第二次获取name时缓存中有数据,跳过方法直接从缓存中获取数据。
    更新缓存。
    第三次获取name时缓存中的数据修改成了1:nickname。
    删除缓存。
    第四次获取name时缓存中没有数据,走方法并将返回值存储到缓存。


阅读原文有项目链接

文章转载自dean技术分享,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论