private EFCachePolicy?getSpecificGlobalPolicy(string commandText, IList <TableEntityInfo> allEntityTypes) { var options = _cacheSettings.CacheSpecificQueriesOptions; if (options?.IsActive != true || _sqlCommandsProcessor.IsCrudCommand(commandText) || commandText.Contains(EFCachedQueryExtensions.IsNotCachableMarker, StringComparison.Ordinal)) { return(null); } var shouldBeCached = false; if (options.TableNames != null) { var commandTableNames = _sqlCommandsProcessor.GetSqlCommandTableNames(commandText); if (options.TableNames.Any(tableName => commandTableNames.Contains(tableName, StringComparer.OrdinalIgnoreCase))) { shouldBeCached = true; } } if (options.EntityTypes != null) { var queryEntityTypes = _sqlCommandsProcessor.GetSqlCommandEntityTypes(commandText, allEntityTypes); if (queryEntityTypes.Any(entityType => options.EntityTypes.Contains(entityType))) { shouldBeCached = true; } } return(shouldBeCached ? new EFCachePolicy().ExpirationMode(options.ExpirationMode).Timeout(options.Timeout) : null); }
/// <summary> /// Finds the related table names of the current query. /// </summary> public SortedSet <string> GetCacheDependencies(EFCachePolicy cachePolicy, SortedSet <string> tableNames, string commandText) { if (cachePolicy == null) { throw new ArgumentNullException(nameof(cachePolicy)); } var textsInsideSquareBrackets = _sqlCommandsProcessor.GetSqlCommandTableNames(commandText); var cacheDependencies = new SortedSet <string>( tableNames.Intersect(textsInsideSquareBrackets, StringComparer.OrdinalIgnoreCase), StringComparer.OrdinalIgnoreCase); if (cacheDependencies.Any()) { logProcess(tableNames, textsInsideSquareBrackets, cacheDependencies); return(cacheDependencies); } cacheDependencies = cachePolicy.CacheItemsDependencies as SortedSet <string>; if (cacheDependencies?.Any() != true) { _logger.LogDebug($"It's not possible to calculate the related table names of the current query[{commandText}]. Please use EFCachePolicy.Configure(options => options.CacheDependencies(\"real_table_name_1\", \"real_table_name_2\")) to specify them explicitly."); cacheDependencies = new SortedSet <string>(StringComparer.OrdinalIgnoreCase) { EFCachePolicy.EFUnknownsCacheDependency }; } logProcess(tableNames, textsInsideSquareBrackets, cacheDependencies); return(cacheDependencies); }