/// <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); }
/// <summary> /// Reads data from cache or cache it and then returns the result /// </summary> public T ProcessExecutedCommands <T>(DbCommand command, DbContext context, T result, [CallerMemberName] string methodName = null) { if (result is EFTableRowsDataReader rowsReader) { _logger.LogInformation(CacheableEventId.CacheHit, $"Using the TableRows[{rowsReader.TableName}] from the cache."); return(result); } if (_cacheDependenciesProcessor.InvalidateCacheDependencies(command, context, new EFCachePolicy())) { return(result); } var cachePolicy = _cachePolicyParser.GetEFCachePolicy(command.CommandText); if (cachePolicy != null) { var efCacheKey = _cacheKeyProvider.GetEFCacheKey(command, context, cachePolicy); if (result is int data) { _cacheService.InsertValue(efCacheKey, new EFCachedData { NonQuery = data }, cachePolicy); _logger.LogInformation(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.LogInformation(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.LogInformation(CacheableEventId.QueryResultCached, $"[{result}] added to the cache[{efCacheKey}]."); return(result); } } return(result); }
/// <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 (command == null) { throw new ArgumentNullException(nameof(command)); } if (result is EFTableRowsDataReader rowsReader) { _logger.LogDebug(CacheableEventId.CacheHit, $"Returning the cached TableRows[{rowsReader.TableName}]."); return(result); } var commandText = command.CommandText; var cachePolicy = getCachePolicy(context, commandText); var efCacheKey = _cacheKeyProvider.GetEFCacheKey(command, context, cachePolicy ?? new EFCachePolicy()); if (_cacheDependenciesProcessor.InvalidateCacheDependencies(commandText, efCacheKey)) { return(result); } if (cachePolicy == null) { _logger.LogDebug($"Skipping a none-cachable command[{commandText}]."); return(result); } if (result is int data) { if (!shouldSkipCachingResults(commandText, 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(); } if (!shouldSkipCachingResults(commandText, tableRows)) { _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) { if (!shouldSkipCachingResults(commandText, result)) { _cacheService.InsertValue(efCacheKey, new EFCachedData { Scalar = result }, cachePolicy); _logger.LogDebug(CacheableEventId.QueryResultCached, $"[{result}] added to the cache[{efCacheKey}]."); } return(result); } return(result); }