码迷,mamicode.com
首页 > Web开发 > 详细

.net 服务端缓存 Cache/CacheHelper

时间:2019-12-30 19:05:18      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:none   object   单位   default   type   returns   addm   expires   nta   

 

服务端缓存公共类

/// <summary>
    /// 公共缓存类
    /// </summary>
    public static class CacheHelper
    {
        private static ObjectCache cache = MemoryCache.Default;

        /// <summary>
        /// 查询缓存是否存在
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns>是否存在</returns>
        public static bool ExistsCache(string key)
        {
            return cache.Contains(key);
        }

        /// <summary>
        /// 根据缓存键移除缓存对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="key">缓存键</param>
        public static void RemoveCache(string key)
        {
            if (!cache.Contains(key)) return;

            cache.Remove(key);
        }

        /// <summary>
        /// 设置缓存对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="obj">缓存对象</param>
        /// <param name="expires">过期时间(单位:分钟)</param>
        public static void SetCache<T>(string key, T obj, double expires = 20) where T : class
        {
            CacheItemPolicy policy = new CacheItemPolicy()
            {
                AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(expires)
            };

            cache.Set(key, obj, policy);
        }

        /// <summary>
        /// 根据缓存键获取缓存对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="key">缓存键</param>
        /// <returns>缓存对象</returns>
        public static T GetCache<T>(string key) where T : class
        {
            if (!cache.Contains(key)) return null;
            var item = cache.GetCacheItem(key);
            if (item != null)
            {
                return item.Value as T;
            }
            return null;
        }

        /// <summary>
        /// 获取缓存对象,如果不存在,则重新设置
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="func">缓存委托</param>
        /// <param name="expires">过期时间(单位:分钟)</param>
        /// <returns>缓存对象</returns>
        public static T GetCache<T>(string key, Func<T> func, double expires = 20) where T : class
        {
            if (!cache.Contains(key))
            {
                CacheItemPolicy policy = new CacheItemPolicy()
                {
                    AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(expires)
                };

                T obj = null;

                if (func != null)
                {
                    obj = func();

                    if (obj != null)
                    {
                        cache.Set(key, obj, policy);
                    }
                }

                return obj;
            }
            else { return cache.GetCacheItem(key).Value as T; };
        }
    }

 

.net 服务端缓存 Cache/CacheHelper

标签:none   object   单位   default   type   returns   addm   expires   nta   

原文地址:https://www.cnblogs.com/jiangqw/p/12121245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!