Skip to content

Commit d6ebd2c

Browse files
committed
Add EFCacheKey to EFCacheableLogEvent
1 parent 14ac0c3 commit d6ebd2c

File tree

11 files changed

+144
-84
lines changed

11 files changed

+144
-84
lines changed

src/EFCoreSecondLevelCacheInterceptor/DbCommandInterceptorProcessor.cs

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public T ProcessExecutedCommands<T>(DbCommand command, DbContext? context, T res
6464
return result;
6565
}
6666

67+
EFCacheKey? efCacheKey = null;
68+
6769
try
6870
{
6971
if (!_cacheServiceCheck.IsCacheServiceAvailable())
@@ -84,14 +86,16 @@ public T ProcessExecutedCommands<T>(DbCommand command, DbContext? context, T res
8486
{
8587
var logMessage = $"Returning the cached TableRows[{rowsReader.TableName}].";
8688
_interceptorProcessorLogger.LogDebug(CacheableEventId.CacheHit, logMessage);
87-
_logger.NotifyCacheableEvent(CacheableLogEventId.CacheHit, logMessage, command.CommandText);
89+
90+
_logger.NotifyCacheableEvent(CacheableLogEventId.CacheHit, logMessage, command.CommandText,
91+
efCacheKey);
8892
}
8993

9094
return result;
9195
}
9296

9397
var commandText = command.CommandText;
94-
var efCacheKey = _cacheKeyProvider.GetEFCacheKey(command, context, cachePolicy ?? new EFCachePolicy());
98+
efCacheKey = _cacheKeyProvider.GetEFCacheKey(command, context, cachePolicy ?? new EFCachePolicy());
9599

96100
if (_cacheDependenciesProcessor.InvalidateCacheDependencies(commandText, efCacheKey))
97101
{
@@ -104,7 +108,7 @@ public T ProcessExecutedCommands<T>(DbCommand command, DbContext? context, T res
104108
{
105109
var message = $"Skipping a none-cachable command[{commandText}].";
106110
_interceptorProcessorLogger.LogDebug(message);
107-
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingSkipped, message, commandText);
111+
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingSkipped, message, commandText, efCacheKey);
108112
}
109113

110114
return result;
@@ -123,7 +127,9 @@ public T ProcessExecutedCommands<T>(DbCommand command, DbContext? context, T res
123127
{
124128
var message = $"[{data}] added to the cache[{efCacheKey}].";
125129
_interceptorProcessorLogger.LogDebug(CacheableEventId.QueryResultCached, message);
126-
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultCached, message, commandText);
130+
131+
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultCached, message, commandText,
132+
efCacheKey);
127133
}
128134
}
129135

@@ -150,7 +156,9 @@ public T ProcessExecutedCommands<T>(DbCommand command, DbContext? context, T res
150156
{
151157
var message = $"TableRows[{tableRows.TableName}] added to the cache[{efCacheKey}].";
152158
_interceptorProcessorLogger.LogDebug(CacheableEventId.QueryResultCached, message);
153-
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultCached, message, commandText);
159+
160+
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultCached, message, commandText,
161+
efCacheKey);
154162
}
155163
}
156164

@@ -174,7 +182,9 @@ public T ProcessExecutedCommands<T>(DbCommand command, DbContext? context, T res
174182
{
175183
var message = $"[{result}] added to the cache[{efCacheKey}].";
176184
_interceptorProcessorLogger.LogDebug(CacheableEventId.QueryResultCached, message);
177-
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultCached, message, commandText);
185+
186+
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultCached, message, commandText,
187+
efCacheKey);
178188
}
179189
}
180190

@@ -193,7 +203,9 @@ public T ProcessExecutedCommands<T>(DbCommand command, DbContext? context, T res
193203
if (_logger.IsLoggerEnabled)
194204
{
195205
_interceptorProcessorLogger.LogCritical(ex, message: "Interceptor Error");
196-
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingError, ex.ToString(), command.CommandText);
206+
207+
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingError, ex.ToString(), command.CommandText,
208+
efCacheKey);
197209
}
198210

199211
return result;
@@ -210,6 +222,8 @@ public T ProcessExecutingCommands<T>(DbCommand command, DbContext? context, T re
210222
return result;
211223
}
212224

