. net core 2.0 migration tips: how to fix the memorycache problem

Time:2021-5-6

preface

As you all know, for traditional. Net framework projects,System.Runtime.CachingNamespace is a common tool, and the memorycache class is often used to implement memory caching.

. net core 2.0 does not support system. Runtime. Caching DLL for the time being, which means that the code related to memorycache no longer works.

But the good news is that we can use the new API of. Net core 2.0 to implement the memory caching function, simply modify the code and solve the incompatibility problem. Let’s take a look at the detailed introduction.

Solution

1. Import the old code into the project as follows:

using System;
using System.Runtime.Caching;

namespace TestWebApp.Service
{
 public class MemoryCacheService
 {
  static ObjectCache cache = MemoryCache.Default;
  /// <summary>
  ///Get cache value
  /// </summary>
  /// <param name="key"></param>
  /// <returns></returns>
  private object GetCacheValue(string key)
  {
   if (key != null && cache.Contains(key))
   {
    return cache[key];
   }
   return default(object);
  }
  /// <summary>
  ///Add cache content
  /// </summary>
  /// <param name="key"></param>
  /// <param name="value"></param>
  public static void SetChacheValue(string key, object value)
  {
   if (key != null)
   {
    CacheItemPolicy policy = new CacheItemPolicy
    {
     SlidingExpiration = TimeSpan.FromHours(1)
     
    };
    cache.Set(key, value, policy);
   }
  }
 }
}

After importing, you will find that vs will prompt that it cannot be foundSystem.Runtime.CachingThe original code cannot be directly compiled and used.

2. Add a pair ofMicrosoft.Extensions.Caching.MemoryIt provides the default memorycache class implemented by. Net core and a new memory cache API


using Microsoft.Extensions.Caching.Memory;

3. Rewrite the code and use the new API to realize the memory cache function

Before initializing cache object mode Rewriting:


static ObjectCache cache = MemoryCache.Default;

After initializing cache object mode Rewriting:


static MemoryCache cache = new MemoryCache(new MemoryCacheOptions());

Change of reading method of memory cache value:


private object GetCacheValue(string key)
{
 if (key != null && cache.Contains(key))
 {
  return cache[key];
 }
 return default(object);
}

After Rewriting:


private object GetCacheValue(string key)
{
 object val = null;
 if (key != null && cache.TryGetValue(key, out val))
 {
  return val;
 }
 else
 {
  return default(object);
 }
}

Change the method of setting the contents of memory cache:


public static void SetChacheValue(string key, object value)
{
 if (key != null)
 {
  CacheItemPolicy policy = new CacheItemPolicy
  {
   SlidingExpiration = TimeSpan.FromHours(1)
  };
  cache.Set(key, value, policy);
 }
}

After modification:


public static void SetChacheValue(string key, object value)
{
 if (key != null)
 {
  cache.Set(key, value, new MemoryCacheEntryOptions
  {
   SlidingExpiration = TimeSpan.FromHours(1)
  });
 }
}

conclusion

In useMicrosoft.Extensions.Caching.MemoryAfter rewriting the old code, you will find that all the existing memory cache timeout policies correspond to the new API, including absolute expiration, sliding expiration, etc.

So we can still easily use the new API of. Net core. With simple changes, we can reuse most of the existing old code and migrate it to continue to work.

The complete code after migration is as follows:

using Microsoft.Extensions.Caching.Memory;
using System;

namespace TestMemoryCacheWebApp.Services
{
 public class MemoryCacheService
 {
  static MemoryCache cache = new MemoryCache(new MemoryCacheOptions());
  /// <summary>
  ///Get cache value
  /// </summary>
  /// <param name="key"></param>
  /// <returns></returns>
  private object GetCacheValue(string key)
  {
   object val = null;
   if (key != null && cache.TryGetValue(key, out val))
   {

    return val;
   }
   else
   {
    return default(object);
   }
  }
  /// <summary>
  ///Add cache content
  /// </summary>
  /// <param name="key"></param>
  /// <param name="value"></param>
  public static void SetChacheValue(string key, object value)
  {
   if (key != null)
   {
    cache.Set(key, value, new MemoryCacheEntryOptions
    {
     SlidingExpiration = TimeSpan.FromHours(1)
    });
   }
  }
 }
}

summary

The above is the whole content of this article, I hope the content of this article can bring some help to your study or work, if you have any questions, you can leave a message to exchange, thank you for your support to developer.

Recommended Today

Analysis on the investigation scheme of the integrated platform of medical and welfare (2)

Business Tags: hospital information integration platform, Internet hospital, Internet nursing, chronic disease follow-up Technical labels: ESB, ETL + CDC, NLP, FAAS, SaaS, Hadoop, microservice   Technology wechat group:Add wechat: wonter send: technical QMedical wechat group:Add wechat: wonter send: Medical Q   —————— BEGIN ——————   Analysis on the investigation scheme of Neusoft integration platform (1) […]