Java EHCache刷新
在EHCache中,是否有一种方法可以实现某种db侦听器,如果数据不同步,cahce将自动更新?(例如,一旦用户请求数据,cahce就会检查数据是否不同步,如果是……更新自身并返回数据,如果不是……从缓存返回数据)如果有人能告诉我规范的哪一部分突出了此用途,那就太棒了 目标是始终向用户提供最新的数据。因此,我猜定时刷新机制不会起作用,因为数据随时都可能更改 在我的案例中,EHCAche不是必须使用的,所以任何满足这一点的机制都是最受欢迎的Java EHCache刷新,java,ehcache,Java,Ehcache,在EHCache中,是否有一种方法可以实现某种db侦听器,如果数据不同步,cahce将自动更新?(例如,一旦用户请求数据,cahce就会检查数据是否不同步,如果是……更新自身并返回数据,如果不是……从缓存返回数据)如果有人能告诉我规范的哪一部分突出了此用途,那就太棒了 目标是始终向用户提供最新的数据。因此,我猜定时刷新机制不会起作用,因为数据随时都可能更改 在我的案例中,EHCAche不是必须使用的,所以任何满足这一点的机制都是最受欢迎的 谢谢 对于EhCache,我相信您正在寻找。如果您不想执
谢谢 对于EhCache,我相信您正在寻找。如果您不想执行定时刷新(即使这是一个简单的解决方案),那么触发器或基于消息总线的更新将是一个不错的选择。一旦触发建立,您可以执行一些统计并查看更新频率,然后切换到具有足够频率的定时更新以满足要求。我使用ehcache spring注释实现了这一点。这些是我在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>
org.springframework
弹簧芯
3.0.5.1发布
net.sf.ehcache
ehcache内核
2.2.0
com.googlecode.ehcache-spring-annotations
ehcache-spring注释
1.2.0-M1
@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);
}
...
}
package com.company.project.dao;
导入java.util.List;
导入net.sf.ehcache.CacheManager;
导入net.sf.ehcache.ehcache;
导入org.hibernate.SessionFactory;
导入org.springframework.beans.factory.FactoryBean;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.stereotype.Component;
导入org.springframework.transaction.annotation.Propagation;
导入org.springframework.transaction.annotation.Transactional;
导入com.company.project.domain.Parent;
导入com.company.project.domain.Child;
导入com.googlecode.ehcache.annotations.Cacheable;
导入com.googlecode.ehcache.annotations.KeyGenerator;
导入com.googlecode.ehcache.annotations.Property;
@组件(“示例”)
公共类EhcacheExample{
@自动连线
@限定词(“ehCacheManager”)
私人工厂经理;
public void createParen(父级){
cleanCache(父级);
创建(父级);
}
专用void cleanCache(父级){
试一试{
CacheManager CacheManager=ehCacheManager.getObject();
Ehcache-Ehcache=cacheManager.getEhcache(“myCache”);
remove(parent.getChild().hashCode());
}捕获(例外e){
e、 printStackTrace();
}
}
@可缓存
(cacheName=“myCache”,
keyGenerator=@keyGenerator(
name=“com.company.project.util.ChildCacheKeyGenerator”,
properties=@Property(name=“includeMethod”,value=“false”)
)
)
公共列表getParentsByChild(子项){
返回。。。;
}
@凌驾
公共无效deleteParentById(长id){
父项=findById(id);
cleanCache(父级);
删除(母公司);
}
...
}
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;
}
}
package com.company.project.util;
导入java.io.Serializable;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入com.company.project.domain.Child;
导入com.googlecode.ehcache.annotations.key.AbstractCacheKeyGenerator;
公共类ChildCacheKeyGenerator扩展了AbstractCacheKeyGenerator{
Logger Logger=LoggerFactory.getLogger(this.getClass());
@凌驾
公共可序列化generateKey(对象…数据){
if(子级的数据[0]实例){
子项=(子项)数据[0];
返回child.hashCode();
}
新的IllegalArgumentException();
返回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>
我希望它有用。这是工作链接