SpringBoot Ehcache初步使用

本文介绍了如何在SpringBoot项目中使用Ehcache作为缓存工具,包括导入Ehcache jar包,启用缓存功能,配置Ehcache,创建ehcache.xml配置文件,使用@Cacheable和@CacheEvict注解实现缓存操作,并解决因key生成问题可能导致的错误。通过重写KeyGenerator接口确保缓存的正确性。
摘要由CSDN通过智能技术生成

由于缓存是在单机使用的,因此根据文章https://www.cnblogs.com/qlqwjy/p/7788912.html的提示,使用的Ehcache,而不使用Redis作为缓存工具。

第一步,导入jar包。

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
		</dependency>
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache</artifactId>
		</dependency>

第二步,让SpringBoot知道你打算用缓存了,即增加EnableCaching注解。

@SpringBootApplication
@EnableCaching
public class ManualSpringbootApplication {
	public static void main(String[] args) {
		SpringApplication.run(ManualSpringbootApplication.class, args);
	}
}

第三步,让spring知道你想用的是Ehcache缓存,而不是其他的缓存,在springboot的配置文件中增加缓存配置。

########
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值