Пример #1
0
        /// <exclude />
        internal static IQueryable <T> GetDataFromCache <T>(Func <IQueryable <T> > getQueryFunc)
            where T : class, IData
        {
            Verify.That(_isEnabled, "The cache is disabled.");

            DataScopeIdentifier dataScopeIdentifier = DataScopeManager.MapByType(typeof(T));
            CultureInfo         localizationScope   = LocalizationScopeManager.MapByType(typeof(T));

            var typeData = _cachedData.GetOrAdd(typeof(T), t => new TypeData());

            var dataScopeData = typeData.GetOrAdd(dataScopeIdentifier, scope => new ConcurrentDictionary <CultureInfo, CachedTable>());


            CachedTable cachedTable;

            if (!dataScopeData.TryGetValue(localizationScope, out cachedTable))
            {
                IQueryable <T> wholeTable = getQueryFunc();

                if (!DataProvidersSupportDataWrapping(typeof(T)))
                {
                    DisableCachingForType(typeof(T));

                    return(Verify.ResultNotNull(wholeTable));
                }

                if (_maximumSize != -1)
                {
                    List <IData> cuttedTable = TakeElements(wholeTable, _maximumSize + 1);
                    if (cuttedTable.Count > _maximumSize)
                    {
                        DisableCachingForType(typeof(T));

                        return(Verify.ResultNotNull(wholeTable));
                    }
                    cachedTable = new CachedTable(cuttedTable.Cast <T>().AsQueryable());
                }
                else
                {
                    cachedTable = new CachedTable(wholeTable.Evaluate().AsQueryable());
                }

                dataScopeData[localizationScope] = cachedTable;
            }

            var typedData = cachedTable.Queryable as IQueryable <T>;

            Verify.IsNotNull(typedData, "Cached value is invalid.");

            // Leaving a possibility to extract original query
            Func <IQueryable> originalQueryGetter = () =>
            {
                using (new DataScope(dataScopeIdentifier, localizationScope))
                {
                    return(getQueryFunc());
                }
            };

            return(new CachingQueryable <T>(cachedTable, originalQueryGetter));
        }
Пример #2
0
        private CachedTable GetCachedTable()
        {
            if (_cachedTable == null)
            {
                lock (this)
                {
                    _cachedTable = _cachedTable ?? new CachedTable <T>(GetOriginalQuery().Cast <T>().ToList());
                }
            }

            return(_cachedTable);
        }
Пример #3
0
        public CachingQueryable(CachedTable cachedTable, Func <IQueryable> originalQueryGetter)
        {
            _cachedTable = cachedTable;

            _source            = cachedTable.Queryable;
            _currentExpression = Expression.Constant(this);
            _getQueryFunc      = originalQueryGetter;

            if (_source is IEnumerable <T> )
            {
                _innerEnumerable = _source as IEnumerable <T>;
                if (_innerEnumerable is CachingEnumerable <T> )
                {
                    _wrappedEnumerable = _innerEnumerable as CachingEnumerable <T>;
                }
            }
        }
Пример #4
0
        /// <exclude />
        internal static IQueryable <T> GetDataFromCache <T>(Func <IQueryable <T> > getQueryFunc)
            where T : class, IData
        {
            Verify.That(_isEnabled, "The cache is disabled.");

            DataScopeIdentifier dataScopeIdentifier = DataScopeManager.MapByType(typeof(T));
            CultureInfo         localizationScope   = LocalizationScopeManager.MapByType(typeof(T));

            var cachedDataset = _resourceLocker.Resources.CachedData;

            Hashtable <DataScopeIdentifier, Hashtable <CultureInfo, CachedTable> > dataScopeData;

            if (!cachedDataset.TryGetValue(typeof(T), out dataScopeData))
            {
                using (_resourceLocker.Locker)
                {
                    if (!cachedDataset.TryGetValue(typeof(T), out dataScopeData))
                    {
                        dataScopeData = new Hashtable <DataScopeIdentifier, Hashtable <CultureInfo, CachedTable> >();

                        cachedDataset.Add(typeof(T), dataScopeData);
                    }
                }
            }

            Hashtable <CultureInfo, CachedTable> localizationScopeData;

            if (!dataScopeData.TryGetValue(dataScopeIdentifier, out localizationScopeData))
            {
                using (_resourceLocker.Locker)
                {
                    if (!dataScopeData.TryGetValue(dataScopeIdentifier, out localizationScopeData))
                    {
                        localizationScopeData = new Hashtable <CultureInfo, CachedTable>();
                        dataScopeData.Add(dataScopeIdentifier, localizationScopeData);
                    }
                }
            }

            CachedTable cachedTable;

            if (!localizationScopeData.TryGetValue(localizationScope, out cachedTable))
            {
                IQueryable <T> wholeTable = getQueryFunc();

                if (!DataProvidersSupportDataWrapping(typeof(T)))
                {
                    DisableCachingForType(typeof(T));

                    return(Verify.ResultNotNull(wholeTable));
                }

                if (_maximumSize != -1)
                {
                    List <IData> cuttedTable = TakeElements(wholeTable, _maximumSize + 1);
                    if (cuttedTable.Count > _maximumSize)
                    {
                        DisableCachingForType(typeof(T));

                        return(Verify.ResultNotNull(wholeTable));
                    }
                    cachedTable = new CachedTable(cuttedTable.Cast <T>().AsQueryable());
                }
                else
                {
                    cachedTable = new CachedTable(wholeTable.Evaluate().AsQueryable());
                }

                using (_resourceLocker.Locker)
                {
                    if (!localizationScopeData.ContainsKey(localizationScope))
                    {
                        localizationScopeData.Add(localizationScope, cachedTable);
                    }
                }
            }

            var typedData = cachedTable.Queryable as IQueryable <T>;

            Verify.IsNotNull(typedData, "Cached value is invalid.");

            // Leaving a possibility to extract original query
            Func <IQueryable> originalQueryGetter = () =>
            {
                using (new DataScope(dataScopeIdentifier, localizationScope))
                {
                    return(getQueryFunc());
                }
            };

            return(new CachingQueryable <T>(cachedTable, originalQueryGetter));
        }