225+
EFCacheKey? efCacheKey = null;
226+
213227
try
214228
{
215229
if (!_cacheServiceCheck.IsCacheServiceAvailable())
@@ -232,13 +246,13 @@ public T ProcessExecutingCommands<T>(DbCommand command, DbContext? context, T re
232246
{
233247
var message = $"Skipping a none-cachable command[{commandText}].";
234248
_interceptorProcessorLogger.LogDebug(message);
235-
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingSkipped, message, commandText);
249+
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingSkipped, message, commandText, efCacheKey);
236250
}
237251

238252
return result;
239253
}
240254

241-
var efCacheKey = _cacheKeyProvider.GetEFCacheKey(command, context, cachePolicy);
255+
efCacheKey = _cacheKeyProvider.GetEFCacheKey(command, context, cachePolicy);
242256

243257
if (_cacheService.GetValue(efCacheKey, cachePolicy) is not { } cacheResult)
244258
{
@@ -259,7 +273,9 @@ public T ProcessExecutingCommands<T>(DbCommand command, DbContext? context, T re
259273
{
260274
var message = "Suppressed the result with an empty TableRows.";
261275
_interceptorProcessorLogger.LogDebug(message);
262-
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultSuppressed, message, commandText);
276+
277+
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultSuppressed, message, commandText,
278+
efCacheKey);
263279
}
264280

265281
using var rows = new EFTableRowsDataReader(new EFTableRows()
@@ -278,7 +294,9 @@ public T ProcessExecutingCommands<T>(DbCommand command, DbContext? context, T re
278294
$"Suppressed the result with the TableRows[{cacheResult.TableRows.TableName}] from the cache[{efCacheKey}].";
279295

280296
_interceptorProcessorLogger.LogDebug(message);
281-
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultSuppressed, message, commandText);
297+
298+
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultSuppressed, message, commandText,
299+
efCacheKey);
282300
}
283301

284302
using var dataRows = new EFTableRowsDataReader(cacheResult.TableRows
@@ -299,7 +317,9 @@ public T ProcessExecutingCommands<T>(DbCommand command, DbContext? context, T re
299317
{
300318
var message = $"Suppressed the result with {cachedResult} from the cache[{efCacheKey}].";
301319
_interceptorProcessorLogger.LogDebug(message);
302-
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultSuppressed, message, commandText);
320+
321+
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultSuppressed, message, commandText,
322+
efCacheKey);
303323
}
304324

305325
return (T)Convert.ChangeType(InterceptionResult<int>.SuppressWithResult(cachedResult), typeof(T),
@@ -314,7 +334,9 @@ public T ProcessExecutingCommands<T>(DbCommand command, DbContext? context, T re
314334
{
315335
var message = $"Suppressed the result with {cachedResult} from the cache[{efCacheKey}].";
316336
_interceptorProcessorLogger.LogDebug(message);
317-
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultSuppressed, message, commandText);
337+
338+
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultSuppressed, message, commandText,
339+
efCacheKey);
318340
}
319341

320342
return (T)Convert.ChangeType(
@@ -326,7 +348,7 @@ public T ProcessExecutingCommands<T>(DbCommand command, DbContext? context, T re
326348
{
327349
var message = $"Skipped the result with {result?.GetType()} type.";
328350
_interceptorProcessorLogger.LogDebug(message);
329-
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingSkipped, message, commandText);
351+
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingSkipped, message, commandText, efCacheKey);
330352
}
331353

332354
return result;
@@ -341,7 +363,9 @@ public T ProcessExecutingCommands<T>(DbCommand command, DbContext? context, T re
341363
if (_logger.IsLoggerEnabled)
342364
{
343365
_interceptorProcessorLogger.LogCritical(ex, message: "Interceptor Error");
344-
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingError, ex.ToString(), command.CommandText);
366+
367+
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingError, ex.ToString(), command.CommandText,
368+
efCacheKey);
345369
}
346370

347371
return result;
@@ -396,7 +420,7 @@ private bool ShouldSkipCachingDbContext(DbContext context, string commandText)
396420
{
397421
var message = $"Skipped caching of this DbContext: {context.GetType()}";
398422
_interceptorProcessorLogger.LogDebug(message);
399-
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingSkipped, message, commandText);
423+
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingSkipped, message, commandText, efCacheKey: null);
400424
}
401425

402426
return result;
@@ -421,7 +445,7 @@ private bool ShouldSkipCachingResults(string commandText, object value)
421445
{
422446
var message = "Skipped caching of this result based on the provided predicate.";
423447
_interceptorProcessorLogger.LogDebug(message);
424-
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingSkipped, message, commandText);
448+
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingSkipped, message, commandText, efCacheKey: null);
425449
}
426450

