首页 > 解决方案 > 根据对象的属性生成唯一的字符串键(cachekey)

问题描述

我只是想生成一个唯一的缓存键,它接受对象类型和属性值 GetHashCode 每次都返回不同的结果,这样就不会工作,所以我必须实现一个解决方案,但它必须很快(我可以通过属性和并将它们的值连接到一个字符串,但这可能很慢而且不是最好的方法)

很高兴:因此,如果 2 个不同的对象类型具有完全相同的属性和相同的值但它们是不同的类,它们应该是不同的缓存键(发生这种情况的可能性非常小,但以防万一)

这是我的代码

public interface ICachableRequest
{
    string GetCacheKey();
}

public class Object1 : ICachableRequest
{
    public int IntValue1 { get; set; }
    public double DoubleVal1 { get; set; }
    public string  StringVal1 { get; set; }
    public string GetCacheKey()
    {
        throw new NotImplementedException();
    }
}
public class Object2 : ICachableRequest
{
    public int SomeIntValue1 { get; set; }
    public double SomeOtherDoubleVal1 { get; set; }
    public string MoreStringVal1 { get; set; }

    public string MoreStringVal2 { get; set; }
    public string MoreStringVal3 { get; set; }
    public string MoreStringVal4 { get; set; }

    public string GetCacheKey()
    {
        throw new NotImplementedException();
    }
}

标签: c#

解决方案


推荐阅读