Spring @Cacheable not working - what's wrong with my config?

五迷三道 提交于 2019-12-10 18:04:40

问题


I've seen many incarnations of this same issue but I think I've tried all the fixes - my usage is quite straightforward.

I had been using Ehcache which also didn't work. So, to rule out Ehcache issues and help point to something more fundamental, I moved to SimpleCacheManager and ConcurrentMapCacheFactoryBean.

Here's my config:

<cache:annotation-driven/>

<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
      <property name="caches">
        <set>
          <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="parentAppIds"/>
        </set>
      </property>
    </bean>

Here's my method:

    @Cacheable(value="parentAppIds", key="accountNumber")
        public Long findApplicationId(String accountNsc, String accountNumber) throws EMSException {
....
}

This is a method on an interface, who's implementing class is Spring managed @Service("foo")

I tried using 'p0' as is suggested here but to no avail. I have no compilation problems and no errors in my server logs so I'm confident that I have all that is necessary on my classpath; and that Namespaces are all fine, since I'm using STS for that - so I left out pom.xml and spring Namespace declarations to block noise.

I'm using Spring 3.1; Java 1.5 and Websphere 6.1

The symptom is that the method is being visited with the same parameters repeatedly.

Please help - I'm hungry and refuse to go for lunch until I nail this.

note: I have simplified my @Cacheable declaration my actual one is

@Cacheable(value="parentAppIds", key="#p0.concat('-').concat(#p1)")

Neither work.

Thanks.

** Edit - I've ruled out Websphere as being a problem by creating a test rig with

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(...)

which mimics what is happening. @Cacheable is simply not working. There must be something blindingly obvious that I am missing. (I've had lunch now)


回答1:


My issue is resolved. Unfortunately I cannot pinpoint exactly where my issue lay. Certainly, all that is required is that which I have mentioned in my question.

TO fix this, I tidied up my Spring configuration a bit and cleared my browser and application server cache and temp directories. I did a full clean install and cache is now working.

It is possible that I was testing with an earlier version which did not include this important line in the application config:

<cache:annotation-driven/>

I had omitted that at the start. Maybe my adding of that was not picked up until now. Otherwise I am stumped. Thanks for your time.




回答2:


Did you perhaps change

@Cacheable(value="parentAppIds", key="accountNumber")

to

@Cacheable(value="parentAppIds", key="#accountNumber")

as adding the # that removed one error for me while trying to get caching working.



来源:https://stackoverflow.com/questions/20494574/spring-cacheable-not-working-whats-wrong-with-my-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!