Skip to content

Commit 7a20559

Browse files
author
zzzprojects
committed
Adding Query Cache Fix && IncludeOptimized
Adding Query Cache Fix && IncludeOptimized
1 parent c8d12f7 commit 7a20559

File tree

184 files changed

+8636
-204
lines changed

Some content is hidden

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

184 files changed

+8636
-204
lines changed

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.1")]
22-
[assembly: AssemblyFileVersion("1.4.1")]
21+
[assembly: AssemblyVersion("1.4.3")]
22+
[assembly: AssemblyFileVersion("1.4.3")]

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// More projects: http://www.zzzprojects.com/
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

8+
using System;
89
using System.Collections.Concurrent;
910
using System.Collections.Generic;
1011
using System.Data.Common;
@@ -87,18 +88,39 @@ internal static void AddCacheTag(string cacheKey, params string[] tags)
8788
{
8889
foreach (var tag in tags)
8990
{
90-
CacheTags.AddOrUpdate(tag, x => new List<string> {cacheKey}, (x, list) =>
91+
CacheTags.AddOrUpdate(CachePrefix + tag, x => new List<string> {cacheKey}, (x, list) =>
9192
{
92-
if (!list.Contains(x))
93+
if (!list.Contains(cacheKey))
9394
{
94-
list.Add(x);
95+
list.Add(cacheKey);
9596
}
9697

9798
return list;
9899
});
99100
}
100101
}
101102

103+
#if !EFCORE
104+
/// <summary>Expire all cached objects && tag.</summary>
105+
public static void ExpireAll()
106+
{
107+
List<string> list = new List<string>();
108+
109+
foreach (var item in Cache)
110+
{
111+
if (item.Key.StartsWith(CachePrefix, StringComparison.InvariantCulture))
112+
{
113+
list.Add(item.Key);
114+
}
115+
}
116+
117+
foreach (var item in list)
118+
{
119+
Cache.Remove(item);
120+
}
121+
}
122+
#endif
123+
102124
/// <summary>Expire all cached keys linked to specified tags.</summary>
103125
/// <param name="tags">
104126
/// A variable-length parameters list containing tag to expire linked cache
@@ -109,7 +131,7 @@ public static void ExpireTag(params string[] tags)
109131
foreach (var tag in tags)
110132
{
111133
List<string> list;
112-
if (CacheTags.TryRemove(tag, out list))
134+
if (CacheTags.TryRemove(CachePrefix + tag, out list))
113135
{
114136
foreach (var item in list)
115137
{

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.1")]
22-
[assembly: AssemblyFileVersion("1.4.1")]
21+
[assembly: AssemblyVersion("1.4.3")]
22+
[assembly: AssemblyFileVersion("1.4.3")]

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// More projects: http://www.zzzprojects.com/
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

8+
using System;
89
using System.Collections.Concurrent;
910
using System.Collections.Generic;
1011
using System.Data.Common;
@@ -87,18 +88,39 @@ internal static void AddCacheTag(string cacheKey, params string[] tags)
8788
{
8889
foreach (var tag in tags)
8990
{
90-
CacheTags.AddOrUpdate(tag, x => new List<string> {cacheKey}, (x, list) =>
91+
CacheTags.AddOrUpdate(CachePrefix + tag, x => new List<string> {cacheKey}, (x, list) =>
9192
{
92-
if (!list.Contains(x))
93+
if (!list.Contains(cacheKey))
9394
{
94-
list.Add(x);
95+
list.Add(cacheKey);
9596
}
9697

9798
return list;
9899
});
99100
}
100101
}
101102

103+
#if !EFCORE
104+
/// <summary>Expire all cached objects && tag.</summary>
105+
public static void ExpireAll()
106+
{
107+
List<string> list = new List<string>();
108+
109+
foreach (var item in Cache)
110+
{
111+
if (item.Key.StartsWith(CachePrefix, StringComparison.InvariantCulture))
112+
{
113+
list.Add(item.Key);
114+
}
115+
}
116+
117+
foreach (var item in list)
118+
{
119+
Cache.Remove(item);
120+
}
121+
}
122+
#endif
123+
102124
/// <summary>Expire all cached keys linked to specified tags.</summary>
103125
/// <param name="tags">
104126
/// A variable-length parameters list containing tag to expire linked cache
@@ -109,7 +131,7 @@ public static void ExpireTag(params string[] tags)
109131
foreach (var tag in tags)
110132
{
111133
List<string> list;
112-
if (CacheTags.TryRemove(tag, out list))
134+
if (CacheTags.TryRemove(CachePrefix + tag, out list))
113135
{
114136
foreach (var item in list)
115137
{

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ internal class ExceptionMessage
2929
public static string QueryIncludeFilter_Include = "Oops! 'Include' method from Entity Framework is not supported, use only IncludeFilter method. For more information, contact us: [email protected]";
3030
#endif
3131
#if FULL || QUERY_INCLUDEOPTIMIZED
32+
public static string QueryIncludeOptimized_NodeReduce = "Oops! A node has not been reduced using AllowIncludeSubPath feature. Please report the LINQ query to our support team: [email protected]";
33+
public static string QueryIncludeOptimized_ByPath_MissingPath = "Oops! the following type '{0}' doesn't contains the specified property '{1}'. For more information, contact us: [email protected]";
3234
public static string QueryIncludeOptimized_ArgumentExpression = "Oops! immediate method with expression argument are not supported. For more information, contact us: [email protected]";
3335
public static string QueryIncludeOptimized_CreateQueryElement = "Oops! Select projection are not supported in EF+ Query IncludeFilter For more information, contact us: [email protected]";
3436
public static string QueryIncludeOptimized_Include = "Oops! 'Include' method from Entity Framework is not supported, use only IncludeOptimized method. 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.1")]
22-
[assembly: AssemblyFileVersion("1.4.1")]
21+
[assembly: AssemblyVersion("1.4.3")]
22+
[assembly: AssemblyFileVersion("1.4.3")]

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// More projects: http://www.zzzprojects.com/
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

8+
using System;
89
using System.Collections.Concurrent;
910
using System.Collections.Generic;
1011
using System.Data.Common;
@@ -87,18 +88,39 @@ internal static void AddCacheTag(string cacheKey, params string[] tags)
8788
{
8889
foreach (var tag in tags)
8990
{
90-
CacheTags.AddOrUpdate(tag, x => new List<string> {cacheKey}, (x, list) =>
91+
CacheTags.AddOrUpdate(CachePrefix + tag, x => new List<string> {cacheKey}, (x, list) =>
9192
{
92-
if (!list.Contains(x))
93+
if (!list.Contains(cacheKey))
9394
{
94-
list.Add(x);
95+
list.Add(cacheKey);
9596
}
9697

9798
return list;
9899
});
99100
}
100101
}
101102

103+
#if !EFCORE
104+
/// <summary>Expire all cached objects && tag.</summary>
105+
public static void ExpireAll()
106+
{
107+
List<string> list = new List<string>();
108+
109+
foreach (var item in Cache)
110+
{
111+
if (item.Key.StartsWith(CachePrefix, StringComparison.InvariantCulture))
112+
{
113+
list.Add(item.Key);
114+
}
115+
}
116+
117+
foreach (var item in list)
118+
{
119+
Cache.Remove(item);
120+
}
121+
}
122+
#endif
123+
102124
/// <summary>Expire all cached keys linked to specified tags.</summary>
103125
/// <param name="tags">
104126
/// A variable-length parameters list containing tag to expire linked cache
@@ -109,7 +131,7 @@ public static void ExpireTag(params string[] tags)
109131
foreach (var tag in tags)
110132
{
111133
List<string> list;
112-
if (CacheTags.TryRemove(tag, out list))
134+
if (CacheTags.TryRemove(CachePrefix + tag, out list))
113135
{
114136
foreach (var item in list)
115137
{

src/Z.EntityFramework.Plus.EF6.NET40/QueryIncludeOptimized/BaseQueryIncludeOptimizedChild.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
using System;
99
using System.Linq;
10+
using System.Linq.Expressions;
1011

1112
namespace Z.EntityFramework.Plus
1213
{
@@ -19,5 +20,11 @@ public virtual void CreateIncludeQuery(IQueryable rootQuery)
1920
{
2021
throw new Exception(ExceptionMessage.GeneralException);
2122
}
23+
24+
/// <summary>Gets or sets a value indicating whether this object is lazy.</summary>
25+
/// <value>true if this object is lazy, false if not.</value>
26+
public bool IsLazy { get; set; }
27+
28+
public abstract Expression GetFilter();
2229
}
2330
}

src/Z.EntityFramework.Plus.EF6.NET40/QueryIncludeOptimized/Extensions/IQueryable`.IncludeOptimized.cs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,35 @@
1212

1313
namespace Z.EntityFramework.Plus
1414
{
15-
public static class QueryIncludeOptimizedExtensions
15+
public static partial class QueryIncludeOptimizedExtensions
1616
{
17+
private static IQueryable<T> IncludeOptimizedSingleLazy<T, TChild>(this IQueryable<T> query, Expression<Func<T, TChild>> queryIncludeFilter) where T : class where TChild : class
18+
{
19+
// GET query root
20+
var includeOrderedQueryable = query as QueryIncludeOptimizedParentQueryable<T> ?? new QueryIncludeOptimizedParentQueryable<T>(query);
21+
22+
// ADD sub query
23+
includeOrderedQueryable.Childs.Add(new QueryIncludeOptimizedChild<T, TChild>(queryIncludeFilter, true));
24+
25+
// RETURN root
26+
return includeOrderedQueryable;
27+
}
28+
29+
private static IQueryable<T> IncludeOptimizedSingle<T, TChild>(this IQueryable<T> query, Expression<Func<T, TChild>> queryIncludeFilter) where T : class where TChild : class
30+
{
31+
// INCLUDE sub path
32+
query = QueryIncludeOptimizedIncludeSubPath.IncludeSubPath(query, queryIncludeFilter);
33+
34+
// GET query root
35+
var includeOrderedQueryable = query as QueryIncludeOptimizedParentQueryable<T> ?? new QueryIncludeOptimizedParentQueryable<T>(query);
36+
37+
// ADD sub query
38+
includeOrderedQueryable.Childs.Add(new QueryIncludeOptimizedChild<T, TChild>(queryIncludeFilter));
39+
40+
// RETURN root
41+
return includeOrderedQueryable;
42+
}
43+
1744
/// <summary>
1845
/// An IQueryable&lt;T&gt; extension method that include and filter related entities with a optimized SQL.
1946
/// </summary>
@@ -24,14 +51,7 @@ public static class QueryIncludeOptimizedExtensions
2451
/// <returns>An IQueryable&lt;T&gt; that include and filter related entities.</returns>
2552
public static IQueryable<T> IncludeOptimized<T, TChild>(this IQueryable<T> query, Expression<Func<T, IEnumerable<TChild>>> queryIncludeFilter) where T : class where TChild : class
2653
{
27-
// GET query root
28-
var includeOrderedQueryable = query as QueryIncludeOptimizedParentQueryable<T> ?? new QueryIncludeOptimizedParentQueryable<T>(query);
29-
30-
// ADD sub query
31-
includeOrderedQueryable.Childs.Add(new QueryIncludeOptimizedChild<T, TChild>(queryIncludeFilter));
32-
33-
// RETURN root
34-
return includeOrderedQueryable;
54+
return query.IncludeOptimizedSingle(queryIncludeFilter);
3555
}
3656

3757
/// <summary>
@@ -45,14 +65,7 @@ public static IQueryable<T> IncludeOptimized<T, TChild>(this IQueryable<T> query
4565
/// <returns>An IQueryable&lt;T&gt; that include and filter related entities.</returns>
4666
public static IQueryable<T> IncludeOptimized<T, TChild>(this IQueryable<T> query, Expression<Func<T, TChild>> queryIncludeFilter) where T : class where TChild : class
4767
{
48-
// GET query root
49-
var includeOrderedQueryable = query as QueryIncludeOptimizedParentQueryable<T> ?? new QueryIncludeOptimizedParentQueryable<T>(query);
50-
51-
// ADD sub query
52-
includeOrderedQueryable.Childs.Add(new QueryIncludeOptimizedChild<T, TChild>(queryIncludeFilter));
53-
54-
// RETURN root
55-
return includeOrderedQueryable;
68+
return query.IncludeOptimizedSingle(queryIncludeFilter);
5669
}
5770
}
5871
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit)
2+
// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus
3+
// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues
4+
// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
7+
8+
using System.Linq;
9+
10+
namespace Z.EntityFramework.Plus
11+
{
12+
public static partial class QueryIncludeOptimizedExtensions
13+
{
14+
/// <summary>
15+
/// An IQueryable&lt;T&gt; extension method that include and filter related entities with a optimized SQL.
16+
/// </summary>
17+
/// <typeparam name="T">The type of elements of the query.</typeparam>
18+
/// <param name="query">The query to filter included related entities.</param>
19+
/// <param name="navigationProperties">The navigation properties to include.</param>
20+
/// <returns>An IQueryable&lt;T&gt; that include and filter related entities.</returns>
21+
public static IQueryable<T> IncludeOptimizedByPath<T>(this IQueryable<T> query, string navigationProperties)
22+
{
23+
return QueryIncludeOptimizedByPath.IncludeOptimizedByPath(query, navigationProperties);
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)