服务器之家

服务器之家 > 正文

springboot缓存的使用实践

时间:2021-05-07 11:06     来源/作者:luckyxl029

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

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
返回顶部