我可以使用對象的哈希碼作爲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