【问题标题】:Values are not being populated into gemfire cache值未填充到 gemfire 缓存中
【发布时间】:2012-12-17 20:49:55
【问题描述】:

我有如下 2 个 xml 配置文件

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>

我有一个名为 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;
    }

}

但我没有看到实际上没有缓存到该区域中。对于retrieveSomeValues() 方法的两个方法调用,实际上是在执行该方法,而不是从缓存中检索值。

我能知道上面的测试类有什么问题吗?我可以知道如何使用@Cacheable 来缓存able retriveSomeValues() 方法的值吗?

【问题讨论】:

    标签: java spring gemfire spring-data-gemfire


    【解决方案1】:

    您需要 Spring Framework 的缓存支持才能使用 @Cacheable。有一个 Spring GemFire 快速入门示例,展示了如何使用带有 @Cacheable 注释的 GemFire:

    https://github.com/SpringSource/spring-gemfire-examples/tree/master/quickstart/spring-cache

    您会在那里找到所需的一切:应用程序的 xml 配置、缓存、gemfire 和使用注释的代码。

    【讨论】:

    • 谢谢苏利希。这很有帮助:)
    猜你喜欢
    • 2013-05-01
    • 2020-05-09
    • 2017-12-07
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 2011-04-12
    • 2022-06-12
    • 1970-01-01
    相关资源
    最近更新 更多