【问题标题】:Can Interface be used as key in Coherence Cache?Interface 可以用作 Coherence Cache 中的键吗?
【发布时间】:2017-09-20 15:54:09
【问题描述】:

我有 2 种不同类型的键:

interface Key extends Serializable {
   String getName();
}

class KeyA implements Key {
    private String name;
    private int fieldA;
}

class KeyB implements Key {
    private String name;
    private int fieldB;
}

我可以在 Coherence 分区缓存中使用Key 接口作为键吗?

【问题讨论】:

    标签: caching oracle-coherence


    【解决方案1】:

    是的,但您必须确保Key 的所有可能实现都满足以下先决条件(来自Oracle Documentation):

    缓存键还必须提供hashCode()equals() 方法,并且这些方法必须返回一致的结果 跨集群节点。这意味着实施 hashCode()equals() 必须完全基于对象的 可序列化状态(即对象的非瞬态字段)。

    ...

    一些缓存实现(特别是分区缓存)使用 用于相等性测试的关键对象的序列化形式,其中 表示 equals() 方法返回 true 的 键必须 序列化相同

    Patrick Peralta 的 article 很好地解释了这种行为。

    【讨论】:

      【解决方案2】:

      实现该接口的类的实例可以用作此处的键。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-09
        • 2010-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多