Пример #1
0
        /// <summary>
        /// Reads data from cache or cache it and then returns the result
        /// </summary>
        public T ProcessExecutedCommands <T>(DbCommand command, DbContext context, T result)
        {
            if (result is EFTableRowsDataReader rowsReader)
            {
                _logger.LogDebug(CacheableEventId.CacheHit, $"Returning the cached TableRows[{rowsReader.TableName}].");
                return(result);
            }

            if (_cacheDependenciesProcessor.InvalidateCacheDependencies(command, context, new EFCachePolicy()))
            {
                return(result);
            }

            var allEntityTypes = _sqlCommandsProcessor.GetAllTableNames(context);
            var cachePolicy    = _cachePolicyParser.GetEFCachePolicy(command.CommandText, allEntityTypes);

            if (cachePolicy == null)
            {
                return(result);
            }

            var efCacheKey = _cacheKeyProvider.GetEFCacheKey(command, context, cachePolicy);

            if (result is int data)
            {
                _cacheService.InsertValue(efCacheKey, new EFCachedData {
                    NonQuery = data
                }, cachePolicy);
                _logger.LogDebug(CacheableEventId.QueryResultCached, $"[{data}] added to the cache[{efCacheKey}].");
                return(result);
            }

            if (result is DbDataReader dataReader)
            {
                EFTableRows tableRows;
                using (var dbReaderLoader = new EFDataReaderLoader(dataReader))
                {
                    tableRows = dbReaderLoader.LoadAndClose();
                }

                _cacheService.InsertValue(efCacheKey, new EFCachedData {
                    TableRows = tableRows
                }, cachePolicy);
                _logger.LogDebug(CacheableEventId.QueryResultCached, $"TableRows[{tableRows.TableName}] added to the cache[{efCacheKey}].");
                return((T)(object)new EFTableRowsDataReader(tableRows));
            }

            if (result is object)
            {
                _cacheService.InsertValue(efCacheKey, new EFCachedData {
                    Scalar = result
                }, cachePolicy);
                _logger.LogDebug(CacheableEventId.QueryResultCached, $"[{result}] added to the cache[{efCacheKey}].");
                return(result);
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        /// Finds the related table names of the current query.
        /// </summary>
        public SortedSet <string> GetCacheDependencies(DbCommand command, DbContext context, EFCachePolicy cachePolicy)
        {
            var tableNames = new SortedSet <string>(
                _sqlCommandsProcessor.GetAllTableNames(context).Select(x => x.TableName));

            return(GetCacheDependencies(cachePolicy, tableNames, command.CommandText));
        }
        /// <summary>
        /// Finds the related table names of the current query.
        /// </summary>
        public SortedSet <string> GetCacheDependencies(DbCommand command, DbContext context, EFCachePolicy cachePolicy)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var tableNames = new SortedSet <string>(
                _sqlCommandsProcessor.GetAllTableNames(context).Select(x => x.TableName),
                StringComparer.OrdinalIgnoreCase);

            return(GetCacheDependencies(cachePolicy, tableNames, command.CommandText));
        }
        private EFCachePolicy?getCachePolicy(DbContext context, string commandText)
        {
            var allEntityTypes = _sqlCommandsProcessor.GetAllTableNames(context);

            return(_cachePolicyParser.GetEFCachePolicy(commandText, allEntityTypes));
        }