springboot2.x版本中简单使用自带cache

首先需要在启动类中加注解
@EnableCaching开启缓存

@SpringBootApplication
@EnableCaching  //开启缓存

在service层加入缓存
@Cacheable

    @Override
    @Cacheable(cacheNames = "userDataCache",key = "#name")
    public List<UserEntity> getByName(String ame) 

在service层清空缓存
@CacheEvict

    @Override
    @CacheEvict(cacheNames={
    
    "userDataCache"},key = "#name")
    public void  clearCache(String name){
    
    
        System.out.println("success");
    }

在控制层调用对应的加入或清空缓存的方法即可

猜你喜欢

转载自blog.csdn.net/weixin_38323645/article/details/108740383