В настоящее время я тестирую новый MemoryCache
в .Net 4 для кэширования нескольких бит данных в одном из наших приложений. У меня проблема в том, что объекты обновляются, и в кеше сохраняются изменения, например.
public IEnumerable<SomeObject> GetFromDatabase(){
const string _cacheKeyGetDisplayTree = "SomeKey";
ObjectCache _cache = MemoryCache.Default;
var objectInCache = _cache.Get(_cacheKeyGetDisplayTree) as IEnumerable<SomeObject>;
if (objectInCache != null)
return objectInCache.ToList();
// Do something to get the items
_cache.Add(_cacheKeyGetDisplayTree, categories, new DateTimeOffset(DateTime.UtcNow.AddHours(1)));
return categories.ToList();
}
public IEnumerable<SomeObject> GetWithIndentation(){
var categories = GetFromDatabase();
foreach (var c in categories)
{
c.Name = "-" + c.Name;
}
return categories;
}
Если бы я сначала вызывал GetWithIndentation()
, а затем вызывал GetFromDatabase()
, я ожидал, что он вернет исходный список SomeObject
, но вместо этого он вернет измененные элементы (с "-" с префиксом имени).
Я думал, что ToList()
уничтожил ссылку, но, похоже, она продолжает сохраняться. Я уверен, что это очевидно, но может ли кто-нибудь обнаружить, где я ошибаюсь?