我可以使用对象的哈希码作为cachekey吗?我可以使用对象的哈希码作为缓存键吗?
我的假设是散列对每个标准都是一样的,其中属性值是相同的?
像这样:
function void Method1(Criteria criteria)
{
ObjectCache cache = MemoryCache.Default;
string CacheKey = "Method1-" + criteria.GetHashCode();
if(cache.Contains(CacheKey))
return (IEnumerable)cache.Get(CacheKey);
else
{
IEnumerable stuff = repository.GetStuff();
CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddHours(1.0);
cache.Add(CacheKey, stuff, cacheItemPolicy);
return stuff;
}
}
你测试了你的假设吗? –
“Criteria”的文档对“GetHashCode”返回的内容有何说法?不要假设。 – shf301
如果你打算这么做,我会推荐'覆盖GetHashCode()'。 – IAbstract