我正在迁移应用程序以使用 ehcache 3.10.0,但出现构建错误:无法在中央找到工件 javax.xml.bind:jaxb-api:pom:2.3.0-b161121.1438 ( https://repo1.1)。 maven.org/maven2)
我在本地 .m2 目录中看到该文件: .m2\repository\javax\xml\bind\jaxb-api -- 2.3.0-b161121.1438
所以这是一个 IDE 问题,为什么它在我的本地构建中看不到,因为它确实存在于我的本地 .m2 上,但这个版本 (2.3.0-b161121.1438) 在 Maven 上仍然不可用,https://repo1。 maven.org/maven2/javax/xml/bind/jaxb-api/
因此构建因该工件错误而失败。关于如何解决它有什么建议吗?
我正在尝试在 Spring Boot 2.2 中使用 Hibernate 配置 EHCache,但似乎我做错了什么。我查看了几个教程和 SO 问题,但没有找到与我的方法完全匹配的内容。
我为缓存选择了无 XML、jcache 配置的方法。但是,Hibernate 没有检测到现有的缓存管理器(我检查甚至强制执行@AutoconfigureBefore:缓存管理器在 Hibernate 自动配置之前加载)。结果,Hibernate 创建了第二个EhcacheManager并抛出了几个警告,例如:
HHH90001006: Missing cache[com.example.demo.one.dto.MyModel] was created on-the-fly. The created cache will use a provider-specific default configuration: make sure you defined one. You can disable this warning by setting 'hibernate.javax.cache.missing_cache_strategy' to 'create'.
Run Code Online (Sandbox Code Playgroud)
我尝试使用 aHibernatePropertiesCustomizer来告诉 Hibernate 它应该使用哪个缓存管理器。bean 已实例化,但从未被调用,因此它失去了所有吸引力和用途。
有人知道我做错了什么以及我应该如何让 Hibernate 使用我已经配置的缓存管理器而不是创建自己的缓存管理器吗?
我将我的配置与JHipster生成的配置进行了比较。它看起来非常相似,尽管它们HibernatePropertiesCustomizer 被称为。我没有成功确定他们的缓存配置和我的缓存配置之间的差异。
这似乎与我的数据源配置有关(请参阅下面的代码)。我尝试删除它并以更简单的方式启用我的 JPA 配置,并且HibernatePropertiesCustomizer确实按预期调用。
HHH90001006: Missing cache[com.example.demo.one.dto.MyModel] was …Run Code Online (Sandbox Code Playgroud) maxBytesLocalHeapEhcache 中有一个名为的配置2.x,我们可以在其中输入堆百分比值。我在 中找不到等效项,但发行说明或迁移指南中3.x没有关于弃用的内容。
有没有办法通过版本中的 JVM 堆百分比指定 Ehcache 堆大小3.x?
我正在使用 EHCache 3.5.2 并且在获取所有缓存键和缓存条目时遇到问题。
我正在使用 CacheManager 创建缓存。然后我用一些数据填充它。然后我想检索缓存中的所有条目。
一些示例代码:
Cache<String, Foo> cache = cacheManager.createCache("fooCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, Foo.class,
ResourcePoolsBuilder.heap(20)).build());
cache.putAll(repository.findAll().stream().collect(toMap(Foo::getId, foo -> foo)));
List<Foo> foos = cache.???
List<String> keys = cache.???
Run Code Online (Sandbox Code Playgroud)
v3.5 可以实现吗?似乎在旧版本的 EHCache 中是可能的。
谢谢
net.sf.ehcache和org.ehcache有什么区别?
net.sf.ehcache的当前版本为2.10.5,而org.ehcache的当前版本为3.5.2。
Spring使用net.sf.ehcache的CacheManager,而org.ehcache的CacheManager不兼容。
有什么具体原因吗?请解释。
ehcache 3.x 版本中需要将缓存保存到磁盘时如何指定磁盘路径。在 ehcache 2.x 版本中可以使用 指定<diskStore path="java.io.tmpdir/ehcache/" />,但在 3.x 版本中我没有找到任何等效的 xml 标记。
我尝试更新到 EhCache 3,但注意到我的 spring-security-acl 的 AclConfig 不再有效。原因是EhCacheBasedAclCache还在用import net.sf.ehcache.Ehcache。EhCacheorg.ehcache从第 3 版开始迁移,因此不再有效。spring 是否为 EhCache 3 提供了替换类,或者我是否需要实现自己的 Acl 缓存?这是代码,不再起作用:
@Bean
public EhCacheBasedAclCache aclCache() {
return new EhCacheBasedAclCache(aclEhCacheFactoryBean().getObject(),
permissionGrantingStrategy(), aclAuthorizationStrategy());
}
Run Code Online (Sandbox Code Playgroud) 我需要初始化需要包含的缓存
String in key
List<Object> in value
Run Code Online (Sandbox Code Playgroud)
所以我有 CacheHelper 类,其中有
public class CacheHelper {
private CacheManager cacheManager;
private Cache<String,List<Person>> cacheDataList;
private static final String CACHE_PERSON="cache_key_person";
public CacheHelper() {
}
public void putInCacheFromDb(){
System.getProperties().setProperty("java -Dnet.sf.ehcache.use.classic.lru", "true");
cacheManager= CacheManagerBuilder
.newCacheManagerBuilder().build();
cacheManager.init();
cacheDataList = cacheManager
.createCache("cacheOfPersonList", CacheConfigurationBuilder
.newCacheConfigurationBuilder(
String.class,Person.class,
ResourcePoolsBuilder.heap(10)).withExpiry(Expirations.timeToLiveExpiration(Duration.of(60,
TimeUnit.SECONDS))));
}
public void putInList(List<Person> personList){
System.out.println("putting list in cache");
cacheDataList.put(CACHE_PERSON,personList);
}
}
Run Code Online (Sandbox Code Playgroud)
但是在这行代码中,我无法将对象转换为列表 String.class,Person.class,它需要是 String.class,List:
cacheDataList = cacheManager
.createCache("cacheOfPersonList", CacheConfigurationBuilder
.newCacheConfigurationBuilder(
String.class,Person.class,
ResourcePoolsBuilder.heap(10)).withExpiry(Expirations.timeToLiveExpiration(Duration.of(60,
TimeUnit.SECONDS))));
Run Code Online (Sandbox Code Playgroud)
但我收到错误如下:
Incompatible types. Required Cache<String, List<Person>> but …Run Code Online (Sandbox Code Playgroud) 我使用 Springboot 2.1 和 spring data-jpa 使用@RepositoryRestResource进行持久化。我已经为我的 API 调用启用了缓存,并且它与@Cacheable配合得很好,但是现在我想为我的所有 JPA 实体启用二级缓存并且具有以下配置,但仍然对这些实体的任何查询都在触发休眠查询而不使用缓存. 请让我知道我缺少此实体缓存的内容。
Gradle依赖:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
implementation 'org.springframework.boot:spring-boot-starter-jersey'
implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'org.ehcache:ehcache:3.7.1'
implementation 'javax.cache:cache-api'
compile group: 'org.hibernate', name: 'hibernate-jcache', version: '5.3.10.Final'
runtimeOnly 'mysql:mysql-connector-java'
Run Code Online (Sandbox Code Playgroud)
}
应用程序属性
spring.datasource.url=jdbc:mysql://localhost:3306/mar_db
spring.datasource.username=root
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.MySQL57Dialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.jcache.JCacheRegionFactory
spring.jpa.properties.javax.persistence.sharedCache.mode=ENABLE_SELECTIVE
spring.cache.jcache.config=classpath:ehcache.xml
Run Code Online (Sandbox Code Playgroud)
缓存文件
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ehcache.org/v3"
xmlns:jsr107="http://www.ehcache.org/v3/jsr107">
<service>
<jsr107:defaults enable-statistics="true" />
</service>
<cache alias="readOnlyEntityData">
<key-type>java.lang.Object</key-type>
<expiry>
<ttl unit="minutes">360</ttl> …Run Code Online (Sandbox Code Playgroud) 我一直在尝试找到一种使用 Spring Boot 3 + Ehcache 3 + Hibernate 6 来实现二级缓存的方法,但到目前为止还没有成功。
我尝试在互联网上查找它,但没有教程。也许我是第一个?
编辑:基本上问题在于依赖关系。Spring Boot 3 需要 Jakarta,但 Ehcache 使用 Javax。EhCache 3 还存在许多不可用的依赖项。将所有内容强制放在一起是行不通的。
有人可以帮忙吗?