对Cache类的一个泛型包装CacheHelper

对Cache类的一个泛型包装


public static class CacheHelper
    {
        public class Settings
        {
            public string Key { get; set; }
            /// 
            /// Seconds
            /// 
            public int Duration { get; set; }

            public Settings(string key,object o=null)
            {
                Key = key;
                if (o != null)
                {
                    Key = BuildKey(o, key);
                }
#if DEBUG
                Duration = 1;
#else
                Duration = 60;
#endif
            }
        }

        public static string BuildKey(T t,string baseKey=null)
        {
            var sbKey = new StringBuilder(baseKey);
            foreach (PropertyInfo pi in t.GetType().GetProperties())
            {
                sbKey.AppendFormat("{0}{1}", pi.Name, pi.GetValue(t, null));
            }
            return sbKey.ToString();
        }
        public static Cache CurrentCache
        {
            get
            {
                return HttpContext.Current != null ? HttpContext.Current.Cache : HttpRuntime.Cache;
            }
        }

        public static T GetCache(Settings settings)
        {
            if (CurrentCache[settings.Key] != null)
            {
                return (T)CurrentCache[settings.Key];
            }
            return default(T);
        }
        public static T BuildCache(T t, Settings settings)
        {
            if (CurrentCache[settings.Key] != null)
            {
                return (T)CurrentCache[settings.Key];
            }
            CurrentCache.Insert(settings.Key, t, null, DateTime.Now.AddSeconds(settings.Duration),
                TimeSpan.Zero);
            return t;
        }
    }


你可能感兴趣的:(C#,Cache包装,泛型)