我的项目需要将 SQL DB 中的一些数据保存在缓存中以满足业务服务需求。这些数据就像可以由管理应用程序更改的系统参数。
规范告诉我,其中一些数据需要每 2 分钟刷新一次。
我使用 Spring 3 + Hibernate 3.6 + ehcache 来做到这一点。
对于 2 分钟刷新要求,我已经像这样配置了我的 ehcache.xml:
<cache name="xxx.yyy.zzz.domain.Parameter"
maxEntriesLocalHeap="1000" eternal="false" timeToIdleSeconds="0"
timeToLiveSeconds="120" statistics="true">
<cacheEventListenerFactory class="xxx.yyy.zzz.listner.CacheListnerFactory" properties="bean=myCacheEventListner" listenFor="all"/>
<persistence strategy="localTempSwap" />
</cache>
当这个缓存过期时,我的监听器通知方法会被调用。很好。但从医生。ehcache,我读到删除块,直到通知方法返回!!!!我想使用这个通知方法来回调数据库并重新加载缓存中的数据!。
如何使用 cacheEventListener 实现此行为。
这是我的监听器代码:
@Override
public void notifyElementExpired(Ehcache arg0, Element arg1) {
log.info("CACHE EXPIRED : " + arg0.getName());
log.info("CACHE EXPIRED ELEMENT: " + arg1);
log.info("RELOADING CACHE");
List<Parameter> params = servParam.getAllParameter();
for (Parameter p : params) {
servParam.getParameter(p.getCodeParam(), p.getCodeMarche());
}
}
这是我的junit日志的一部分:
<ENTER testGetParameterExpired>
<enter - get parameters - code param = PAIEMENT_FACTURE_DELAI_MIN code marche = P>
<CACHE EXPIRED : [ name = auport.commun.shared.domain.Parameter status = STATUS_ALIVE eternal = false overflowToDisk = true maxEntriesLocalHeap = 1000 maxEntriesLocalDisk = 0 memoryStoreEvictionPolicy = LRU timeToLiveSeconds = 5 timeToIdleSeconds = 0 persistence = LOCALTEMPSWAP diskExpiryThreadIntervalSeconds = 120 cacheEventListeners: auport.commun.shared.listner.MyCacheEventListner net.sf.ehcache.statistics.LiveCacheStatisticsWrapper hitCount = 2 memoryStoreHitCount = 2 diskStoreHitCount = 0 missCountNotFound = 5 missCountExpired = 1 maxBytesLocalHeap = 0 overflowToOffHeap = false maxBytesLocalOffHeap = 0 maxBytesLocalDisk = 0 pinned = false ]>
<CACHE EXPIRED ELEMENT: [ key = sql: select parameter0_.cod_paap as cod1_0_, parameter0_.cod_mrch as cod2_0_, parameter0_.TMS_MAJ as TMS3_0_, parameter0_.NOM_ANGL_PAAP as NOM4_0_, parameter0_.nom_fran_paap as nom5_0_, parameter0_.NOM_UTLR_MAJ as NOM6_0_, parameter0_.NUM_UTLR_MAJ as NUM7_0_, parameter0_.PRN_UTLR_MAJ as PRN8_0_, parameter0_.VAL_PAAP as VAL9_0_ from TABCOMMUN.TPAAP parameter0_ where parameter0_.cod_paap=? and parameter0_.cod_mrch=?; parameters: PAIEMENT_FACTURE_DELAI_MIN, P, ; named parameters: {}, value=[5519186315907073, [Ljava.lang.Object;@1f6d2e3], version=1, hitCount=1, CreationTime = 1347457596657, LastAccessTime = 1347457596750 ]>
<RELOADING CACHE>
<enter - get all parameters>
<CACHE EXPIRED : [ name = auport.commun.shared.domain.Parameter status = STATUS_ALIVE eternal = false overflowToDisk = true maxEntriesLocalHeap = 1000 maxEntriesLocalDisk = 0 memoryStoreEvictionPolicy = LRU timeToLiveSeconds = 5 timeToIdleSeconds = 0 persistence = LOCALTEMPSWAP diskExpiryThreadIntervalSeconds = 120 cacheEventListeners: auport.commun.shared.listner.MyCacheEventListner net.sf.ehcache.statistics.LiveCacheStatisticsWrapper hitCount = 2 memoryStoreHitCount = 2 diskStoreHitCount = 0 missCountNotFound = 6 missCountExpired = 2 maxBytesLocalHeap = 0 overflowToOffHeap = false maxBytesLocalOffHeap = 0 maxBytesLocalDisk = 0 pinned = false ]>
<CACHE EXPIRED ELEMENT: [ key = sql: select parameter0_.cod_paap as cod1_0_, parameter0_.cod_mrch as cod2_0_, parameter0_.TMS_MAJ as TMS3_0_, parameter0_.NOM_ANGL_PAAP as NOM4_0_, parameter0_.nom_fran_paap as nom5_0_, parameter0_.NOM_UTLR_MAJ as NOM6_0_, parameter0_.NUM_UTLR_MAJ as NUM7_0_, parameter0_.PRN_UTLR_MAJ as PRN8_0_, parameter0_.VAL_PAAP as VAL9_0_ from TABCOMMUN.TPAAP parameter0_; parameters: ; named parameters: {}, value=[5519186315202560, [Ljava.lang.Object;@1286b10, [Ljava.lang.Object;@8d2280, [Ljava.lang.Object;@1d4340c, [Ljava.lang.Object;@a51027, [Ljava.lang.Object;@c7833c, [Ljava.lang.Object;@790192, [Ljava.lang.Object;@5646a5, [Ljava.lang.Object;@381a9c, [Ljava.lang.Object;@15b011c], version=1, hitCount=0, CreationTime = 1347457596485, LastAccessTime = 1347457596485 ]>
<RELOADING CACHE>
<enter - get all parameters>
...
如您所见,调用getParemeters触发过期事件,然后调用getAllParameter刷新缓存但是这个调用重新触发过期事件......所以刷新缓存的过程完成了两次!
提前感谢您的帮助。