1- using System ;
21using System . Data . Common ;
32using System . Globalization ;
4- using System . Threading ;
53using Microsoft . EntityFrameworkCore ;
64using Microsoft . EntityFrameworkCore . Diagnostics ;
75using Microsoft . Extensions . Logging ;
108namespace EFCoreSecondLevelCacheInterceptor ;
119
1210/// <summary>
13- /// Helps processing SecondLevelCacheInterceptor
11+ /// Helps process SecondLevelCacheInterceptor
1412/// </summary>
1513public class DbCommandInterceptorProcessor : IDbCommandInterceptorProcessor
1614{
1715 private readonly IEFCacheDependenciesProcessor _cacheDependenciesProcessor ;
1816 private readonly IEFCacheKeyProvider _cacheKeyProvider ;
19- private readonly IEFCachePolicyParser _cachePolicyParser ;
2017 private readonly IEFCacheServiceProvider _cacheService ;
2118 private readonly IEFCacheServiceCheck _cacheServiceCheck ;
2219 private readonly EFCoreSecondLevelCacheSettings _cacheSettings ;
20+ private readonly IDbCommandIgnoreCachingProcessor _ignoreCachingProcessor ;
2321 private readonly ILogger < DbCommandInterceptorProcessor > _interceptorProcessorLogger ;
2422 private readonly IEFDebugLogger _logger ;
25- private readonly IEFSqlCommandsProcessor _sqlCommandsProcessor ;
2623
2724 /// <summary>
28- /// Helps processing SecondLevelCacheInterceptor
25+ /// Helps process SecondLevelCacheInterceptor
2926 /// </summary>
3027 public DbCommandInterceptorProcessor ( IEFDebugLogger logger ,
3128 ILogger < DbCommandInterceptorProcessor > interceptorProcessorLogger ,
3229 IEFCacheServiceProvider cacheService ,
3330 IEFCacheDependenciesProcessor cacheDependenciesProcessor ,
3431 IEFCacheKeyProvider cacheKeyProvider ,
35- IEFCachePolicyParser cachePolicyParser ,
36- IEFSqlCommandsProcessor sqlCommandsProcessor ,
3732 IOptions < EFCoreSecondLevelCacheSettings > cacheSettings ,
38- IEFCacheServiceCheck cacheServiceCheck )
33+ IEFCacheServiceCheck cacheServiceCheck ,
34+ IDbCommandIgnoreCachingProcessor ignoreCachingProcessor )
3935 {
4036 _cacheService = cacheService ;
4137 _cacheDependenciesProcessor = cacheDependenciesProcessor ;
4238 _cacheKeyProvider = cacheKeyProvider ;
43- _cachePolicyParser = cachePolicyParser ;
4439 _logger = logger ;
4540 _interceptorProcessorLogger = interceptorProcessorLogger ;
46- _sqlCommandsProcessor = sqlCommandsProcessor ;
4741 _cacheServiceCheck = cacheServiceCheck ;
42+ _ignoreCachingProcessor = ignoreCachingProcessor ;
4843
4944 if ( cacheSettings == null )
5045 {
@@ -73,7 +68,7 @@ public T ProcessExecutedCommands<T>(DbCommand command, DbContext? context, T res
7368 return result ;
7469 }
7570
76- var ( shouldSkipProcessing , cachePolicy ) = ShouldSkipProcessing ( command , context ) ;
71+ var ( shouldSkipProcessing , cachePolicy ) = _ignoreCachingProcessor . ShouldSkipProcessing ( command , context ) ;
7772
7873 if ( shouldSkipProcessing )
7974 {
@@ -116,7 +111,7 @@ public T ProcessExecutedCommands<T>(DbCommand command, DbContext? context, T res
116111
117112 if ( result is int data )
118113 {
119- if ( ! ShouldSkipCachingResults ( commandText , data ) )
114+ if ( ! _ignoreCachingProcessor . ShouldSkipCachingResults ( commandText , data ) )
120115 {
121116 _cacheService . InsertValue ( efCacheKey , new EFCachedData
122117 {
@@ -145,7 +140,7 @@ public T ProcessExecutedCommands<T>(DbCommand command, DbContext? context, T res
145140 tableRows = dbReaderLoader . Load ( ) ;
146141 }
147142
148- if ( ! ShouldSkipCachingResults ( commandText , tableRows ) )
143+ if ( ! _ignoreCachingProcessor . ShouldSkipCachingResults ( commandText , tableRows ) )
149144 {
150145 _cacheService . InsertValue ( efCacheKey , new EFCachedData
151146 {
@@ -171,7 +166,7 @@ public T ProcessExecutedCommands<T>(DbCommand command, DbContext? context, T res
171166
172167 if ( result is object )
173168 {
174- if ( ! ShouldSkipCachingResults ( commandText , result ) )
169+ if ( ! _ignoreCachingProcessor . ShouldSkipCachingResults ( commandText , result ) )
175170 {
176171 _cacheService . InsertValue ( efCacheKey , new EFCachedData
177172 {
@@ -231,7 +226,7 @@ public T ProcessExecutingCommands<T>(DbCommand command, DbContext? context, T re
231226 return result ;
232227 }
233228
234- var ( shouldSkipProcessing , cachePolicy ) = ShouldSkipProcessing ( command , context ) ;
229+ var ( shouldSkipProcessing , cachePolicy ) = _ignoreCachingProcessor . ShouldSkipProcessing ( command , context ) ;
235230
236231 if ( shouldSkipProcessing )
237232 {
@@ -371,83 +366,4 @@ public T ProcessExecutingCommands<T>(DbCommand command, DbContext? context, T re
371366 return result ;
372367 }
373368 }
374-
375- /// <summary>
376- /// Is this command marked for caching?
377- /// </summary>
378- private ( bool ShouldSkipProcessing , EFCachePolicy ? CachePolicy ) ShouldSkipProcessing ( DbCommand ? command ,
379- DbContext ? context ,
380- CancellationToken cancellationToken = default )
381- {
382- if ( context is null || command is null )
383- {
384- return ( true , null ) ;
385- }
386-
387- if ( cancellationToken . IsCancellationRequested )
388- {
389- return ( true , null ) ;
390- }
391-
392- var commandCommandText = command . CommandText ?? "" ;
393-
394- if ( ShouldSkipCachingDbContext ( context , commandCommandText ) )
395- {
396- return ( true , null ) ;
397- }
398-
399- var cachePolicy = GetCachePolicy ( context , commandCommandText ) ;
400-
401- if ( ShouldSkipQueriesInsideExplicitTransaction ( command ) )
402- {
403- return ( ! _sqlCommandsProcessor . IsCrudCommand ( commandCommandText ) , cachePolicy ) ;
404- }
405-
406- if ( _sqlCommandsProcessor . IsCrudCommand ( commandCommandText ) )
407- {
408- return ( false , cachePolicy ) ;
409- }
410-
411- return cachePolicy is null ? ( true , null ) : ( false , cachePolicy ) ;
412- }
413-
414- private bool ShouldSkipCachingDbContext ( DbContext context , string commandText )
415- {
416- var result = _cacheSettings . SkipCachingDbContexts is not null &&
417- _cacheSettings . SkipCachingDbContexts . Contains ( context . GetType ( ) ) ;
418-
419- if ( result && _logger . IsLoggerEnabled )
420- {
421- var message = $ "Skipped caching of this DbContext: { context . GetType ( ) } ";
422- _interceptorProcessorLogger . LogDebug ( message ) ;
423- _logger . NotifyCacheableEvent ( CacheableLogEventId . CachingSkipped , message , commandText , efCacheKey : null ) ;
424- }
425-
426- return result ;
427- }
428-
429- private bool ShouldSkipQueriesInsideExplicitTransaction ( DbCommand ? command )
430- => ! _cacheSettings . AllowCachingWithExplicitTransactions && command ? . Transaction is not null ;
431-
432- private EFCachePolicy ? GetCachePolicy ( DbContext context , string commandText )
433- {
434- var allEntityTypes = _sqlCommandsProcessor . GetAllTableNames ( context ) ;
435-
436- return _cachePolicyParser . GetEFCachePolicy ( commandText , allEntityTypes ) ;
437- }
438-
439- private bool ShouldSkipCachingResults ( string commandText , object value )
440- {
441- var result = _cacheSettings . SkipCachingResults != null &&
442- _cacheSettings . SkipCachingResults ( ( commandText , value ) ) ;
443-
444- if ( result && _logger . IsLoggerEnabled )
445- {
446- var message = "Skipped caching of this result based on the provided predicate." ;
447- _interceptorProcessorLogger . LogDebug ( message ) ;
448- _logger . NotifyCacheableEvent ( CacheableLogEventId . CachingSkipped , message , commandText , efCacheKey : null ) ;
449- }
450-
451- return result ;
452- }
453369}
0 commit comments