예제 #1
0
        public static IQueryable <T> Cache <T>(string cacheKey, int expireCacheSecondTime = 60, bool canUseCacheIfPossible = true)
        {
            var            cacheInfo = CacheConfig.CacheInfoDic[cacheKey];
            var            queryableCacheExecution = new QueryableCacheDataProvider <T>(cacheInfo);
            IQueryable <T> result = queryableCacheExecution.Cache <IQueryable>(cacheInfo, expireCacheSecondTime, cacheKey, canUseCacheIfPossible) as IQueryable <T>;

            return(result);
        }
예제 #2
0
        private IEnumerable <T> GetNavigationPropertyDataAtFirstTime <T>(string navigationPropertyName, out object resultForSecondLevelDataSource) where T : ObjectBase
        {
            resultForSecondLevelDataSource = null;
            var infoForFillingNavigationProperty = EntityInfo().InfoForFillingNavigationPropertyDic[navigationPropertyName];

            InitForFillingNavigationProperty_JustOneTime(infoForFillingNavigationProperty);

            IEnumerable <T> result = null;

            if (!NavigationPropertyDataDic.ContainsKey(navigationPropertyName))
            {
                var thisPropertyValue = this[infoForFillingNavigationProperty.ThisEntityRefrencePropertyName];
                var propertyType      = EntityInfo().Properties[infoForFillingNavigationProperty.ThisEntityRefrencePropertyName].PropertyType;
                var cacheInfo         = CacheConfig.CacheInfoDic.First(ci => ci.Value.Name == infoForFillingNavigationProperty.CacheName).Value;
                if (thisPropertyValue != Activator.CreateInstance(propertyType))
                {
                    lock (cacheInfo.InfoAndEntityListForFillingNavigationPropertyDic)
                    {
                        if (!NavigationPropertyDataDic.ContainsKey(navigationPropertyName))
                        {
                            if (thisPropertyValue != Activator.CreateInstance(propertyType))
                            {
                                if (cacheInfo.DisableCache)
                                {
                                    ObjectBase resultItem;
                                    result = (cacheInfo.MethodInfo.Invoke(null, new object[] { cacheInfo.Repository.GetQueryableForCahce(AppBase.DependencyInjectionFactory.CreateContextInstance()) }) as IQueryable <T>)
                                             .Where(string.Format("{0} == {1}", infoForFillingNavigationProperty.OtherEntityRefrencePropertyName, thisPropertyValue));
                                    resultItem = result.FirstOrDefault() as ObjectBase;
                                    if (resultItem != null)
                                    {
                                        resultItem.EnableFillNavigationProperyByCache();
                                    }
                                }
                                else
                                {
                                    var queryableCacheExecution = new QueryableCacheDataProvider <T>(cacheInfo);
                                    //  _stopwatch.Restart();
                                    result = queryableCacheExecution.Cache <List <T> >(cacheInfo, cacheInfo.AutoRefreshInterval, cacheInfo.BasicKey.ToString(), true).
                                             Where(item =>
                                    {
                                        var otherPropertyValue = item[infoForFillingNavigationProperty.OtherEntityRefrencePropertyName];
                                        return(otherPropertyValue != null && otherPropertyValue.Equals(thisPropertyValue));
                                    });
                                    // result.ToList().ForEach(item => (item as _EntityBase).EnableFillNavigationProperyByCache = true);
                                    // _stopwatch.Stop();
                                    // cacheInfo.UsingTime += TimeSpan.FromTicks(_stopwatch.ElapsedTicks);
                                }

                                if (!infoForFillingNavigationProperty.IsEnumerable)
                                {
                                    if (result.Count() == 0 && !string.IsNullOrEmpty(infoForFillingNavigationProperty.SecondLevelDataSourceName))
                                    {
                                        var dataSourceInfo = CacheConfig.CacheInfoDic.First(ci => ci.Value.Name == infoForFillingNavigationProperty.SecondLevelDataSourceName).Value;
                                        resultForSecondLevelDataSource = ((dataSourceInfo.MethodInfo.Invoke(null, new object[] { dataSourceInfo.Repository.GetQueryableForCahce(AppBase.DependencyInjectionFactory.CreateContextInstance()) }) as IQueryable)
                                                                          .Where(string.Format("{0} == {1}", infoForFillingNavigationProperty.OtherEntityRefrencePropertyName, thisPropertyValue)) as IEnumerable).Cast <IEntity>().FirstOrDefault();
                                        if (resultForSecondLevelDataSource != null)
                                        {
                                            ((ObjectBase)resultForSecondLevelDataSource).EnableFillNavigationProperyByCache();
                                        }
                                    }

                                    ObjectBase resultItem;
                                    if (!cacheInfo.DisableCache && result.Count() == 0)
                                    {
                                        //ToDo: Change invoke method by excact delegate for better performance.
                                        result = (cacheInfo.MethodInfo.Invoke(null, new object[] { cacheInfo.Repository.GetQueryableForCahce(AppBase.DependencyInjectionFactory.CreateContextInstance()) }) as IQueryable <T>)
                                                 .Where(string.Format("{0} == {1}", infoForFillingNavigationProperty.OtherEntityRefrencePropertyName, thisPropertyValue));
                                        resultItem = result.FirstOrDefault() as ObjectBase;
                                        if (resultItem != null)
                                        {
                                            resultItem.EnableFillNavigationProperyByCache();
                                        }
                                        //item => item[infoForFillingNavigationProperty.OtherEntityRefrencePropertyName].Equals(thisPropertyValue));
                                    }
                                }
                            }
                            else
                            {
                                result = new List <T>();
                            }

                            if (infoForFillingNavigationProperty.IsEnumerable)
                            {
                                NavigationPropertyDataDic[navigationPropertyName] = result.ToList <T>();
                            }
                            else
                            {
                                if (resultForSecondLevelDataSource == null)
                                {
                                    if (result != null)
                                    {
                                        NavigationPropertyDataDic[navigationPropertyName] = result.FirstOrDefault();
                                    }
                                }
                                else
                                {
                                    NavigationPropertyDataDic[navigationPropertyName] = resultForSecondLevelDataSource;
                                }
                            }
                        }
                        else
                        {
                            result = NavigationPropertyDataDic[navigationPropertyName] as List <T>;
                        }

                        var entities = cacheInfo.InfoAndEntityListForFillingNavigationPropertyDic[infoForFillingNavigationProperty];
                        lock (entities)
                        {
                            cacheInfo.InfoAndEntityListForFillingNavigationPropertyDic[infoForFillingNavigationProperty].Add(this);
                        }
                    }
                }
                else
                {
                    result = new List <T>();
                }
            }
            else
            {
                result = NavigationPropertyDataDic[navigationPropertyName] as List <T>;
            }

            return(result);
        }