Spring Boot 作为主流微服务框架,拥有成熟的社区生态。市场应用广泛,为了方便大家,整理了一个基于spring boot的常用中间件快速集成入门系列手册,涉及RPC、缓存、消息队列、分库分表、注册中心、分布式配置等常用开源组件,大概有几十篇文章,陆续会开放出来,感兴趣同学请提前关注&收藏
EhCache 从 Hibernate 发展而来,是一个纯Java的进程内缓存框架,具有快速、精干等特点。Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。
主要特性:
在 pom.xml 文件中添加 Ehcache 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
不需要配置version,SpringBoot的根pom已经对版本号做了统一声明!
配置文件:
在配置文件 application.yaml
中配置 ehcache 的相关参数,具体内容如下:
spring:
application:
name: spring-boot-bulking-ehcache
cache:
type: ehcache
ehcache:
config: classpath:/ehcache.xml
spring.cache.type 声明spring框架使用哪一种类型的缓存,因为spring框架提供了多种缓存可供选择。
添加 Ehcache 配置:
在src/main/resources
目录下,创建配置文件ehcache.xml
,内容如下:
<ehcache name="test">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxEntriesLocalHeap="1000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
<cache name="userCache"
maxEntriesLocalHeap="200"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true">
</cache>
</ehcache>
参数含义:
开启缓存:
入口启动类添加注解 @EnableCaching
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class})
@EnableCaching // 开启缓存,Spring Boot 会自动配置缓存的 CacheManager
public class StartApplication {
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
}
缓存业务使用:
@Component
@CacheConfig(cacheNames = "userCache")
public class UserService {
@Cacheable(key = "#id")
public User getUserById(Long id) {
System.out.println("缓存中无值");
User user = User.builder().id(id).userName("雪糕(" + id + ")").age(18).address("杭州").build();
return user;
}
@CachePut(key = "#user.id")
public User updateUser(User user) {
user.setUserName("雪糕(new name)");
return user;
}
@CacheEvict(key = "#id")
public void deleteById(Long id) {
System.out.println("db 删除数据,id=" + id);
}
}
除了采用 @Cacheable
、@CachePut
等方法注解解耦式操作缓存外,我们也可以使用 CacheManager
显示方式手动来操作缓存。
Spring定义了CacheManager
和Cache
接口统一不同的缓存技术。其中CacheManager
是Spring提供的各种缓存技术的抽象接口,而Cache
接口包含缓存的读、写、删等各种操作。
针对不同的缓存技术,需要实现不同的CacheManager
,Spring预先定义了主流缓存框架的cacheManger实现类
CacheManager | 描述 |
---|---|
SimpleCacheManager | 使用简单的Collection来存储缓存,主要用于测试 |
ConcurrentMapCacheManager | 使用ConcurrentMap作为缓存技术(默认) |
NoOpCacheManager | 测试用 |
EhCacheCacheManager | 使用EhCache作为缓存技术,以前在hibernate的时候经常用 |
GuavaCacheManager | 使用google guava的GuavaCache作为缓存技术 |
HazelcastCacheManager | 使用Hazelcast作为缓存技术 |
JCacheCacheManager | 使用JCache标准的实现作为缓存技术,如Apache Commons JCS |
RedisCacheManager | 使用Redis作为缓存技术 |
CaffeineCacheManager | 使用Caffeine作为缓存技术 |
Spring Boot 为我们预留接口扩展,方便我们自动配置 EhCache、Redis、Guava、ConcurrentMap等缓存,默认使用ConcurrentMapCacheManager。Spring Boot的
application.yaml
配置文件,使用spring.cache
前缀属性进行配置。
本文我们使用 EhCache
缓存,代码示例如下:
@Component
public class UserCacheManager {
@Resource
private CacheManager cacheManager;
public User getUserById(Long id) {
Cache cache = cacheManager.getCache("userCache");
User user = cache.get(id, User.class);
if (user == null) {
System.out.println("缓存中无值");
user = User.builder().id(id).userName("雪糕(" + id + ")").age(18).address("杭州").build();
cache.put(id, user);
}
return user;
}
public User updateUser(User user) {
user.setUserName("雪糕(new name)");
Cache cache = cacheManager.getCache("userCache");
cache.put(user.getId(), user);
return user;
}
public void deleteById(Long id) {
Cache cache = cacheManager.getCache("userCache");
cache.evict(id);
System.out.println("db 删除数据,id=" + id);
}
}
https://github.com/aalansehaiyang/spring-boot-bulking
模块:spring-boot-bulking-ehcache
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有