.net core 缓存帮助类

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


namespace Common
{
    ///
    /// 缓存帮助类
    ///

    public class CacheCommon
    {
        private static MemoryCache cache = new MemoryCache(new MemoryCacheOptions());


        #region 获取缓存值
        ///
        /// 获取缓存值
        ///

        /// 缓存的键
        /// 返回缓存的值
        public static object GetCacheValue(string key)
        {
            object val = null;
            if (key != null && cache.TryGetValue(key, out val))
            {
                return val;
            }
            else
            {
                return default(object);
            }
        }
        #endregion




        #region 设置缓存值
        ///
        /// 设置缓存值
        ///

        /// 缓存的键
        /// 缓存值
        public static void SetChacheValue(string key, object value)
        {
            if (key != null)
            {
                cache.Set(key, value, new MemoryCacheEntryOptions
                {
                    SlidingExpiration = TimeSpan.FromHours(1)
                });
            }
        } 
        #endregion


    }
}

你可能感兴趣的:(.net,core)