Spring @Cacheable not working - what's wrong w

2019-07-03 17:51发布

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)

2条回答
叼着烟拽天下
2楼-- · 2019-07-03 18:28

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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-07-03 18:41

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.

查看更多
登录 后发表回答