springboot Ehcache使用

复制代码
1.Ehcache简单说明及使用

EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。
Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。
Ehcache最初是由Greg Luck于2003年开始开发。2009年,该项目被Terracotta购买。软件仍然是开源,但一些新的主要功能(例如,快速可重启性之间的一致性 的)只能在商业产品中使用,例如Enterprise EHCache and BigMemory。,维基媒体Foundationannounced目前使用的就是Ehcache技术。

Ehcache2.6.6 源码和jar包下载地址:https://oss.sonatype.org/content/repositories/sourceforge-releases/net/sf/ehcache/ehcache-core/2.6.6/


2.1 添加依赖
```xml
<!-- 缓存 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
```

2.2 加入注解 @EnableCaching
```java
@SpringBootApplication
@EntityScan(basePackages ={ "entity","com.holworth.domain"})
@EnableCaching
public class domainApplication {}
```
2.3 EhCache配置
在src\main\resources目录下,添加ehcache.xml文件,
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<ehcache>
<cache name="objectCache" maxElementsInMemory="10000"></cache>
</ehcache>
```
2.4 application.application配置
# 配置ehcache缓存
spring.cache.type=ehcache
# 指定ehcache配置文件路径
spring.cache.ehcache.config=classpath:/ehcache.xml

2.5
```java
package com.holworth.domain;

import entity.BasCurrency;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.EnableCaching;
import util.HraUtility;
import org.springframework.cache.CacheManager;

import javax.annotation.Resource;
import java.util.List;

@SpringBootApplication
@EntityScan(basePackages ={ "entity","com.holworth.domain"})
@EnableCaching
public class domainApplication {
@Resource
private CacheManager cacheManager;
// @Autowired
// private static com.holworth.domain.dao.a commonService;

public static void main(String[] args) {
//http://localhost:8086/MKT/MktRiskPriceManage.html
//http://localhost:8080/api/RiskFactor/GetRiskFactor
SpringApplication.run(domainApplication.class);
CacheManager cacheManager = (CacheManager) SpringContextUtils.getBean("CacheManager");
Cache cache = cacheManager.getCache("objectCache");
cache.put("key", "123");
System.out.println("缓存成功");
String res = cache.get("key", String.class);
System.out.println(res);
}
}
```
复制代码

 

posted on   听哥哥的话  阅读(411)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示