I use MemoryCache with ASP .Net Core. I want to save List<> in cache, but got strange problem.
Forming List;
Succesfully saved it in cache:
TrySetEntity(typeof(SkvBase).Name, _wells, _cacheEntryOptions); public virtual void TrySetEntity(string key, IList<TEntity> createItem, MemoryCacheEntryOptions cacheEntryOptions) { using var entry = _cache.CreateEntry(key); entry.SetOptions(cacheEntryOptions); entry.Value = createItem; _cache.Set( key, entry, new CancellationChangeToken(_resetCacheToken.Token)); }Cleared List!
Cache values was cleared too!! Why???
public virtual TWell Get<TWell>(string key) { _cache.TryGetValue<TWell>(key, out var entry); return entry; }
List<T>is reference type. You saved a reference. So you can change content by reference.