PDA

View Full Version : Declarative Caching


t.schoellhorn
May 18th, 2006, 10:01 AM
Hi,

I just have started to use sping-modules because I like the idea of declarative caching. So I wrote a very small example which should just demonstrate that feature - in fact it is more or less a copy from the documentation.

Caching works fine - but I get always some messages like this one:

[INFO] MetadataFlushingInterceptor - -Unable to flush cache. No model is associated to the intercepted method

Sorry about posting a lot of source - but I don't know what else to do.


The interface is a simple one:

<code>
public interface MyService {

public String method(String arg);

public void update(String newValue);

public void update();

}
</code>

The implementation is:

<code>
public class MyServiceImpl implements MyService {
private String value = "DefaultValue";

@Cacheable(modelId = "testCaching")
public String method(String arg) {
System.err.println("->computing...");

try {
Thread.sleep(3000);
} catch (Exception e) {
}

System.err.println("->computed!");

return value + arg;
}

@CacheFlush(modelId = "testFlushing")
public void update(String newValue) {
System.err.println("-->Updating...");
value = newValue;
}

@CacheFlush(modelId = "testFlushing")
public void update() {
}


public static void main(String[] args) throws Exception {
MyService service = (MyService)Ini.getInstance().getBean("myService");

String res = service.method("1");

System.err.println("->res: " + res);

String res2 = service.method("2");

System.err.println("->res2: " + res2);

service.update();

System.err.println("->res3 after update: " + service.method("1"));
}
</code>

And the bean-configuration is here:

<code>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="myService"
class="kos.wnetz.sandbox.springmodules.MyServiceImpl"/>


<!-- Dieses Bean ist dafuer verantwortlich, dass die hier benannten
Beans mit dem cachingInterceptor und dem flushingInterceptor
ausgestattet werden. -->
<bean
class="org.springframework.aop.framework.autoproxy.BeanNa meAutoProxyCreator">
<property name="beanNames">
<list>
<idref local="myService"/>
</list>
</property>
<property name="interceptorNames">
<list>
<value>cachingInterceptor</value>
<value>flushingInterceptor</value>
</list>
</property>
</bean>


<bean id="cachingAttributeSource"
class="org.springmodules.cache.annotations.AnnotationCach ingAttributeSource">
</bean>

<bean id="cachingInterceptor"
class="org.springmodules.cache.interceptor.caching.Metada taCachingInterceptor">

<property name="cacheProviderFacade" ref="cacheProviderFacade" />
<property name="cachingAttributeSource" ref="cachingAttributeSource" />
<!--<property name="cachingListeners">
<list>
<ref bean="cachingListener" />
</list>
</property>-->
<property name="cachingModels">
<props>
<prop key="testCaching">cacheName=SandboxCache</prop>
</props>
</property>
</bean>

<bean id="cachingAttributeSourceAdvisor"
class="org.springmodules.cache.interceptor.caching.Cachin gAttributeSourceAdvisor">
<constructor-arg ref="cachingInterceptor" />
</bean>

<bean id="flushingAttributeSource"
class="org.springmodules.cache.annotations.AnnotationFlus hingAttributeSource">
</bean>

<bean id="flushingInterceptor"
class="org.springmodules.cache.interceptor.flush.Metadata FlushingInterceptor">
<property name="cacheProviderFacade" ref="cacheProviderFacade" />
<property name="flushingAttributeSource" ref="flushingAttributeSource" />
<property name="flushingModels">
<props>
<prop key="testFlushing">cacheNames=SandboxCache</prop>
</props>
</property>
</bean>

<bean id="flushingAttributeSourceAdvisor"
class="org.springmodules.cache.interceptor.flush.Flushing AttributeSourceAdvisor">
<constructor-arg ref="flushingInterceptor" />
</bean>


<bean id="cacheProviderFacade"
class="org.springmodules.cache.provider.ehcache.EhCacheFa cade">
<property name="cacheManager" ref="cacheManager"/>
</bean>


<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFa ctoryBean">
</bean>
</beans>
</code>

Costin Leau
May 19th, 2006, 04:02 PM
can you turn on debugging what happens underneath? Btw, which version of Spring Modules are you using?

t.schoellhorn
May 21st, 2006, 05:43 AM
Hi,

I am using the just released 0.3 version. How would I turn on debugging for this module?

Regards
Tino

Costin Leau
May 21st, 2006, 05:52 AM
Spring Modules uses Commons Logging which in turn relies on Log4j or Jdk 1.4 logging. See http://jakarta.apache.org/commons/logging/ and http://logging.apache.org/log4j/docs/ for more info. Most of the times, a log4j.properties inside your classpath is the solution. You can find a quick guide here: http://www.vipan.com/htdocs/log4jhelp.html - however, if you encounter problem I advice to use the links above.