服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - springboot缓存的使用实践

springboot缓存的使用实践

2021-05-07 11:06luckyxl029 Java教程

这篇文章主要介绍了springboot缓存的使用,spring针对各种缓存实现,抽象出了CacheManager接口,用户使用该接口处理缓存,而无需关心底层实现,感兴趣的小伙伴们可以参考一下

spring针对各种缓存实现,抽象出了cachemanager接口,用户使用该接口处理缓存,而无需关心底层实现。并且也可以方便的更改缓存的具体实现,而不用修改业务代码。下面对于在springboot中使用缓存做一简单介绍:

1、添加依赖

?
1
2
3
4
<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-cache</artifactid>
</dependency>

2、在配置类里开启缓存,如下图所示:

springboot缓存的使用实践

3、在需要使用缓存的方法上加上注解,如下:

?
1
2
3
4
5
6
7
8
@override
  //@cacheput 该注解会将方法的返回值缓存起来,其中缓存名字是 people,数据的key是person的id
  @cacheput(value = "people", key = "#person.id")
  public person save(person person) {
    person p = personrepository.save(person);
    system.out.println("为id、key为:"+p.getid()+"数据做了缓存");
    return p;
  }
?
1
2
3
4
5
6
7
@override
  //@cacheevict 该注解会删除people缓存中key为id 的数据
  @cacheevict(value = "people", key = "#id")
  public void remove(long id) {
    system.out.println("删除了id、key为"+id+"的数据缓存");
    //这里不做实际删除操作
  }
?
1
2
3
4
5
6
7
8
9
@override
  //@cacheable 该注解会在方法执行时,判断缓存people中key为#person.id
的缓存是否存在,如果存在,则直接返回缓存中的数据。如果不存在,则会查数据库,然后将返回结果缓存起来。
  @cacheable(value = "people", key = "#person.id")
  public person findone(person person) {
    person p = personrepository.findone(person.getid());
    system.out.println("为id、key为:"+p.getid()+"数据做了缓存");
    return p;
  }

以上几部就完成了缓存,但是现在的缓存是默认的基于内存的,没有实现持久化。下面以redis作为缓存的具体实现,如下:

4、添加依赖

?
1
2
3
4
<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-redis</artifactid>
</dependency>

5、在配置文件里添加redis配置

?
1
2
redis.hostname=localhost
redis.port=6379

6、在spring容器中配置redis

?
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
@configuration
public class redisconfig extends cachingconfigurersupport{
  private static final logger logger = loggerfactory.getlogger(redisconfig.class);
 
  @autowired
  private environment env;
 
  @bean
  public jedisconnectionfactory redisconnectionfactory() {
    jedisconnectionfactory redisconnectionfactory = new jedisconnectionfactory();
    redisconnectionfactory.sethostname(env.getproperty("redis.hostname"));
    redisconnectionfactory.setport(integer.parseint(env.getproperty("redis.port")));
    return redisconnectionfactory;
  }
 
  @bean
  public redistemplate<string, string> redistemplate(redisconnectionfactory cf) {
    redistemplate<string, string> redistemplate = new redistemplate<>();
    redistemplate.setconnectionfactory(cf);
    return redistemplate;
  }
 
  @bean
  public cachemanager cachemanager(redistemplate<?, ?> redistemplate) {
    rediscachemanager cachemanager = new rediscachemanager(redistemplate);
    cachemanager.setdefaultexpiration(600);
    return cachemanager;
  }
   
}

ok,完成了,其他什么都不用改,是不是很方便?

另外,要缓存的类必须序列化。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/luckyxl029/article/details/79420334

延伸 · 阅读

精彩推荐