Пример #1
0
        internal static Dictionary <string, TItem> GetCacheItem <TItem>(string key) where TItem : IModel
        {
            if (!cacheDatas.ContainsKey(key))
            {
                return(new Dictionary <string, TItem>());
            }
            var dataItem = cacheDatas[key];

            dataItem.UseTime = DateTime.Now;
            if (dataItem.UpdatedData != null)
            {
                dataItem.Data        = ObjectConvert.ConvertToDictionary <TItem>(dataItem.UpdatedData);
                dataItem.UpdatedData = null;
            }

            return(dataItem.Data as Dictionary <string, TItem>);
        }
Пример #2
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);

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

            //按参数进行缓存
            key = StringHelper.EncryptMD5(query + Params + "|" + helper.DatabaseName);//按库名
            //初始缓存
            if (!cacheDatas.ContainsKey(key))
            {
                cacheDatas.TryAdd(key, new MemoryDataCacheItem()
                {
                    Data = null, Mapping = mapping, TimeOut = timeOut, DBHelper = helper, Query = query, Params = new Dictionary <string, object>(helper.Params), Type = type
                });
                if (typeCache.ContainsKey(type))
                {
                    typeCache[type].Add(key);
                }
                else
                {
                    typeCache[type] = new List <string>()
                    {
                        key
                    };
                }
            }
            else
            {
                var dataItem2 = cacheDatas[key];
                if (dataItem2.QueryCount == 0)//缓存没有创建好时返回空
                {
                    throw new CRLException("CRL缓存创建中...");
                    return(new Dictionary <string, TItem>());
                }
            }

            var dataItem = cacheDatas[key];

            //首次查询
            if (dataItem.QueryCount == 0)
            {
                try
                {
                    var data = QueryData(key, type, query, mapping, helper);
                    dataItem.Data       = ObjectConvert.ConvertToDictionary <TItem>(data);
                    dataItem.Count      = data.Count;
                    dataItem.QueryCount = 1;
                }
                catch (Exception ero)
                {
                    MemoryDataCacheItem item;
                    cacheDatas.TryRemove(key, out item);
                    throw ero;
                }
            }

            if (timer == null)
            {
                StarWatch();
            }
            //更新缓存数据
            if (dataItem.UpdatedData != null)
            {
                dataItem.Data        = ObjectConvert.ConvertToDictionary <TItem>(dataItem.UpdatedData);
                dataItem.UpdatedData = null;
            }
            cacheDatas[key].UseTime = DateTime.Now;
            return(dataItem.Data as Dictionary <string, TItem>);
        }
Пример #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>);
        }
Пример #4
0
        internal static Dictionary <int, TItem> GetCacheList <TItem>(string qeury, int timeOut, DBHelper helper, out string key) where TItem : IModel, new()
        {
            Type type = typeof(TItem);

            qeury = qeury.ToLower();
            string Params = "";

            foreach (KeyValuePair <string, object> item in helper.Params)
            {
                Params += item.Key + ":" + item.Value;
            }
            //按参数进行缓存
            key = StringHelper.EncryptMD5(qeury + Params);
            //初始缓存
            lock (lockObj)
            {
                if (!cacheDatas.ContainsKey(key))
                {
                    cacheDatas.Add(key, new MemoryDataCacheItem()
                    {
                        Data = null, TimeOut = timeOut, DBHelper = helper, Query = qeury, Params = new Dictionary <string, object>(helper.Params), Type = type
                    });
                    if (typeCache.ContainsKey(type))
                    {
                        typeCache[type].Add(key);
                    }
                    else
                    {
                        typeCache[type] = new List <string>()
                        {
                            key
                        };
                    }
                }
                else
                {
                    var dataItem2 = cacheDatas[key];
                    if (dataItem2.QueryCount == 0)//缓存没有创建好时返回空
                    {
                        return(new Dictionary <int, TItem>());
                    }
                }
            }
            var dataItem = cacheDatas[key];

            //首次查询
            if (dataItem.QueryCount == 0)
            {
                var data = QueryData(key, type, qeury, helper);
                dataItem.Data       = ObjectConvert.ConvertToDictionary <TItem>(data);
                dataItem.Count      = data.Count;
                dataItem.QueryCount = 1;
            }

            if (thread == null)
            {
                StarWatch();
            }
            if (dataItem.UpdatedData != null)
            {
                dataItem.Data        = ObjectConvert.ConvertToDictionary <TItem>(dataItem.UpdatedData);
                dataItem.UpdatedData = null;
            }
            cacheDatas[key].UseTime = DateTime.Now;
            return(dataItem.Data as Dictionary <int, TItem>);
        }