Announcement Announcement Module
Collapse

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions (http://www.springsource.org/extensions) project, this forum has been decommissioned in favour of Spring Extensions one at:
http://forum.springsource.org/forumdisplay.php?f=44

Please see the Spring Extensions home page for a complete list of current projects in Java, .NET and ActionScript. You can also propose one if you want.

Cheers,
Costin Leau
SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
http://twitter.com/costinl
See more
See less
Problem using @Cacheable with jcs Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem using @Cacheable with jcs

    Hi,

    I am trying use the spring-modules-cache (0.9 version) with jcs, but it does not work.
    I wrote this class:

    Code:
    package com;
    
    import java.util.ArrayList;
    import java.util.List;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springmodules.cache.annotations.CacheFlush;
    import org.springmodules.cache.annotations.Cacheable;
    
    public class TigerCacheableService {
    	
      List<String> names = new ArrayList<String>();
    	
      public List<String> getNames() {
        return names;
      }
    
      public void setNames(List<String> names) {
        this.names = names;
      }
    
      @Cacheable(modelId = "testCaching")
      public final String getName(int index) {
        System.out.println("Processing getName");
        return names.get(index);
      }
    
      @CacheFlush(modelId = "testFlushing")
      public final void updateName(int index, String name) {
        System.out.println("Processing updateName");
      }
    	
      public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/application-context.xml");
        TigerCacheableService tigerCacheableService = (TigerCacheableService)applicationContext.getBean("cacheableService");
        System.out.println(tigerCacheableService.getName(0));
        System.out.println(tigerCacheableService.getName(0));
        System.out.println(tigerCacheableService.getName(0));
      }
    
    }
    With this jcs-config.properties:

    Code:
    jcs.default=
    jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
    jcs.default.cacheattributes.MaxObjects=1000
    jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
    jcs.default.cacheattributes.UseMemoryShrinker=true
    jcs.default.cacheattributes.MaxMemoryIdleTimeSeconds=3600
    jcs.default.cacheattributes.ShrinkerIntervalSeconds=60
    jcs.default.cacheattributes.MaxSpoolPerRun=500
    jcs.default.elementattributes=org.apache.jcs.engine.ElementAttributes
    jcs.default.elementattributes.IsEternal=false
    And with this application-context.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans.xsd">
    
      <!-- Cache Provider -->
      <bean id="cacheManager" class="org.springmodules.cache.provider.jcs.JcsManagerFactoryBean">
        <!-- Optional properties -->
        <property name="configLocation" value="classpath:/jcs-config.properties" />                          
      </bean>
    	
      <bean id="cacheProviderFacade" class="org.springmodules.cache.provider.jcs.JcsFacade">
        <property name="cacheManager" ref="cacheManager" />
      </bean>
    	
      <!-- 1.5+ Annotation -->	
      <bean id="cachingAttributeSource" class="org.springmodules.cache.annotations.AnnotationCachingAttributeSource"></bean>
    
      <bean id="cachingInterceptor" class="org.springmodules.cache.interceptor.caching.MetadataCachingInterceptor">
        <property name="cacheProviderFacade" ref="cacheProviderFacade" />
        <property name="cachingAttributeSource" ref="cachingAttributeSource" />
        <property name="cachingModels">
          <props>
            <prop key="testCaching">cacheName=testCache</prop>
          </props>
        </property>
      </bean>
    
      <bean id="cachingAttributeSourceAdvisor" class="org.springmodules.cache.interceptor.caching.CachingAttributeSourceAdvisor">
        <constructor-arg ref="cachingInterceptor" />
      </bean>
    
      <bean id="flushingAttributeSource" class="org.springmodules.cache.annotations.AnnotationFlushingAttributeSource"></bean>
    
      <bean id="flushingInterceptor" class="org.springmodules.cache.interceptor.flush.MetadataFlushingInterceptor">
        <property name="cacheProviderFacade" ref="cacheProviderFacade" />
        <property name="flushingAttributeSource" ref="flushingAttributeSource" />
        <property name="flushingModels">
          <props>
            <prop key="testFlushing">cacheName=testCache</prop>
          </props>
        </property>
      </bean>
    
      <bean id="flushingAttributeSourceAdvisor" class="org.springmodules.cache.interceptor.flush.FlushingAttributeSourceAdvisor">
        <constructor-arg ref="flushingInterceptor" />
      </bean>
    
      <!-- Set up the objects to apply caching to -->
      <bean id="cacheableService"	class="com.TigerCacheableService">
        <property name="names">
          <list>
            <value>Luke Skywalker</value>
            <value>Leia Organa</value>
          </list>
        </property>
      </bean>
    
    </beans>
    In the System.out, I got:

    Code:
    Processing getName
    Luke Skywalker
    Processing getName
    Luke Skywalker
    Processing getName
    Luke Skywalker
    But what was expected:

    Code:
    Processing getName
    Luke Skywalker
    Luke Skywalker
    Luke Skywalker
    In the log I did not see any exception or error
    Can someone help me?
    Thanks

  • #2
    Hi,

    I solved the problem.
    I added in my application-context.xml the follow line:

    Code:
    <bean id="autoproxy" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
    In my class I changed from:

    Code:
      @Cacheable(modelId = "testCaching")
      public final String getName(int index) {
        System.out.println("Processing getName");
        return names.get(index);
      }
    
      @CacheFlush(modelId = "testFlushing")
      public final void updateName(int index, String name) {
        System.out.println("Processing updateName");
      }
    to:

    Code:
    @Cacheable(modelId = "testCaching")
      public String getName(int index) {
        System.out.println("Processing getName");
        return names.get(index);
      }
    
      @CacheFlush(modelId = "testFlushing")
      public void updateName(int index, String name) {
        System.out.println("Processing updateName");
      }
    The code worked. Yes!
    Bye

    Comment


    • #3
      version of spring

      may i know what version of spring are u using. i trying out with spring-aop 3.0 but it doesnt work because there is no such class

      java.lang.NoClassDefFoundError: org/springframework/metadata/Attributes

      Comment


      • #4
        Hi,

        Has anybody got the solution of this problem? I included spring 2.5.6-jar and it is now not giving exception and code is working but caching is not happening.

        Thanks,
        Bhushan

        Comment

        Working...
        X