Skip to content

Commit 7ab3736

Browse files
committed
Allow overriding the default cache policy, Close #286
1 parent 5a05b2d commit 7ab3736

File tree

13 files changed

+235
-56
lines changed

13 files changed

+235
-56
lines changed

README.md

Lines changed: 132 additions & 48 deletions
Large diffs are not rendered by default.

src/EFCoreSecondLevelCacheInterceptor.CacheManager.Core/EFCoreSecondLevelCacheInterceptor.CacheManager.Core.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>5.1.1</VersionPrefix>
4+
<VersionPrefix>5.2.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.EasyCaching.Core/EFCoreSecondLevelCacheInterceptor.EasyCaching.Core.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>5.1.1</VersionPrefix>
4+
<VersionPrefix>5.2.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.FusionCache/EFCoreSecondLevelCacheInterceptor.FusionCache.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>5.1.1</VersionPrefix>
4+
<VersionPrefix>5.2.0</VersionPrefix>
55
<Authors>Vahid Nasiri</Authors>
66
<TargetFrameworks>net9.0;net8.0;</TargetFrameworks>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>

src/EFCoreSecondLevelCacheInterceptor.HybridCache/EFCoreSecondLevelCacheInterceptor.HybridCache.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>5.1.1</VersionPrefix>
4+
<VersionPrefix>5.2.0</VersionPrefix>
55
<Authors>Vahid Nasiri</Authors>
66
<TargetFrameworks>net9.0;net8.0;</TargetFrameworks>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>

src/EFCoreSecondLevelCacheInterceptor.MemoryCache/EFCoreSecondLevelCacheInterceptor.MemoryCache.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>5.1.1</VersionPrefix>
4+
<VersionPrefix>5.2.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.StackExchange.Redis/EFCoreSecondLevelCacheInterceptor.StackExchange.Redis.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>5.1.1</VersionPrefix>
4+
<VersionPrefix>5.2.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>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace EFCoreSecondLevelCacheInterceptor;
5+
6+
/// <summary>
7+
/// Provides information about the currently executing query
8+
/// </summary>
9+
public class EFCachePolicyContext
10+
{
11+
/// <summary>
12+
/// The currently executing query
13+
/// </summary>
14+
public string CommandText { set; get; } = null!;
15+
16+
/// <summary>
17+
/// The associated currently executing query's table names.
18+
/// </summary>
19+
public ISet<string> CommandTableNames { set; get; } = new SortedSet<string>(StringComparer.OrdinalIgnoreCase);
20+
21+
/// <summary>
22+
/// The associated currently executing query's entity types.
23+
/// </summary>
24+
public IList<Type> CommandEntityTypes { set; get; } = [];
25+
26+
/// <summary>
27+
/// Is `insert`, `update` or `delete`?
28+
/// </summary>
29+
public bool IsCrudCommand { set; get; }
30+
31+
/// <summary>
32+
/// The currently executing query's EFCachePolicy value, before being possibly altered by calling
33+
/// `OverrideCachePolicy()` method.
34+
/// </summary>
35+
public EFCachePolicy? CommandCachePolicy { set; get; }
36+
}

src/EFCoreSecondLevelCacheInterceptor/EFCachePolicyParser.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ public string RemoveEFCachePolicyTag(string commandText)
127127
var efCachePolicy = GetParsedPolicy(commandText) ?? GetSpecificGlobalPolicy(commandText, allEntityTypes) ??
128128
GetSkippedGlobalPolicy(commandText, allEntityTypes) ?? GetGlobalPolicy(commandText);
129129

130+
efCachePolicy = TryOverrideCachePolicy(efCachePolicy, commandText, allEntityTypes);
131+
130132
if (efCachePolicy != null && _logger.IsLoggerEnabled)
131133
{
132134
var message = $"Using EFCachePolicy: {efCachePolicy}.";
@@ -139,6 +141,32 @@ public string RemoveEFCachePolicyTag(string commandText)
139141
return efCachePolicy;
140142
}
141143

144+
private EFCachePolicy? TryOverrideCachePolicy(EFCachePolicy? efCachePolicy,
145+
string commandText,
146+
IList<TableEntityInfo> allEntityTypes)
147+
{
148+
if (_cacheSettings.OverrideCachePolicy is null)
149+
{
150+
return efCachePolicy;
151+
}
152+
153+
var newCachePolicy = _cacheSettings.OverrideCachePolicy(new EFCachePolicyContext
154+
{
155+
CommandText = commandText,
156+
CommandTableNames = _sqlCommandsProcessor.GetSqlCommandTableNames(commandText),
157+
CommandEntityTypes = _sqlCommandsProcessor.GetSqlCommandEntityTypes(commandText, allEntityTypes),
158+
IsCrudCommand = _sqlCommandsProcessor.IsCrudCommand(commandText),
159+
CommandCachePolicy = efCachePolicy
160+
});
161+
162+
if (newCachePolicy is not null)
163+
{
164+
efCachePolicy = newCachePolicy;
165+
}
166+
167+
return efCachePolicy;
168+
}
169+
142170
private bool ShouldSkipCachingCommands(string commandText)
143171
{
144172
var result = _cacheSettings.SkipCachingCommands != null && _cacheSettings.SkipCachingCommands(commandText);

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>5.1.1</VersionPrefix>
4+
<VersionPrefix>5.2.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>

0 commit comments

Comments
 (0)