带有注释的 EhCache 配置 - 休眠



有没有办法使用注释来配置EHcache。

我有一个启用了缓存的 Spring/Hibernate 项目。目前,我正在使用 ehcache.xml 来定义如何缓存实体的配置。

这是我的 ehcache.xml 的样子:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
dynamicConfig="true">
<diskStore path="java.io.tmpdir/ehcache"/>
<defaultCache
.
.
.
</defaultCache>
<cache name="exmaple.model.User" maxEntriesLocalHeap="1000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="300">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>

我想知道是否可以在@Entity之上使用注释而不是使用此 xml 文件来做同样的事情。

有两个部分。一种是Spring Cache和Hibernate。它们都提供注释来告知应缓存方法或实体。

一个例子是从 Spring 缓存@Cacheable

然后,你有ehcache.xml.这是为了配置缓存本身。它不会通过注释。但它可以通过编程方式进行。Ehcache 3 使使用构建器变得容易。您将在此处看到一个示例。此示例还使用 Spring 缓存和休眠二级缓存。

Ehcache 2(您正在使用的(没有构建器。您需要调用CacheManager.newInstance(Configuration)并将所需的任何配置放入其中。可悲的是,我手头没有一个例子。

最新更新