Ejemplo n.º 1
0
 //初始化数据项
 /// <summary>
 /// 初始化数据项
 /// </summary>
 /// <param name="key"></param>
 /// <param name="storedData"></param>
 private static string InitStoredDataItem(string key, StoredDataInfo<T> storedData)
 {
     lock (lockObj)
     {
         if (EntireStoredData.ContainsKey(key))
         {
             return "key:" + key + " 已存在";
         }
         EntireStoredData.Add(key, storedData);
     }
     return "";
 }
Ejemplo n.º 2
0
        // 获取指定key的数据项
        /// <summary>
        /// 获取指定key的数据项
        /// </summary>
        /// <param name="key"></param>
        /// <param name="isForcedRefresh">是否强制更新</param>
        /// <returns></returns>
        public static T GetData(string key, bool isForcedRefresh = false)
        {
            //不存在key
            if (!HasKey(key))
            {
                #region

                string currKeys = "";
                string currTType = "";
                lock (lockObj)
                {
                    if (EntireStoredData.Any())
                    {
                        currKeys = string.Join(",", EntireStoredData.Keys.ToArray());
                        var v = EntireStoredData.First().Value.Data;
                        currTType = v.GetType().ToString();
                    }
                }
                throw new Exception(string.Format("无指定key:{0},当前池包含key集合{1},当前池类型:{2}", key, currKeys, currTType));

                #endregion
            }

            //根据key获取value
            StoredDataInfo<T> sdi = EntireStoredData[key];

            //判断是否过期
            int timeOfDuration = sdi.TimeOfDuration;
            DateTime lastModifyTime = sdi.LastModifyTime;

            if (!isForcedRefresh && DateTime.Now.AddMinutes(-timeOfDuration) <= lastModifyTime)
                return sdi.Data;

            //重新更新数据
            sdi.Data = sdi.GetDataMethod();
            sdi.LastModifyTime = DateTime.Now;

            return sdi.Data;
        }