Esempio n. 1
0
 /// <summary>
 /// 根据键更新一个缓存
 /// </summary>
 /// <param name="key"></param>
 public static bool UpdateCache(string key)
 {
     key = key.Trim();
     if (cacheDatas.ContainsKey(key))
     {
         MemoryDataCacheItem cacheItem = cacheDatas[key];
         DBHelper            sqlHelper = cacheItem.DBHelper;
         sqlHelper.Params = cacheItem.Params;
         try
         {
             var data = QueryData(key, cacheItem.Type, cacheItem.Query, sqlHelper);
             cacheItem.UpdatedData = data;
             cacheItem.UpdateTime  = DateTime.Now;
             cacheItem.QueryCount += 1;
             return(true);
         }
         catch { }
     }
     return(false);
 }
Esempio n. 2
0
 /// <summary>
 /// 根据键更新一个缓存
 /// </summary>
 /// <param name="key"></param>
 public static bool UpdateCache(string key)
 {
     key = key.Trim();
     if (cacheDatas.ContainsKey(key))
     {
         MemoryDataCacheItem cacheItem = cacheDatas[key];
         DBHelper            helper    = cacheItem.DBHelper;
         helper.Params = cacheItem.Params;
         try
         {
             var data = QueryData(key, cacheItem.Type, cacheItem.Query, cacheItem.Mapping, helper);
             //将新数据放放UpdateData中, 下次调用时填入Data
             cacheItem.UpdatedData = data;
             cacheItem.UpdateTime  = DateTime.Now;
             cacheItem.QueryCount += 1;
             cacheItem.Data        = null;
             return(true);
         }
         catch { }
     }
     return(false);
 }
Esempio n. 3
0
        internal static Dictionary <string, TItem> GetCacheList <TItem>(string query, IEnumerable <Attribute.FieldMapping> mapping, int timeOut, DBHelper helper, out string key) where TItem : IModel, new()
        {
            Type type    = typeof(TItem);
            var  typeKey = string.Format("{0}_{1}", type.FullName, helper.DatabaseName.ToLower());

            query = query.ToLower();
            string Params = string.Join(":", helper.Params);

            //按参数进行缓存
            key = StringHelper.EncryptMD5(query + Params + "|" + helper.DatabaseName);//按库名
            //初始缓存
            MemoryDataCacheItem dataItem;
            var a = cacheDatas.TryGetValue(key, out dataItem);

            if (!a)
            {
                dataItem = new MemoryDataCacheItem()
                {
                    Data = null, Mapping = mapping, TimeOut = timeOut, DBHelper = helper, Query = query, Params = new Dictionary <string, object>(helper.Params), Type = type
                };
                cacheDatas.TryAdd(key, dataItem);
                lock (lockObj)
                {
                    if (typeCache.ContainsKey(typeKey))
                    {
                        typeCache[typeKey].Add(key);
                    }
                    else
                    {
                        typeCache[typeKey] = new List <string>()
                        {
                            key
                        };
                    }
                }
            }
            else
            {
                if (dataItem.QueryCount == 0)//缓存没有创建好时返回空
                {
                    var ts = DateTime.Now - dataItem.UpdateTime;
                    if (ts.TotalMinutes < 1)
                    {
                        throw new CRLException($"缓存[{typeof(TItem)}]创建中...");
                    }
                }
            }
            //首次查询
            if (dataItem.QueryCount == 0)
            {
                try
                {
                    var data  = QueryData(key, type, query, mapping, helper);
                    var data2 = ObjectConvert.ConvertToDictionary <TItem>(data);
                    dataItem.Data       = data2;
                    dataItem.Count      = data2.Count;
                    dataItem.QueryCount = 1;
                    dataItem.UpdateTime = DateTime.Now;
                }
                catch (Exception ero)
                {
                    cacheDatas.TryRemove(key, out dataItem);
                    typeCache[typeKey].Remove(key);
                    throw ero;
                }
            }

            if (timer == null)
            {
                StarWatch();
            }
            //更新缓存数据
            if (dataItem.UpdatedData != null)
            {
                var data2 = ObjectConvert.ConvertToDictionary <TItem>(dataItem.UpdatedData);
                dataItem.Data        = data2;
                dataItem.Count       = data2.Count;
                dataItem.UpdatedData = null;
            }
            dataItem.UseTime = DateTime.Now;
            return(dataItem.Data as Dictionary <string, TItem>);
        }