使用SpringCache进行缓存数据库查询


1、在SpringBoot的启动类上添加注解@EnableCaching,开启SpringCache缓存支持

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

2、在service的方法上添加对应的注解

/** * 根据ID查询 * * @param id * @return */
// 使用SpringCache进行缓存数据库查询
@Cacheable(value = "gathering", key = "#id")
public Gathering findById(String id) {
	return gatheringDao.findById(id).get();
}
/** * 修改 * * @param gathering */
// 修改数据库数据后需要删除redis中的缓存
@CacheEvict(value = "gathering", key = "#gathering.id")
public void update(Gathering gathering) {
	gatheringDao.save(gathering);
}

/** * 删除 * * @param id */
// 删除数据库数据后需要删除redis中的缓存
@CacheEvict(value = "gathering", key = "#id")
public void deleteById(String id) {
	gatheringDao.deleteById(id);
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM