Пример #1
0
 /// <summary>
 /// 获取缓存的值
 /// </summary>
 /// <param name="cacheName"></param>
 /// <returns></returns>
 public static T Get <T>(string cacheName)
 {
     if (CacheObjectData.ContainsKey(cacheName))
     {
         DataObjectModel <T> model = (DataObjectModel <T>)CacheObjectData[cacheName];
         if (DateTime.Now > model.LastRequestTime.Add(model.TimeSpan))
         {
             lock (model.DataObject)
             {
                 if (DateTime.Now > model.LastRequestTime.Add(model.TimeSpan))
                 {
                     model.DataObject = model.Function.Invoke(model.args);
                 }
                 else
                 {
                     return(model.DataObject);
                 }
             }
             return(model.DataObject);
         }
         else
         {
             return(model.DataObject);
         }
     }
     else
     {
         throw new Exception("缓存管理器中不包含该缓存");
     }
 }
Пример #2
0
        /// <summary>
        /// 更新缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="timeSpan"></param>
        /// <param name="cacheName"></param>
        /// <param name="function"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        private static bool Updata <T>(TimeSpan timeSpan, string cacheName, Func <Args, T> function, Args args)
        {
            DataObjectModel <T> dataStringModel = new DataObjectModel <T>();

            dataStringModel.DataObject      = function.Invoke(args);
            dataStringModel.LastRequestTime = DateTime.Now;
            dataStringModel.TimeSpan        = timeSpan;
            dataStringModel.Function        = function;
            dataStringModel.args            = args;
            CacheObjectData[cacheName]      = dataStringModel;
            return(true);
        }
Пример #3
0
 /// <summary>
 /// 设置缓存
 /// </summary>
 /// <param name="timeSpan">多久更新一次</param>
 /// <param name="cacheName">缓存名称</param>
 /// <param name="function">更新缓存的方法</param>
 /// <param name="args">更新缓存的方法参数</param>
 public static bool Set <T>(TimeSpan timeSpan, string cacheName, Func <Args, T> function, Args args)
 {
     if (CacheObjectData.ContainsKey(cacheName))
     {
         return(Updata(timeSpan, cacheName, function, args));
     }
     else
     {
         DataObjectModel <T> dataStringModel = new DataObjectModel <T>();
         dataStringModel.DataObject      = function.Invoke(args);
         dataStringModel.LastRequestTime = DateTime.Now;
         dataStringModel.TimeSpan        = timeSpan;
         dataStringModel.Function        = function;
         dataStringModel.args            = args;
         CacheObjectData.Add(cacheName, dataStringModel);
         return(true);
     }
 }