springcache:spring-boot-starter-data-redis 中注解的使用:

首先,application使用@EnableCaching注解。

@EnableCaching
@SpringBootApplication
public class GatheringApplication {

	public static void main(String[] args) {
		SpringApplication.run(GatheringApplication.class, args);
	}

	@Bean
	public IdWorker idWorkker(){
		return new IdWorker(1, 1);
	}
	
}

第二、在service find系列方法中增加缓存。使用@Cacheable注解。

/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	@Cacheable(value = "gathering",key="#id")//{"value::key":"{活动内容}"}
	public Gathering findById(String id) {
		return gatheringDao.findById(id).get();
	}

@Cacheable注解的作用是:

第三、在修改类service方法中使用@CacheEvict去除缓存。

/**
	 * 修改
	 * @param gathering
	 */
	@CacheEvict(value = "gathering",key = "gathering.id")
	public void update(Gathering gathering) {
		gatheringDao.save(gathering);
	}

就是这么简单!再也不用判空了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值