带有复合键控实体的引用集合的二级缓存未命中

时间:2016-06-30 11:01:58

标签: c# caching nhibernate

我有一个复合身份class_tier_info_iditem_type_idstage的实体:

public class ClassTierItemTypeUpgrade : IEquatable<ClassTierItemTypeUpgrade>
{
    public virtual ClassTierInfo ClassTier { get; set; } // key-part

    public virtual ItemType ItemType { get; set; } // key-part

    public virtual int Stage { get; set; } // key-part

    public virtual int Value { get; set; }

    public virtual bool Equals(ClassTierItemTypeUpgrade other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return Equals(ClassTier, other.ClassTier) && Equals(ItemType, other.ItemType) && Stage == other.Stage;
    }

    public override bool Equals(object obj) // ...
    public override int GetHashCode() // ...
}


mapping.CompositeId()
    .KeyReference(x => x.ItemType, DbNamingHelper.FormatColumnName(nameof(ClassTierItemTypeUpgrade.ItemType)) + "_id")
    .KeyReference(x => x.ClassTier, DbNamingHelper.FormatColumnName(nameof(ClassTierItemTypeUpgrade.ClassTier)) + "_id")
    .KeyProperty(x => x.Stage);

// Value is auto-mapped

mapping.Schema("public");
mapping.Cache.Region("Static");
mapping.Cache.ReadOnly();
mapping.ReadOnly();

此实体从另一个实体ItemType(自动映射,相同的缓存区域)引用:

public class ItemType
{
    public virtual int Id { get; set; }
    public virtual Iesi.Collections.Generic.ISet<ClassTierItemTypeUpgrade> TierUpgrades { get; set; }
        = new HashedSet<ClassTierItemTypeUpgrade>();
    // ...other properties...
}

所有ItemTypeClassTierItemTypeUpgrade实体都已预加载,应放在二级缓存中。

但是当加载引用ItemType的其他实体时,我在查询日志中看到:

SELECT tierupgrad0_.item_type_id  as item1_1_,
       tierupgrad0_.class_tier_id as class2_1_,
       tierupgrad0_.Stage         as Stage1_,
       tierupgrad0_.item_type_id  as item1_21_0_,
       tierupgrad0_.class_tier_id as class2_21_0_,
       tierupgrad0_.Stage         as Stage21_0_,
       tierupgrad0_.value          as value21_0_
FROM   public.class_tier_item_type_upgrade_infos tierupgrad0_
WHERE  tierupgrad0_.item_type_id = 5 /* :p0 */

表示未正确使用二级缓存。

ItemType实体本身实际上是从二级缓存加载的。

为什么会发生这种情况,我该如何解决?

1 个答案:

答案 0 :(得分:0)

集合缓存设置应与表(每个集合)分开指定:

// instance is ICollectionInstance
instance.Cache.Region("Region");
instance.Cache.ReadOnly();

但后来我another problem所以现在我没有使用它。