【发布时间】:2015-01-12 04:12:12
【问题描述】:
尝试使用内联刷新实现我自己的缓存加载器并阅读以下文档:
http://terracotta.org/documentation/4.1/bigmemorymax/api/refresh-ahead#scheduled-refresh-ahead
&
但我不确定如何实现自己的缓存。在这个类中应该如何实现 createCacheLoader 方法:
public class MyCacheLoaderFactory extends net.sf.ehcache.loader.CacheLoaderFactory{
@Override
public CacheLoader createCacheLoader(Ehcache cache, Properties properties) {
// TODO Auto-generated method stub
return null;
}
}
要返回的CacheLoader是如何创建的,参数缓存和属性是如何配置的?
我已经使用下面的代码在 junit 中连接了缓存:
import java.util.Properties;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.loader.CacheLoader;
public class MyCacheLoaderFactory extends net.sf.ehcache.loader.CacheLoaderFactory{
@Override
public CacheLoader createCacheLoader(Ehcache cache, Properties properties) {
// TODO Auto-generated method stub
return null;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<defaultCache eternal="true" maxElementsInMemory="100"
overflowToDisk="false" />
<cache name="myCache" maxElementsInMemory="10000" eternal="true"
overflowToDisk="false">
<cacheLoaderFactory class="MyCacheLoaderFactory"
/>
<cacheDecoratorFactory
class="net.sf.ehcache.constructs.refreshahead.RefreshAheadCacheFactory"
properties="name=myCacheRefresher,
timeToRefreshSeconds=20,
batchSize=10,
numberOfThreads=4,
maximumBacklogItems=100,
evictOnLoadMiss=true" />
</cache>
</ehcache>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cache-manager-ref="ehcache" />
<bean id="ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="my-ehcache.xml" />
<bean id="myCache" class="ehcache.MyCache"></bean>
</beans>
测试类:
package ehcache;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-ehcache.xml")
public class TestEhcache {
@Test
public void testCache(){
while(true){
System.out.println(myCache.tester());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Autowired
private MyCache myCache;
}
package ehcache;
import org.springframework.cache.annotation.Cacheable;
public class MyCache {
@Cacheable(value = "myCache")
public String tester(){
System.out.println("in cache");
return "test";
}
}
当我运行测试 testCache 时,我收到异常:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in class path resource [spring-ehcache.xml]: Cannot resolve reference to bean 'ehcache' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehcache' defined in class path resource [spring-ehcache.xml]: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: java.lang.NullPointerException
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
我希望这是由于我的 createCacheLoader 实现只返回 null 但是应该如何构造返回的对象 CacheLoader ?
这可能不相关,但这是我的 pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ehcache-test</groupId>
<artifactId>ehcache-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>net.sf.ehcache.internal</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
</dependencies>
</project>
【问题讨论】:
标签: java spring caching ehcache terracotta