在EHCache中,有沒有一種方法可以實現某種db偵聽器,如果數據不同步,cahce會自動更新? (例如,只要用戶請求數據,cahce會檢查數據是否不同步,如果是,則更新自身並返回數據,如果不是,則僅從數據緩存中返回數據)如果有人可以指出規範的哪一部分突出顯示了這種用法,那將會很棒!EHCache刷新
目標是始終向用戶提供最新的數據。所以我猜測定時刷新機制不會做,因爲數據可以隨時更改。
的Ehcache不是強制性的在我的情況下使用,因此任何機制,滿足這將是最歡迎...
謝謝!
在EHCache中,有沒有一種方法可以實現某種db偵聽器,如果數據不同步,cahce會自動更新? (例如,只要用戶請求數據,cahce會檢查數據是否不同步,如果是,則更新自身並返回數據,如果不是,則僅從數據緩存中返回數據)如果有人可以指出規範的哪一部分突出顯示了這種用法,那將會很棒!EHCache刷新
目標是始終向用戶提供最新的數據。所以我猜測定時刷新機制不會做,因爲數據可以隨時更改。
的Ehcache不是強制性的在我的情況下使用,因此任何機制,滿足這將是最歡迎...
謝謝!
我做到了使用ehcache-spring-annotations。以上是我的依賴在Maven的pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.googlecode.ehcache-spring-annotations</groupId>
<artifactId>ehcache-spring-annotations</artifactId>
<version>1.2.0-M1</version>
</dependency>
@Cacheable作品,但遺憾的是@TriggersRemove不起作用。解決方法是手動使緩存無效。 這是我使用的例子:
package com.company.project.dao;
import java.util.List;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.company.project.domain.Parent;
import com.company.project.domain.Child;
import com.googlecode.ehcache.annotations.Cacheable;
import com.googlecode.ehcache.annotations.KeyGenerator;
import com.googlecode.ehcache.annotations.Property;
@Component("Example")
public class EhcacheExample {
@Autowired
@Qualifier("ehCacheManager")
private FactoryBean<CacheManager> ehCacheManager;
public void createParen(Parent parent) {
cleanCache(parent);
create(parent);
}
private void cleanCache(Parent parent) {
try {
CacheManager cacheManager = ehCacheManager.getObject();
Ehcache ehcache = cacheManager.getEhcache("myCache");
ehcache.remove(parent.getChild().hashCode());
} catch (Exception e) {
e.printStackTrace();
}
}
@Cacheable
( cacheName = "myCache",
keyGenerator = @KeyGenerator (
name = "com.company.project.util.ChildCacheKeyGenerator",
properties = @Property(name="includeMethod", value="false")
)
)
public List<SerieRecording> getParentsByChild(Child child) {
return ...;
}
@Override
public void deleteParentById(long id) {
Parent parent = findById(id);
cleanCache(parent);
delete(parent);
}
...
}
的實現的KeyGenerator可以是:
package com.company.project.util;
import java.io.Serializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.company.project.domain.Child;
import com.googlecode.ehcache.annotations.key.AbstractCacheKeyGenerator;
public class ChildCacheKeyGenerator extends AbstractCacheKeyGenerator<Serializable> {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public Serializable generateKey(Object... data) {
if (data[0] instanceof Child) {
Child child = (Child)data[0];
return child.hashCode();
}
new IllegalArgumentException();
return null;
}
}
在Spring配置:
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >
<property name="configLocation" value="classpath:config/ehcache-methods.xml"/>
</bean>
的Ehcache-methods.xml:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<cache name="myCache" eternal="false"
maxElementsInMemory="12600" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="1800"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
我希望它是有用的。
不幸的是,鏈接不再工作。 – emanciperingsivraren 2016-08-11 12:03:18
這是工作鏈接http://www.ehcache.org/documentation/2.8/recipes/expiration.html – ChainLooper 2017-03-14 15:45:55