在多个 Tomcat Web 应用程序之间共享本地 ehcache
我有 2 个不同的 Web 应用程序在同一个 Tomcat 6 实例中运行。两者共享一个使用 hibernate 进行持久化的库。我想使用 ehcache 启用 Hibernate 二级缓存,但我不希望每个 web 应用程序都有自己的缓存。
知道我如何实现这个吗?我将 ehcache-core 安装到 $CATALINA_HOME/lib 中,每个 spring 应用程序都将 hibernate 配置为使用 ehcache,如下所示:
<property name="hibernateProperties">
<props>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</prop>
</props>
</property>
两个应用程序都使用 ehcache,但每个应用程序仍然有自己不同的缓存(修改一个应用程序中的一项,并且过时的值仍然出现)在另一个应用程序中)
我的 ehcache.xml (也在 $CATALINA_HOME/lib 中)如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
name="mySharedCache"
updateCheck="false" monitoring="autodetect"
dynamicConfig="false">
<diskStore path="java.io.tmpdir"/>
<defaultCache maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120">
</defaultCache
</ehcache>
更多详细信息:
- Tomcat 6
- Spring 3.0.5
- Hibernate 3.6.5
- EhCache 2.4.5
I have 2 different webapps running in the same Tomcat 6 instance. Both share a library which uses hibernate for persistence. I want to enable Hibernate 2nd level caching with ehcache, but I don't want each of the webapps to have its own cache.
Any idea how I might implement this? I installed ehcache-core into $CATALINA_HOME/lib and each spring application is configuring hibernate to use ehcache like this:
<property name="hibernateProperties">
<props>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</prop>
</props>
</property>
Both apps are using ehcache, but each still has its own distinct cache (modify an item in one app and the stale value still appears in the other app)
My ehcache.xml (also in $CATALINA_HOME/lib) looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
name="mySharedCache"
updateCheck="false" monitoring="autodetect"
dynamicConfig="false">
<diskStore path="java.io.tmpdir"/>
<defaultCache maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120">
</defaultCache
</ehcache>
More details:
- Tomcat 6
- Spring 3.0.5
- Hibernate 3.6.5
- EhCache 2.4.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以配置 EhCache 以在集群环境中使用。每个应用程序都有自己的缓存,但对一个缓存的更改会复制到集群中的其他应用程序。要使用 JGroups 来实现此目的,您可以将如下内容添加到 ehcache.xml 中:
然后在 defaultcache 元素中添加以下内容:
我从 Java Persistence with Hibernate 的一章中了解了这一点,我建议您阅读该章节。它说上面的例子来自EhCache文档。
如果您不希望每个应用程序都有自己的缓存,请阅读 Infinispan。
It is possible to configure EhCache for use in clustered environment. Each app will have its own cache but changes to one cache are replicated to the others in the cluster. To use JGroups for this purpose you can add something like the following to your ehcache.xml:
Then add the following inside your defaultcache element:
I learnt about this from a chapter in Java Persistence with Hibernate which I would recommend reading. It says that the above example is from the EhCache documentation.
If you don't want each app to have its own cache then read up on Infinispan.