zoukankan      html  css  js  c++  java
  • .NET Core 2.0迁移技巧之MemoryCache问题修复

    对于传统的.NET Framework项目而言,System.Runtime.Caching命名空间是常用的工具了,其中MemoryCache类则常被用于实现内存缓存。

    .NET Core 2.0暂时还不支持System.Runtime.Caching dll,这也就意味着MemoryCache相关代码不再起作用了。

    但是好消息是,我们可以使用.NET Core 2.0的新API实现内存缓存功能,简单修改代码,解决不兼容问题。

    解决方案


      1.将旧代码导入项目中,如下:

    using System;
    using System.Runtime.Caching;
    
    namespace TestWebApp.Service
    {
        public class MemoryCacheService
        {
            static ObjectCache cache = MemoryCache.Default;
            /// <summary>
            /// 获取缓存值
            /// </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>
            /// 添加缓存内容
            /// </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);
                }
            }
        }
    }

      导入后你会发现VS会提示无法找到System.Runtime.Caching命名空间,原有的代码无法直接编译使用。

     

      2.添加对Microsoft.Extensions.Caching.Memory命名空间的引用,它提供了.NET Core默认实现的MemoryCache类,以及全新的内存缓存API

      

    using Microsoft.Extensions.Caching.Memory;

      3.改写代码,使用新的API实现内存缓存功能

      初始化缓存对象方式改写前:

    static ObjectCache cache = MemoryCache.Default;

      初始化缓存对象方式改写后:

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

      读取内存缓存值方式变化:

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

      改写后:

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

      设定内存缓存内容方式变化:

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

      修改后:

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

    结论


     

    在使用了Microsoft.Extensions.Caching.Memory下的新API改写了旧代码后,你会发现原有的各种内存缓存超时策略全都是有对应新API的,包括AbsoluteExpiration, SlidingExpiration等等。

    所以我们还是可以很轻松的使用.NET Core新API简单改动下下就能重用现有绝大部分旧代码,将其迁移过来继续起作用。

    迁移后的完整代码如下:

    using Microsoft.Extensions.Caching.Memory;
    using System;
    
    namespace TestMemoryCacheWebApp.Services
    {
        public class MemoryCacheService
        {
            static MemoryCache cache = new MemoryCache(new MemoryCacheOptions());
            /// <summary>
            /// 获取缓存值
            /// </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>
            /// 添加缓存内容
            /// </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)
                    });
                }
            }
        }
    }
  • 相关阅读:
    EXCel操作类
    C#中的excel操作类//集合了几个别人的类
    企业库 AccessHelper
    企业库 SqlHelper
    ASP.NET Excel操作类
    WPF使用MVVM(一)属性绑定
    WPF使用ttf图标字体
    WPF自定义界面WindowChrome
    WPF使用IconFont中的Symbol图标
    字体图标查看工具(Character Map UWP)
  • 原文地址:https://www.cnblogs.com/mantgh/p/7429551.html
Copyright © 2011-2022 走看看