栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot自带的缓存@EnableCaching

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot自带的缓存@EnableCaching

一般使用springboot自带缓存时,直接就在启动类里添加注解@EnableCaching 。@EnableCaching她有两个经常使用的方法

1,@Cacheable添加缓存

  
   这里的value 是该缓存的名称,可以随意写,而key要严格按照查询条件来写,比如这里是查询条件id.

   @Cacheable(value = "gathering",key = "#id")
	public Gathering findById(String id) {
		return gatheringDao.findById(id).get();
	}

 查询数据库已有的数据,第一次缓存没有该数据,直接走数据库,然后存入缓存

 第二次查询该数据,发现缓存中存在key已有的数据,直接走缓存不走数据库

2、@CacheEvict 清理缓存 
   
	@CacheEvict(value = "gathering",key = "#id")
	public void deleteById(String id) {
		gatheringDao.deleteById(id);
	}

   
	@CacheEvict(value = "gathering",key = "#gathering.id")
	public void update(Gathering gathering) {
		gatheringDao.save(gathering);
	}

Redis可以设置过期时间,springboot自带的缓存不可以。

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356104.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号