0

I use MemoryCache with ASP .Net Core. I want to save List<> in cache, but got strange problem.

  1. Forming List;

  2. 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));
     }
    
  3. Cleared List!

  4. Cache values was cleared too!! Why???

     public virtual TWell Get<TWell>(string key)
     {
         _cache.TryGetValue<TWell>(key, out var entry);
         return entry;
     }
    
2
  • 1
    List<T> is reference type. You saved a reference. So you can change content by reference. Commented Sep 25, 2022 at 19:31
  • Ooops! And what i have to do? Main target is to decrease memory consumption, so i dont want to create additional lists. Commented Sep 26, 2022 at 5:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.