예제 #1
0
        public T ProcessExecutedCommands <T>(DbCommand command, DbContext context, T result)
        {
            if (command is null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (result is DataReader)
            {
                return(result);
            }

            var commandText = command.CommandText;
            var cachePolicy = _cachePolicyManager.GetCachePolicy(commandText);

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

            var cacheKey = CacheKeyProvider.GetKey(command, context, cachePolicy);

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

                _cacheProvider.Set(cacheKey.ToString(), new CachedData {
                    TableRows = tableRows
                },
                                   cachePolicy.CacheTimeout);

                return((T)(object)new DataReader(tableRows));
            }

            return(result);
        }