SpringBoot缓存机制

SpringBoot缓存机制

Spring框架支持透明地向应用程序添加缓存对缓存进行管理,其管理缓存的核心是将缓存应用于操作数据的方法(包括增删查改等),从而减少操作数据的执行次数(主要是查询,直接从缓存中读取数据),同时不会对程序本身造成任何干扰。

主要注解:

@EnableCaching

@Cacheable

@CacheEvict

@CachePut

更好的案例介绍请参考:(8条消息) SpringBoot 缓存 @Cacheable、@CachePut、@CacheEvict_cacheevict动态指定key_spring to do的博客-CSDN博客

1. @EnableCaching

SpringBoot继承了Spring框架的缓存管理功能,通过使用@EnableCaching注解开启基于注解的缓存支持,SpringBoot就可以启动缓存管理的自动化配置。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;


@EnableCaching  //开启缓存
@SpringBootApplication
public class BookMallApp {
    public static void main(String[] args) {
        SpringApplication.run(BookMallApp.class);
    }
}

2. @Cacheable

将数据存放在Spring缓存中,在下次执行方法时,查询Spring Cache中是否已有缓存,有则不执行方法,否则执行方法,并将结果存储到Spring Cache中。

在本质上@Cache维护着一个Map<Object,Object>

  @Cacheable(cacheNames="Publisher")
    @Override
    public List<Publisher> findAll() {
        return iPublisherDao.selectList(null);
    }

cacheNames:用来指定缓存组件的名字,将方法的返回结果放在哪个缓存中,可以是数组的方式,支持指定多个缓存。

测试:

 

 由上面的图对比可以看出,第一次查询时缓存中没有数据,得去数据库中查询,所花费的时间较长,第二次查询时,缓存中有了数据,查询时直接从缓存中查查询所花费的时间明显比第一次所花费的时间少很多。

更详细介绍参考:

(8条消息) SpringBoot 缓存之 @Cacheable 详细介绍_zl1zl2zl3的博客-CSDN博客

2. @CacheEvict

能够根据一定的条件对缓存进行清空

3. @CachePut

主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用。

@CachePut既调用方法,又更新缓存数据,同步更新缓存,修改了数据库的某个数据,同时更新缓存。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用缓存可以显著提高应用程序的性能和响应速度。Spring Boot提供了对多种缓存技术的支持,包括EhCache、Redis、Caffeine等。下面是一个示例,演示如何在Spring Boot中使用EhCache。 1. 添加依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> ``` 2. 配置缓存: 在application.properties文件中添加以下配置: ```properties # 开启缓存 spring.cache.type=ehcache ``` 3. 创建缓存: 在你的服务类上添加@Cacheable注解,并指定缓存名称,如: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; @Cacheable(value = "users") @Override public User getUserById(Long id) { Optional<User> optionalUser = userRepository.findById(id); return optionalUser.orElse(null); } } ``` 4. 测试缓存: ```java @RunWith(SpringRunner.class) @SpringBootTest public class UserServiceTest { @Autowired private UserService userService; @Test public void testGetUserById() { Long id = 1L; User user = userService.getUserById(id); System.out.println(user); // 再次调用,应该从缓存中获取数据 User cachedUser = userService.getUserById(id); System.out.println(cachedUser); } } ``` 在第一次调用getUserById方法时,会从数据库中获取数据并添加到缓存中。在第二次调用时,应该从缓存中获取数据,而不是再次查询数据库。 以上是使用EhCache的示例,如果你想使用其他缓存技术,只需要在依赖中添加相应的库,并在application.properties文件中配置即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值