427451
return result;

src/EFCoreSecondLevelCacheInterceptor/EFCacheDependenciesProcessor.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public SortedSet<string> GetCacheDependencies(EFCachePolicy cachePolicy,
9595
$"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.";
9696

9797
_dependenciesProcessorLogger.LogDebug(message);
98-
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingError, message, commandText);
98+
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingError, message, commandText, efCacheKey: null);
9999
}
100100

101101
cacheDependencies = new SortedSet<string>(StringComparer.OrdinalIgnoreCase)
@@ -125,7 +125,7 @@ public bool InvalidateCacheDependencies(string commandText, EFCacheKey cacheKey)
125125
{
126126
var message = $"Skipped invalidating a none-CRUD command[{commandText}].";
127127
_dependenciesProcessorLogger.LogDebug(message);
128-
_logger.NotifyCacheableEvent(CacheableLogEventId.InvalidationSkipped, message, commandText);
128+
_logger.NotifyCacheableEvent(CacheableLogEventId.InvalidationSkipped, message, commandText, cacheKey);
129129
}
130130

131131
return false;
@@ -139,7 +139,7 @@ public bool InvalidateCacheDependencies(string commandText, EFCacheKey cacheKey)
139139
$"Skipped invalidating the related cache entries of this query[{commandText}] based on the provided predicate.";
140140

141141
_dependenciesProcessorLogger.LogDebug(message);
142-
_logger.NotifyCacheableEvent(CacheableLogEventId.InvalidationSkipped, message, commandText);
142+
_logger.NotifyCacheableEvent(CacheableLogEventId.InvalidationSkipped, message, commandText, cacheKey);
143143
}
144144

145145
return false;
@@ -153,7 +153,7 @@ public bool InvalidateCacheDependencies(string commandText, EFCacheKey cacheKey)
153153
{
154154
var message = $"Invalidated [{string.Join(separator: ", ", cacheKey.CacheDependencies)}] dependencies.";
155155
_dependenciesProcessorLogger.LogDebug(CacheableEventId.QueryResultInvalidated, message);
156-
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultInvalidated, message, commandText);
156+
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultInvalidated, message, commandText, cacheKey);
157157
}
158158

159159
_logger.NotifyCacheInvalidation(clearAllCachedEntries: false, cacheKey.CacheDependencies);
@@ -179,7 +179,9 @@ private void LogProcess(SortedSet<string> tableNames,
179179
$"ContextTableNames: {names}, PossibleQueryTableNames: {texts} -> CacheDependencies: {dependencies}.";
180180

181181
_dependenciesProcessorLogger.LogDebug(message);
182-
_logger.NotifyCacheableEvent(CacheableLogEventId.CacheDependenciesCalculated, message, commandText);
182+
183+
_logger.NotifyCacheableEvent(CacheableLogEventId.CacheDependenciesCalculated, message, commandText,
184+
efCacheKey: null);
183185
}
184186
}
185187

src/EFCoreSecondLevelCacheInterceptor/EFCacheManagerCoreProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ public void InvalidateCacheDependencies(EFCacheKey cacheKey)
143143
$"Invalidated all of the cache entries due to early expiration of a root cache key[{rootCacheKey}].";
144144

145145
_cacheManagerCoreProviderLogger.LogDebug(CacheableEventId.QueryResultInvalidated, message);
146-
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultInvalidated, message, commandText: "");
146+
147+
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultInvalidated, message, commandText: "",
148+
cacheKey);
147149
}
148150

149151
ClearAllCachedEntries();

src/EFCoreSecondLevelCacheInterceptor/EFCachePolicyParser.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ public string RemoveEFCachePolicyTag(string commandText)
131131
{
132132
var message = $"Using EFCachePolicy: {efCachePolicy}.";
133133
_policyParserLogger.LogDebug(message: "Using EFCachePolicy: {EfCachePolicy}.", efCachePolicy);
134-
_logger.NotifyCacheableEvent(CacheableLogEventId.CachePolicyCalculated, message, commandText);
134+
135+
_logger.NotifyCacheableEvent(CacheableLogEventId.CachePolicyCalculated, message, commandText,
136+
efCacheKey: null);
135137
}
136138

137139
return efCachePolicy;
@@ -145,7 +147,7 @@ private bool ShouldSkipCachingCommands(string commandText)
145147
{
146148
var message = $"Skipped caching of this command[{commandText}] based on the provided predicate.";
147149
_policyParserLogger.LogDebug(message);
148-
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingSkipped, message, commandText);
150+
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingSkipped, message, commandText, efCacheKey: null);
149151
}
150152

