c# - 根据对象的属性生成唯一的字符串键(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();
}
}
解决方案
推荐阅读
- javascript - 使用 js.erb 错误的 rails 6 动态引导模式
- sql - 如何使用 Postgres 获取最新行但给定类型只有一行
- mariadb - 为什么这个 SQL PREPARE .. EXECUTE 在 MySQL 8 中给出语法错误,但在 MariaDB 中没有
- java - 具有长度限制的逗号分隔字符串的正则表达式
- javascript - 从 javascript / ajax 访问 Spring MVC 模型
- mongodb-atlas-search - 为什么 Mongodb Atlas Search $search 运算符在精确查询匹配时不返回 score=1?
- mysql - 是否可以在 MySQL 中按另一列分组的列中检索名称以及 min 和 max?
- python - 将字符串中字符的索引转换为字符串中字符的相应行和列
- java - Junit ant 任务未将断言错误报告为失败
- python - 如何从python中的for循环在csv文件中创建两列?