Skip to content

Commit 7bce601

Browse files
author
zzzprojects
committed
Add new QueryCache Key generation
Add new QueryCache Key generation
1 parent 0341656 commit 7bce601

File tree

101 files changed

+1038
-41
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+1038
-41
lines changed

src/Z.EntityFramework.Plus.EF5.NET40/ExceptionMessage.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ internal class ExceptionMessage
2323
public static string BatchOperations_PropertyNotFound = "Oops! Mapping for the property '{0}' cannot be found, the current version don't support yet Complex Type, Enum, TPC, TPH and TPT. For more information, contact us: [email protected]";
2424
public static string BatchOperations_AssemblyNotFound = "Oops! The assembly 'Microsoft.EntityFrameworkCore.SqlServer' could not be found. This feature is only supported for SQL Server for .NET Core. For more information, contact us: [email protected]";
2525
#endif
26+
#if FULL || QUERY_CACHE
27+
public static string QueryCache_FirstTagNullOrEmpty = "Oops! The option 'ForceFirstTagAsCacheKey' is enabled but we found no tag, an empty string, or a null string instead. Make sure a tag is provided, and it's not null or empty. For more information, contact us: [email protected]";
28+
#endif
2629
#if FULL || QUERY_INCLUDEFILTER
2730
public static string QueryIncludeFilter_ArgumentExpression = "Oops! immediate method with expression argument are not supported in EF+ Query IncludeFilter. For more information, contact us: [email protected]";
2831
public static string QueryIncludeFilter_CreateQueryElement = "Oops! Select projection are not supported in EF+ Query IncludeFilter. For more information, contact us: [email protected]";

src/Z.EntityFramework.Plus.EF5.NET40/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
[assembly: AssemblyCulture("")]
1919
[assembly: ComVisible(false)]
2020
[assembly: Guid("e4c2af73-caeb-4429-bcb6-0a359484e064")]
21-
[assembly: AssemblyVersion("1.4.4")]
22-
[assembly: AssemblyFileVersion("1.4.4")]
21+
[assembly: AssemblyVersion("1.4.5")]
22+
[assembly: AssemblyFileVersion("1.4.5")]

src/Z.EntityFramework.Plus.EF5.NET40/QueryCache/QueryCacheManager.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ public static Func<MemoryCacheEntryOptions> MemoryCacheEntryOptionsFactory
139139
/// <value>The cache prefix to use to create the cache key.</value>
140140
public static string CachePrefix { get; set; }
141141

142+
/// <summary>Gets or sets the cache key factory.</summary>
143+
/// <value>The cache key factory.</value>
144+
public static Func<IQueryable, string[], string> CacheKeyFactory { get; set; }
145+
142146
/// <summary>
143147
/// Gets or sets a value indicating whether the connection in cache key should be included.
144148
/// </summary>
@@ -149,6 +153,12 @@ public static Func<MemoryCacheEntryOptions> MemoryCacheEntryOptionsFactory
149153
/// <value>The cache tags used to store tags and corresponding cached keys.</value>
150154
public static ConcurrentDictionary<string, List<string>> CacheTags { get; }
151155

