Skip to content

Commit 7509dbf

Browse files
author
zzzprojects
committed
Add UseTagsAsCacheKey && Rename FirstTag
Add UseTagsAsCacheKey && Rename FirstTag
1 parent 7bce601 commit 7509dbf

File tree

46 files changed

+1388
-544
lines changed

Some content is hidden

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

46 files changed

+1388
-544
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ internal class ExceptionMessage
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
2626
#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]";
27+
public static string QueryCache_FirstTagNullOrEmpty = "Oops! The option 'UseFirstTagAsCacheKey' is enabled but we found no tag, an empty tag string, or a null tag string instead. Make sure a tag is provided, and it's not null or empty. For more information, contact us: [email protected]";
28+
public static string QueryCache_UseTagsNullOrEmpty = "Oops! The option 'UseTagsAsCacheKey' is enabled but we found no tag, an empty tag string, or a null tag string instead. Make sure a tag is provided, and it's not null or empty. For more information, contact us: [email protected]";
2829
#endif
2930
#if FULL || QUERY_INCLUDEFILTER
3031
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]";

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.5")]
22-
[assembly: AssemblyFileVersion("1.4.5")]
21+
[assembly: AssemblyVersion("1.4.6")]
22+
[assembly: AssemblyFileVersion("1.4.6")]

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

Lines changed: 73 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,16 @@ public static Func<MemoryCacheEntryOptions> MemoryCacheEntryOptionsFactory
154154
public static ConcurrentDictionary<string, List<string>> CacheTags { get; }
155155

156156
/// <summary>
157-
/// Gets or sets a value indicating whether the first tag as cache key should be forced.
157+
/// Gets or sets a value indicating whether this object use first tag as cache key.
158158
/// </summary>
159-
/// <value>true if force first tag as cache key, false if not.</value>
160-
public static bool ForceFirstTagAsCacheKey { get; set; }
159+
/// <value>true if use first tag as cache key, false if not.</value>
160+
public static bool UseFirstTagAsCacheKey { get; set; }
161+
162+
/// <summary>
163+
/// Gets or sets a value indicating whether this object use tag as cache key.
164+
/// </summary>
165+
/// <value>true if use tag as cache key, false if not.</value>
166+
public static bool UseTagsAsCacheKey { get; set; }
161167

162168
/// <summary>Adds cache tags corresponding to a cached key in the CacheTags dictionary.</summary>
163169
/// <param name="cacheKey">The cache key.</param>
@@ -235,16 +241,6 @@ public static string GetCacheKey(IQueryable query, string[] tags)
235241
}
236242
}
237243

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-
248244
var sb = new StringBuilder();
249245

250246
#if EF5 || EF6
@@ -253,7 +249,7 @@ public static string GetCacheKey(IQueryable query, string[] tags)
253249

