Reputation: 209
I have 2 xml config files as below
app-context.xml:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:gfe="http://www.springframework.org/schema/gemfire" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan
base-package="com.mycompany.data.testcache" />
<context:annotation-config />
<import resource="test-cache.xml" />
<bean id="testCache" class="org.springframework.data.gemfire.GemfireTemplate">
<property name="region" ref="region1" />
</bean>
</beans>
test-cache.xml
<?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:gfe="http://www.springframework.org/schema/gemfire"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan
base-package="com.mycompany.data.testcache" />
<context:annotation-config />
<util:properties id="props" location="classpath:test-cache.properties">
<prop key="log-level">info</prop>
</util:properties>
<gfe:cache id="gemfire-cache" properties-ref="props" />
<gfe:local-region id="region1" cache-ref="gemfire-cache">
<gfe:cache-listener ref="listener" />
</gfe:local-region>
<bean id="listener"
class="com.mycompany.data.TestLoggingCacheListener" />
</beans>
and I have a test class called TestCache.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:app-context.xml" })
public class TestCache {
@Autowired
GemfireTemplate testCache;
@Test
public void testgetSomeValues() {
HashMap<String, String> map1 = retrieveSomeValues();
HashMap<String, String> map2 = retrieveSomeValues();
assertEquals(Map1, Map2);
}
@Cacheable(value = "testCache")
public HashMap<String, String> retrieveSomeValues() {
HashMap<String, String> obj = new HashMap<String, String>();
obj.put("600", "Auto");
obj.put("601", "Life");
return obj;
}
}
But I see nothing is actually being cached into the region. for both the method calls to retrieveSomeValues() method, the method is actually being executed instead of retrieving the values from the cache.
Can I know what is wrong in the above test class. Can I know how can I use the @Cacheable to cache the values for the able retriveSomeValues() method?
Upvotes: 0
Views: 1269
Reputation: 153
You need Spring Framework's cache support in order to use @Cacheable. There is a Spring GemFire quickstart example that shows how to use GemFire with @Cacheable annotation at:
https://github.com/SpringSource/spring-gemfire-examples/tree/master/quickstart/spring-cache
You'll find everything you need there: the xml configuration for the app, cache, gemfire, and code that uses the annotation.
Upvotes: 1