0

我有一个配置,其中缓存保存在一个节点中并从另一个节点访问。虽然我能够很好地获取()和放置(),但某些操作(如 size()、keySet() 等)不会返回正确的结果。

Test1 客户端节点缓存配置

<bean id="test-cache" class="org.gridgain.grid.cache.GridCacheConfiguration">
    <property name="name" value="testCache"/>
     <property name="cacheMode" value="PARTITIONED"/>
    <property name="distributionMode" value="CLIENT_ONLY" />
    <property name="swapEnabled" value="true"/>

</bean>

Test1 客户端节点类

public class GridGainTest1
{
    public static void main(String[] args) throws Exception
    {
        //Client Mode
        Grid g = GridGain.start("etc/config/grid-test1.xml");
        //Put in Remote Cache
        g.cache("testCache").put(1, "ABC");
        g.cache("testCache").put(2, "XYZ");

        System.out.println("Size of Cache :- " + g.cache("testCache").size());
        System.out.println("Value for 1 :- " + g.cache("testCache").get(1));
        System.out.println("Value for 2 :- " + g.cache("testCache").get(2));
    }

Test2 数据节点缓存配置

 <bean id="test-cache" class="org.gridgain.grid.cache.GridCacheConfiguration">
    <property name="name" value="testCache"/>
     <property name="cacheMode" value="PARTITIONED"/>
    <property name="swapEnabled" value="true"/>

</bean>

Test2 数据节点类

    public class GridGainTest2
{
    public static void main(String[] args) throws Exception
    {
        Grid g = GridGain.start("etc/config/grid-test2.xml");
    }
}

节点 1 的输出如下所示,其中 size 为 0,即使映射中有条目。我不确定这是否是由于一些错误配置造成的。

Size of Cache :- 0
Value for 1 :- ABC
Value for 2 :- XYZ
4

1 回答 1

1

在 GridGain 缓存 API 方法size()中,primarySize(), nearSize(), keySet(), primaryKeySet(), values(), primaryValues(), entrySet(),primaryEntrySet()是本地的,它们将仅返回存储在本地节点上的键的大小或集合。

在您的情况下,您在CLIENT_ONLY模式下启动了 Test1 上的缓存,因此该节点不存储任何键。这就是为什么您总是将0其视为缓存大小的原因。

如果需要全局缓存大小,可以使用以下代码:

    GridCallable<Integer> sizeCallable = new GridCallable<Integer>() {
        @Override public Integer call() throws Exception {
            return g.cache("testCache").size();
        }
    };

    Collection<Integer> sizes = g.forCache("testCache").compute()
        .broadcast(sizeCallable).get();

    int globalSize = 0;

    for (Integer s : sizes)
        globalSize += s;

方便GridCache.globalSize()的方法将在即将发布的 GridGain 6.2 版本中添加。

于 2014-06-18T17:02:37.433 回答