Use of. Net core memorycache

Time:2021-2-22

 

 

It seems that you can use it directly without nuget package.

using Microsoft.Extensions.Caching.Memory;

 

public static class CacheHelperNetCore
     {
         public static IMemoryCache _memoryCache = new MemoryCache(new MemoryCacheOptions());
         /// 
         ///Set key value cache
         /// 
         /// 
         ///Value
         ///Expiration time
         public static void SetCache(string key,object obj,TimeSpan timeSpan)
         {
             _memoryCache.Set(key,obj,timeSpan);
         }
         /// 
         ///Get key value cache
         /// 
         /// 
         /// 
         public static object GetCache(string key)
         {
             return _memoryCache.Get(key);
         }
         /// 
         ///Get key value cache
         /// 
         /// 
         /// 
         /// 
         public static T GetCache(string key)
         {
             return _memoryCache.Get(key);
         }
         /// 
         ///Does the key value cache exist
         /// 
         /// 
         /// 
         public static bool Exist(string key)
         {
             return _memoryCache.TryGetValue(key,out _);
         }
     }

 

 

Basically, they are commonly used. It’s OK to improve the performance. However, it is recommended to choose redis if there are persistence requirements or high-level requirements for data structure and processing.