151153
return result;
@@ -446,7 +448,9 @@ private bool ContainsNonDeterministicFunction(string commandText)
446448
{
447449
var message = $"Skipped caching because of the non-deterministic function -> `{item}`.";
448450
_policyParserLogger.LogDebug(message);
449-
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingSkipped, message, commandText);
451+
452+
_logger.NotifyCacheableEvent(CacheableLogEventId.CachingSkipped, message, commandText,
453+
efCacheKey: null);
450454
}
451455

452456
return hasFn;

src/EFCoreSecondLevelCacheInterceptor/EFCacheableLogEvent.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public class EFCacheableLogEvent
2222
/// </summary>
2323
public string CommandText { set; get; } = default!;
2424

25+
/// <summary>
26+
/// Stores information of the computed key of the input LINQ query.
27+
/// </summary>
28+
public EFCacheKey? EFCacheKey { set; get; }
29+
2530
/// <summary>
2631
/// Defines a mechanism for retrieving a service object.
2732
/// For instance, you can create an ILoggerFactory by using it.

src/EFCoreSecondLevelCacheInterceptor/EFCoreSecondLevelCacheInterceptor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Description>Entity Framework Core Second Level Caching Library.</Description>
4-
<VersionPrefix>4.8.8</VersionPrefix>
4+
<VersionPrefix>4.9.0</VersionPrefix>
55
<Authors>Vahid Nasiri</Authors>
66
<TargetFrameworks>net9.0;net8.0;net7.0;net6.0;net5.0;netstandard2.1;netstandard2.0;net462;netcoreapp3.1;</TargetFrameworks>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>

src/EFCoreSecondLevelCacheInterceptor/EFDebugLogger.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public EFDebugLogger(IOptions<EFCoreSecondLevelCacheSettings> cacheSettings,
4242
{
4343
var message = $"InstanceId: {Guid.NewGuid()}, Started @{DateTime.UtcNow} UTC.";
4444
logger.LogDebug(message);
45-
NotifyCacheableEvent(CacheableLogEventId.CachingSystemStarted, message, commandText: "");
45+
NotifyCacheableEvent(CacheableLogEventId.CachingSystemStarted, message, commandText: "", efCacheKey: null);
4646
}
4747
}
4848

@@ -54,7 +54,10 @@ public EFDebugLogger(IOptions<EFCoreSecondLevelCacheSettings> cacheSettings,
5454
/// <summary>
5555
/// If you set DisableLogging to false, this delegate will give you the internal caching events of the library.
5656
/// </summary>
57-
public void NotifyCacheableEvent(CacheableLogEventId eventId, string message, string commandText)
57+
public void NotifyCacheableEvent(CacheableLogEventId eventId,
58+
string message,
59+
string commandText,
60+
EFCacheKey? efCacheKey)
5861
{
5962
if (IsLoggerEnabled && _cacheableEvent is not null)
6063
{
@@ -63,6 +66,7 @@ public void NotifyCacheableEvent(CacheableLogEventId eventId, string message, st
6366
EventId = eventId,
6467
Message = message,
6568
CommandText = commandText,
69+
EFCacheKey = efCacheKey,
6670
ServiceProvider = _serviceProvider
6771
});
6872
}

src/EFCoreSecondLevelCacheInterceptor/EFEasyCachingCoreProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ public void InvalidateCacheDependencies(EFCacheKey cacheKey)
158158
$"Invalidated all of the cache entries due to early expiration of a root cache key[{rootCacheKey}].";
159159

160160
_easyCachingCoreProviderLogger.LogDebug(CacheableEventId.QueryResultInvalidated, message);
161-
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultInvalidated, message, commandText: "");
161+
162+
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultInvalidated, message, commandText: "",
163+
cacheKey);
162164
}
163165

164166
ClearAllCachedEntries();

src/EFCoreSecondLevelCacheInterceptor/IEFDebugLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public interface IEFDebugLogger
1515
/// <summary>
1616
/// If you set DisableLogging to false, this delegate will give you the internal caching events of the library.
1717
/// </summary>
18-
void NotifyCacheableEvent(CacheableLogEventId eventId, string message, string commandText);
18+
void NotifyCacheableEvent(CacheableLogEventId eventId, string message, string commandText, EFCacheKey? efCacheKey);
1919

2020
/// <summary>
2121
/// Represents some information about the current cache invalidation event

0 commit comments

Comments
 (0)