文章出處

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;
 
namespace Utility
{
    /// <summary>
    /// 緩存操作,默認緩存1分鐘
    /// </summary>
    public static class CacheHelper
    {
        static int cacheTime = 1;
 
        /// <summary>
        /// 讀取緩存項
        /// </summary>
        /// <returns></returns>
        public static object CacheReader(string cacheKey)
        {
            return HttpRuntime.Cache[cacheKey];
        }
 
        /// <summary>
        /// 寫入緩存項
        /// </summary>
        public static void CacheWriter(string cacheKey, object cacheValue, int cache_time = 0)
        {
            HttpRuntime.Cache.Insert(cacheKey, cacheValue, null,
                DateTime.Now.AddMinutes(cache_time <= 0 ? cacheTime : cache_time),
                Cache.NoSlidingExpiration);
        }
 
        /// <summary>
        /// 移除指定緩存項
        /// </summary>
        public static void CacheRemove(string cacheName)
        {
            HttpRuntime.Cache.Remove(cacheName);
        }
 
        /// <summary>
        /// 緩存對象泛型實現
        /// </summary>
        public static T ObjectReader<T>(string cacheKey = null)
            where T : class
        {
            string cachekey = typeof(T).GetHashCode() + StringHelper.ToString(cacheKey);
            var obj = CacheReader(cachekey) as T;
            return obj;
        }
 
        /// <summary>
        /// 緩存對象泛型實現
        /// </summary>
        public static void ObjectWriter<T>(T cacheValue, string cacheKey = null, int cache_time = 0)
            where T : class
        {
            string cachekey = typeof (T).GetHashCode() + StringHelper.ToString(cacheKey);
            CacheWriter(cachekey, cacheValue, cache_time);
        }
    }
}

  


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()