254250
if (queryCacheUniqueKeyMethod != null)
255251
{
256-
var queryCacheUniqueKey = (string) queryCacheUniqueKeyMethod.Invoke(query, null);
252+
var queryCacheUniqueKey = (string)queryCacheUniqueKeyMethod.Invoke(query, null);
257253

258254
if (!string.IsNullOrEmpty(queryCacheUniqueKey))
259255
{
@@ -267,37 +263,7 @@ public static string GetCacheKey(IQueryable query, string[] tags)
267263

268264
if (IncludeConnectionInCacheKey)
269265
{
270-
var connection = ((EntityConnection) objectQuery.Context.Connection).GetStoreConnection();
271-
272-
// FORCE database name in case "ChangeDatabase()" method is used
273-
sb.AppendLine(connection.DataSource ?? "");
274-
sb.AppendLine(connection.Database ?? "");
275-
sb.AppendLine(connection.ConnectionString);
276-
}
277-
278-
sb.AppendLine(string.Join(";", tags));
279-
#endif
280-
281-
#if EF5
282-
sb.AppendLine(objectQuery.ToTraceString());
283-
284-
foreach (var parameter in objectQuery.Parameters)
285-
{
286-
sb.Append(parameter.Name);
287-
sb.Append(";");
288-
sb.Append(parameter.Value);
289-
sb.AppendLine(";");
290-
}
291-
#elif EF6
292-
var commandTextAndParameters = objectQuery.GetCommandTextAndParameters();
293-
sb.AppendLine(commandTextAndParameters.Item1);
294-
295-
foreach (DbParameter parameter in commandTextAndParameters.Item2)
296-
{
297-
sb.Append(parameter.ParameterName);
298-
sb.Append(";");
299-
sb.Append(parameter.Value);
300-
sb.AppendLine(";");
266+
sb.AppendLine(GetConnectionStringForCacheKey(query));
301267
}
302268
#elif EFCORE
303269
RelationalQueryContext queryContext;
@@ -307,56 +273,33 @@ public static string GetCacheKey(IQueryable query, string[] tags)
307273

308274
if (IncludeConnectionInCacheKey)
309275
{
310-
var connection = queryContext.Connection.DbConnection;
311-
312-
// FORCE database name in case "ChangeDatabase()" method is used
313-
sb.AppendLine(connection.DataSource ?? "");
314-
sb.AppendLine(connection.Database ?? "");
315-
sb.AppendLine(connection.ConnectionString);
316-
}
317-
318-
sb.AppendLine(string.Join(";", tags));
319-
sb.AppendLine(query.Expression.ToString());
320-
sb.AppendLine(command.CommandText);
321-
322-
foreach (var parameter in queryContext.ParameterValues)
323-
{
324-
sb.Append(parameter.Key);
325-
sb.Append(";");
326-
sb.Append(parameter.Value);
327-
sb.AppendLine(";");
276+
sb.AppendLine(GetConnectionStringForCacheKey(queryContext));
328277
}
329278
#endif
330279

331-
return sb.ToString();
332-
}
333-
334-
/// <summary>Gets cached keys used to cache or retrieve a query from the QueryCacheManager.</summary>
335-
/// <typeparam name="T">Generic type parameter.</typeparam>
336-
/// <param name="query">The query to cache or retrieve from the QueryCacheManager.</param>
337-
/// <param name="tags">A variable-length parameters list containing tags to create the cache key.</param>
338-
/// <returns>The cache key used to cache or retrieve a query from the QueryCacheManager.</returns>
339-
public static string GetCacheKey<T>(QueryDeferred<T> query, string[] tags)
340-
{
341-
var sb = new StringBuilder();
342-
343-
#if EF5 || EF6
344-
var objectQuery = query.Query.GetObjectQuery();
280+
if (UseFirstTagAsCacheKey)
281+
{
282+
if (tags == null || tags.Length == 0 || string.IsNullOrEmpty(tags[0]))
283+
{
284+
throw new Exception(ExceptionMessage.QueryCache_FirstTagNullOrEmpty);
285+
}
345286

346-
sb.AppendLine(CachePrefix);
287+
sb.AppendLine(tags[0]);
288+
return sb.ToString();
289+
}
347290

348-
if (IncludeConnectionInCacheKey)
291+
if (UseTagsAsCacheKey)
349292
{
350-
var connection = ((EntityConnection) objectQuery.Context.Connection).GetStoreConnection();
293+
if (tags == null || tags.Length == 0 || tags.Any(string.IsNullOrEmpty))
294+
{
295+
throw new Exception(ExceptionMessage.QueryCache_UseTagsNullOrEmpty);
296+
}
351297

352-
// FORCE database name in case "ChangeDatabase()" method is used
353-
sb.AppendLine(connection.DataSource ?? "");
354-
sb.AppendLine(connection.Database ?? "");
355-
sb.AppendLine(connection.ConnectionString);
298+
sb.AppendLine(string.Join(";", tags));
299+
return sb.ToString();
356300
}
357301

358302
sb.AppendLine(string.Join(";", tags));
359-
#endif
360303

361304
#if EF5
362305
sb.AppendLine(objectQuery.ToTraceString());
@@ -380,22 +323,6 @@ public static string GetCacheKey<T>(QueryDeferred<T> query, string[] tags)
380323
sb.AppendLine(";");
381324
}
382325
#elif EFCORE
383-
RelationalQueryContext queryContext;
384-
var command = query.Query.CreateCommand(out queryContext);
385-
386-
sb.AppendLine(CachePrefix);
387-
388-
if (IncludeConnectionInCacheKey)
389-
{
390-
var connection = queryContext.Connection.DbConnection;
391-
392-
// FORCE database name in case "ChangeDatabase()" method is used
393-
sb.AppendLine(connection.DataSource ?? "");
394-
sb.AppendLine(connection.Database ?? "");
395-
sb.AppendLine(connection.ConnectionString);
396-
}
397-
398-
sb.AppendLine(string.Join(";", tags));
399326
sb.AppendLine(query.Expression.ToString());
400327
sb.AppendLine(command.CommandText);
401328

@@ -410,5 +337,50 @@ public static string GetCacheKey<T>(QueryDeferred<T> query, string[] tags)
410337

411338
return sb.ToString();
412339
}
340+
341+
public static string GetConnectionStringForCacheKey(IQueryable query)
342+
{
343+
#if EF5 || EF6
344+
var connection = ((EntityConnection) query.GetObjectQuery().Context.Connection).GetStoreConnection();
345+
346+
// FORCE database name in case "ChangeDatabase()" method is used
347+
var connectionString = string.Concat(connection.DataSource ?? "",
348+
Environment.NewLine,
349+
connection.Database ?? "",
350+
Environment.NewLine,
351+
connection.ConnectionString ?? "");
352+
return connectionString;
353+
#elif EFCORE
354+
RelationalQueryContext queryContext;
355+
var command = query.CreateCommand(out queryContext);
356+
return GetConnectionStringForCacheKey(queryContext);
357+
#endif
358+
}
359+
360+
#if EFCORE
361+
public static string GetConnectionStringForCacheKey(RelationalQueryContext queryContext)
362+
{
363+
var connection = queryContext.Connection.DbConnection;
364+
365+
// FORCE database name in case "ChangeDatabase()" method is used
366+
var connectionString = string.Concat(connection.DataSource ?? "",
367+
Environment.NewLine,
368+
connection.Database ?? "",
369+
Environment.NewLine,
370+
connection.ConnectionString ?? "");
371+
return connectionString;
372+
}
373+
#endif
374+
375+
376+
/// <summary>Gets cached keys used to cache or retrieve a query from the QueryCacheManager.</summary>
377+
/// <typeparam name="T">Generic type parameter.</typeparam>
378+
/// <param name="query">The query to cache or retrieve from the QueryCacheManager.</param>
379+
/// <param name="tags">A variable-length parameters list containing tags to create the cache key.</param>
380+
/// <returns>The cache key used to cache or retrieve a query from the QueryCacheManager.</returns>
381+
public static string GetCacheKey<T>(QueryDeferred<T> query, string[] tags)
382+
{
383+
return GetCacheKey(query.Query, tags);
384+
}
413385
}
414386
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ internal class ExceptionMessage
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
2626
#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]";
27+
public static string QueryCache_FirstTagNullOrEmpty = "Oops! The option 'UseFirstTagAsCacheKey' is enabled but we found no tag, an empty tag string, or a null tag string instead. Make sure a tag is provided, and it's not null or empty. For more information, contact us: [email protected]";
28+
public static string QueryCache_UseTagsNullOrEmpty = "Oops! The option 'UseTagsAsCacheKey' is enabled but we found no tag, an empty tag string, or a null tag string instead. Make sure a tag is provided, and it's not null or empty. For more information, contact us: [email protected]";
2829
#endif
2930
#if FULL || QUERY_INCLUDEFILTER
3031
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]";

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.5")]
22-
[assembly: AssemblyFileVersion("1.4.5")]
21+
[assembly: AssemblyVersion("1.4.6")]
22+
[assembly: AssemblyFileVersion("1.4.6")]

0 commit comments

Comments
 (0)