java - Share Hibernate Second-Level Cache - 多个应用程序可以共享同一个缓存吗
问题描述
我有两个应用程序使用具有相同架构的相同数据库。应用程序 A 将读写表 A。应用程序 B 将仅读取表 A。
如果我在两个应用程序中都配置了二级缓存。应用程序 B 不会检索应用程序 A 之前更新了表 A 中的记录的更新值。
我想配置相同的休眠配置以与多个应用程序共享相同的缓存(在这种情况下,应用程序 B 可以获得更新的值)
我读了一些stackoverflow,但仍然不适合我。
我正在使用休眠 5 并使用 Ehcache 进行缓存管理。
知道如何实现吗?我可以参考一下吗?
这是我的两个应用程序中的一些休眠属性
冲刺休眠配置:
<bean id="mySessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
...
</list>
</property>
<property name="hibernateProperties">
<props>
<!-- Debug -->
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<!-- Level two caching -->
<prop key="hibernate.cache.provider_class">=net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="net.sf.ehcache.configurationResourceName">../conf/ehcache.xml</prop>
</property>
</bean>
ehcache.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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"/>
<!-- Default cache settings -->
<defaultCache
maxElementsInMemory="100000"
eternal="true"
timeToIdleSeconds="300"
timeToLiveSeconds="300"
overflowToDisk="false"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="300"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
解决方案
您可以考虑使用像 Hazelcast 这样的分布式缓存。事实上,Hazelcast 内置了与 Hibernate 的集成,可以轻松插入二级缓存
推荐阅读
- angular - 如何将 value.lng 分配给 [纬度] 和 [经度]?
- javascript - POST http://localhost:3000/updateSurvey 400(错误请求)
- sql - SQL Server:多列子查询
- mongodb - 带有pymongo的多列mongodb中的多索引与单索引
- r - `ip_api()` 函数中丢失的 IP 地址
- html - 将子宽度设置为父内部宽度(滚动宽度/溢出滚动)
- ios - Safari 共享扩展 - 自 iOS 14 起,应用程序仅在第二次尝试后才出现在共享菜单中
- javascript - Slike 滑块隐藏/显示带有自定义类的幻灯片
- javascript - 按下回车按钮时触发显示密码功能(不应该这样做时)
- android - 如何到达foreach中的下一个对象