public static IQueryable <T> RefreshCache <T>(string cacheKey) { var cacheInfo = CacheConfig.CacheInfoDic[cacheKey]; var queryableCacheExecution = new QueryableCacheDataProvider <T>(cacheInfo); IQueryable <T> result = CacheBase.RefreshCache <IQueryable>(queryableCacheExecution, cacheInfo) as IQueryable <T>; return(result); }
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); }
public object GetCacheDataViaWcf(ICacheDataProvider cacheDataProvider) { var clientCacheDataProvider = cacheDataProvider as CacheDataProvider; var serverCacheInfo = CacheConfig.CacheInfoDic.Values.First(item => item.UniqueKeyInServerLevel == clientCacheDataProvider.CacheInfo.UniqueKeyInServerLevel); clientCacheDataProvider.SetFunc(serverCacheInfo.Func); var c = clientCacheDataProvider.CacheInfo; clientCacheDataProvider.CacheInfo = serverCacheInfo; var cacheKey = clientCacheDataProvider.GenerateCacheKey(); Type genericType = serverCacheInfo.MethodInfo.ReturnType; if (typeof(IQueryable).IsAssignableFrom(serverCacheInfo.MethodInfo.ReturnType)) { genericType = typeof(List <>).MakeGenericType(serverCacheInfo.MethodInfo.ReturnType.GenericTypeArguments[0]); } else { genericType = serverCacheInfo.MethodInfo.ReturnType; } var result = (typeof(CacheBase)).GetMethod("Cache").MakeGenericMethod(genericType).Invoke(null, new object[] { cacheDataProvider, serverCacheInfo, serverCacheInfo.AutoRefreshInterval, cacheKey, true }); if (clientCacheDataProvider.CacheInfo.EnableToFetchOnlyChangedDataFromDB) { var resultlist = (result as IList); var resultQueryable = QueryableCacheDataProvider <string> .MakeQueryableForFetchingOnlyChangedDataFromDB(resultlist.AsQueryable(), c, false); List <object> lsttmp = new System.Collections.Generic.List <object>(); System.Diagnostics.Debug.WriteLine("server :" + resultlist.Count); foreach (var item in resultQueryable) { lsttmp.Add(item); } var newlst = Activator.CreateInstance(result.GetType()) as IList; foreach (var item in lsttmp) { newlst.Add(item); } System.Diagnostics.Debug.WriteLine("server after filter:" + resultlist.Count); result = newlst; } if (serverCacheInfo.EnableCoreSerialization) { result = SerializeCacheData(result); } return(result); }
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); }