Java EHCache 刷新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3971379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EHCache refresh
提问by Victor
In EHCache, is there a way to implement some kind of a db listener where the cahce will auto update if the data is out of sync? (e.g as soon as the user requests for data, the cahce checks if the data is out of sync, if yes...updates itself and returns the data, if not...just return the data from the cache)If someone can point me which part of the specification highlights this use, that would be awesome!
在 EHCache 中,有没有办法实现某种 db 侦听器,如果数据不同步,cahce 将自动更新?(例如,一旦用户请求数据,cahce 就会检查数据是否不同步,如果是...更新自身并返回数据,如果不是...就从缓存中返回数据)如果有人可以指出规范的哪一部分突出了这种用法,那太棒了!
The goal is to always provide the latest data to the user. So I am guessing a timed refresh mechanism will not do as the data can change anytime.
目标是始终向用户提供最新数据。所以我猜测定时刷新机制不会起作用,因为数据可以随时更改。
EHCAche is not mandatory to use in my case, so any mechanism that satisfies this will be most welcome...
在我的情况下,EHCAche 不是强制使用的,所以任何满足这一点的机制都将是最受欢迎的......
Thanks!!
谢谢!!
采纳答案by whatnick
For EhCache thisis what I believe you are looking for. If you would not like to do timed refresh (even though it is a simple solution), triggers or a message bus based update would be the way to go. You can perform some statistics and see update frequency once the triggering has been established and switch to a timed update with sufficient frequency to satisfy Nyquist.
对于 EhCache,这就是我相信您正在寻找的。如果您不想进行定时刷新(即使它是一个简单的解决方案),那么触发器或基于消息总线的更新将是您的最佳选择。您可以执行一些统计数据并在触发建立后查看更新频率,然后切换到具有足够频率的定时更新以满足Nyquist 要求。
回答by Daniel
I did that using ehcache-spring-annotations. These are my dependencies in the maven pom.xml
我使用 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 works, but unfortunately @TriggersRemove doesn't work. The workaround is to invalidate the cache manually. Here is my example of usage:
@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);
}
...
}
The KeyGenerator implementation can be:
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;
}
}
In the Spring configuration:
在 Spring 配置中:
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >
<property name="configLocation" value="classpath:config/ehcache-methods.xml"/>
</bean>
ehcache-methods.xml:
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>
I hope it is useful.
我希望它有用。