156+
/// <summary>
157+
/// Gets or sets a value indicating whether the first tag as cache key should be forced.
158+
/// </summary>
159+
/// <value>true if force first tag as cache key, false if not.</value>
160+
public static bool ForceFirstTagAsCacheKey { get; set; }
161+
152162
/// <summary>Adds cache tags corresponding to a cached key in the CacheTags dictionary.</summary>
153163
/// <param name="cacheKey">The cache key.</param>
154164
/// <param name="tags">A variable-length parameters list containing tags corresponding to the <paramref name="cacheKey" />.</param>
@@ -215,6 +225,26 @@ public static void ExpireTag(params string[] tags)
215225
/// <returns>The cache key used to cache or retrieve a query from the QueryCacheManager.</returns>
216226
public static string GetCacheKey(IQueryable query, string[] tags)
217227
{
228+
if (CacheKeyFactory != null)
229+
{
230+
var cacheKey = CacheKeyFactory(query, tags);
231+
232+
if (!string.IsNullOrEmpty(cacheKey))
233+
{
234+
return cacheKey;
235+
}
236+
}
237+
238+
if (ForceFirstTagAsCacheKey)
239+
{
240+
if (tags.Length == 0 || string.IsNullOrEmpty(tags[0]))
241+
{
242+
throw new Exception(ExceptionMessage.QueryCache_FirstTagNullOrEmpty);
243+
}
244+
245+
return tags[0];
246+
}
247+
218248
var sb = new StringBuilder();
219249

220250
#if EF5 || EF6

src/Z.EntityFramework.Plus.EF5/ExceptionMessage.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ internal class ExceptionMessage
2323
public static string BatchOperations_PropertyNotFound = "Oops! Mapping for the property '{0}' cannot be found, the current version don't support yet Complex Type, Enum, TPC, TPH and TPT. For more information, contact us: [email protected]";
2424
public static string BatchOperations_AssemblyNotFound = "Oops! The assembly 'Microsoft.EntityFrameworkCore.SqlServer' could not be found. This feature is only supported for SQL Server for .NET Core. For more information, contact us: [email protected]";
2525
#endif
26+
#if FULL || QUERY_CACHE
27+
public static string QueryCache_FirstTagNullOrEmpty = "Oops! The option 'ForceFirstTagAsCacheKey' is enabled but we found no tag, an empty string, or a null string instead. Make sure a tag is provided, and it's not null or empty. For more information, contact us: [email protected]";
28+
#endif
2629
#if FULL || QUERY_INCLUDEFILTER
2730
public static string QueryIncludeFilter_ArgumentExpression = "Oops! immediate method with expression argument are not supported in EF+ Query IncludeFilter. For more information, contact us: [email protected]";
2831
public static string QueryIncludeFilter_CreateQueryElement = "Oops! Select projection are not supported in EF+ Query IncludeFilter. For more information, contact us: [email protected]";

src/Z.EntityFramework.Plus.EF5/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
[assembly: AssemblyCulture("")]
1919
[assembly: ComVisible(false)]
2020
[assembly: Guid("abcbb878-043c-4957-a334-90e9872e684e")]
21-
[assembly: AssemblyVersion("1.4.4")]
22-
[assembly: AssemblyFileVersion("1.4.4")]
21+
[assembly: AssemblyVersion("1.4.5")]
22+
[assembly: AssemblyFileVersion("1.4.5")]

src/Z.EntityFramework.Plus.EF5/QueryCache/QueryCacheManager.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ public static Func<MemoryCacheEntryOptions> MemoryCacheEntryOptionsFactory
139139
/// <value>The cache prefix to use to create the cache key.</value>
140140
public static string CachePrefix { get; set; }
141141

142+
/// <summary>Gets or sets the cache key factory.</summary>
143+
/// <value>The cache key factory.</value>
144+
public static Func<IQueryable, string[], string> CacheKeyFactory { get; set; }
145+
142146
/// <summary>
143147
/// Gets or sets a value indicating whether the connection in cache key should be included.
144148
/// </summary>
@@ -149,6 +153,12 @@ public static Func<MemoryCacheEntryOptions> MemoryCacheEntryOptionsFactory
149153
/// <value>The cache tags used to store tags and corresponding cached keys.</value>
150154
public static ConcurrentDictionary<string, List<string>> CacheTags { get; }
151155

156+
/// <summary>
157+
/// Gets or sets a value indicating whether the first tag as cache key should be forced.
158+
/// </summary>
159+
/// <value>true if force first tag as cache key, false if not.</value>
160+
public static bool ForceFirstTagAsCacheKey { get; set; }
161+
152162
/// <summary>Adds cache tags corresponding to a cached key in the CacheTags dictionary.</summary>
153163
/// <param name="cacheKey">The cache key.</param>
154164
/// <param name="tags">A variable-length parameters list containing tags corresponding to the <paramref name="cacheKey" />.</param>
@@ -215,6 +225,26 @@ public static void ExpireTag(params string[] tags)
215225
/// <returns>The cache key used to cache or retrieve a query from the QueryCacheManager.</returns>
216226
public static string GetCacheKey(IQueryable query, string[] tags)
217227
{
228+
if (CacheKeyFactory != null)
229+
{
230+
var cacheKey = CacheKeyFactory(query, tags);
231+
232+
if (!string.IsNullOrEmpty(cacheKey))
233+
{
234+
return cacheKey;
235+
}
236+
}
237+
238+
if (ForceFirstTagAsCacheKey)
239+
{
240+
if (tags.Length == 0 || string.IsNullOrEmpty(tags[0]))
241+
{
242+
throw new Exception(ExceptionMessage.QueryCache_FirstTagNullOrEmpty);
243+
}
244+
245+
return tags[0];
246+
}
247+
218248
var sb = new StringBuilder();
219249

220250
#if EF5 || EF6

src/Z.EntityFramework.Plus.EF6.NET40/ExceptionMessage.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ internal class ExceptionMessage
2323
public static string BatchOperations_PropertyNotFound = "Oops! Mapping for the property '{0}' cannot be found, the current version don't support yet Complex Type, Enum, TPC, TPH and TPT. For more information, contact us: [email protected]";
2424
public static string BatchOperations_AssemblyNotFound = "Oops! The assembly 'Microsoft.EntityFrameworkCore.SqlServer' could not be found. This feature is only supported for SQL Server for .NET Core. For more information, contact us: [email protected]";
2525
#endif
26+
#if FULL || QUERY_CACHE
27+
public static string QueryCache_FirstTagNullOrEmpty = "Oops! The option 'ForceFirstTagAsCacheKey' is enabled but we found no tag, an empty string, or a null string instead. Make sure a tag is provided, and it's not null or empty. For more information, contact us: [email protected]";
28+
#endif
2629
#if FULL || QUERY_INCLUDEFILTER
2730
public static string QueryIncludeFilter_ArgumentExpression = "Oops! immediate method with expression argument are not supported in EF+ Query IncludeFilter. For more information, contact us: [email protected]";
2831
public static string QueryIncludeFilter_CreateQueryElement = "Oops! Select projection are not supported in EF+ Query IncludeFilter. For more information, contact us: [email protected]";

src/Z.EntityFramework.Plus.EF6.NET40/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
[assembly: AssemblyCulture("")]
1919
[assembly: ComVisible(false)]
2020
[assembly: Guid("ac398eb8-0a31-4d06-a804-84d10b6da96d")]
21-
[assembly: AssemblyVersion("1.4.4")]
22-
[assembly: AssemblyFileVersion("1.4.4")]
21+
[assembly: AssemblyVersion("1.4.5")]
22+
[assembly: AssemblyFileVersion("1.4.5")]

src/Z.EntityFramework.Plus.EF6.NET40/QueryCache/QueryCacheManager.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ public static Func<MemoryCacheEntryOptions> MemoryCacheEntryOptionsFactory
139139
/// <value>The cache prefix to use to create the cache key.</value>
140140
public static string CachePrefix { get; set; }
141141

142+
/// <summary>Gets or sets the cache key factory.</summary>
143+
/// <value>The cache key factory.</value>
144+
public static Func<IQueryable, string[], string> CacheKeyFactory { get; set; }
145+
142146
/// <summary>
143147
/// Gets or sets a value indicating whether the connection in cache key should be included.
144148
/// </summary>
@@ -149,6 +153,12 @@ public static Func<MemoryCacheEntryOptions> MemoryCacheEntryOptionsFactory
149153
/// <value>The cache tags used to store tags and corresponding cached keys.</value>
150154
public static ConcurrentDictionary<string, List<string>> CacheTags { get; }
151155

156+
/// <summary>
157+
/// Gets or sets a value indicating whether the first tag as cache key should be forced.
158+
/// </summary>
159+
/// <value>true if force first tag as cache key, false if not.</value>
160+
public static bool ForceFirstTagAsCacheKey { get; set; }
161+
152162
/// <summary>Adds cache tags corresponding to a cached key in the CacheTags dictionary.</summary>
153163
/// <param name="cacheKey">The cache key.</param>
154164
/// <param name="tags">A variable-length parameters list containing tags corresponding to the <paramref name="cacheKey" />.</param>
@@ -215,6 +225,26 @@ public static void ExpireTag(params string[] tags)
215225
/// <returns>The cache key used to cache or retrieve a query from the QueryCacheManager.</returns>
216226
public static string GetCacheKey(IQueryable query, string[] tags)
217227
{
228+
if (CacheKeyFactory != null)
229+
{
230+
var cacheKey = CacheKeyFactory(query, tags);
231+
232+
if (!string.IsNullOrEmpty(cacheKey))
233+
{
234+
return cacheKey;
235+
}
236+
}
237+
238+
if (ForceFirstTagAsCacheKey)
239+
{
240+
if (tags.Length == 0 || string.IsNullOrEmpty(tags[0]))
241+
{
242+
throw new Exception(ExceptionMessage.QueryCache_FirstTagNullOrEmpty);
243+
}
244+
245+
return tags[0];
246+
}
247+
218248
var sb = new StringBuilder();
219249

220250
#if EF5 || EF6

src/Z.EntityFramework.Plus.EF6/ExceptionMessage.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ internal class ExceptionMessage
2323
public static string BatchOperations_PropertyNotFound = "Oops! Mapping for the property '{0}' cannot be found, the current version don't support yet Complex Type, Enum, TPC, TPH and TPT. For more information, contact us: [email protected]";
2424
public static string BatchOperations_AssemblyNotFound = "Oops! The assembly 'Microsoft.EntityFrameworkCore.SqlServer' could not be found. This feature is only supported for SQL Server for .NET Core. For more information, contact us: [email protected]";
2525
#endif
26+
#if FULL || QUERY_CACHE
27+
public static string QueryCache_FirstTagNullOrEmpty = "Oops! The option 'ForceFirstTagAsCacheKey' is enabled but we found no tag, an empty string, or a null string instead. Make sure a tag is provided, and it's not null or empty. For more information, contact us: [email protected]";
28+
#endif
2629
#if FULL || QUERY_INCLUDEFILTER
2730
public static string QueryIncludeFilter_ArgumentExpression = "Oops! immediate method with expression argument are not supported in EF+ Query IncludeFilter. For more information, contact us: [email protected]";
2831
public static string QueryIncludeFilter_CreateQueryElement = "Oops! Select projection are not supported in EF+ Query IncludeFilter. For more information, contact us: [email protected]";

0 commit comments

Comments
 (0)