Skip to content

Commit 6bf5f92

Browse files
author
zzzprojects
committed
Fixing QueryCache
Fixing QueryCache
1 parent 6795c71 commit 6bf5f92

File tree

43 files changed

+1086
-54
lines changed

Some content is hidden

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

43 files changed

+1086
-54
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.0")]
22-
[assembly: AssemblyFileVersion("1.4.0")]
21+
[assembly: AssemblyVersion("1.4.1")]
22+
[assembly: AssemblyFileVersion("1.4.1")]

src/Z.EntityFramework.Plus.EF5.NET40/QueryCache/Extensions/QueryDeferred`/FromCache.cs

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

8-
#if EF5 || EF6
98
using System;
9+
10+
#if EF5 || EF6
1011
using System.Runtime.Caching;
1112

1213
#elif EFCORE
@@ -40,9 +41,17 @@ public static T FromCache<T>(this QueryDeferred<T> query, CacheItemPolicy policy
4041
if (item == null)
4142
{
4243
item = query.Execute();
43-
item = QueryCacheManager.Cache.AddOrGetExisting(key, item, policy) ?? item;
44+
45+
item = QueryCacheManager.Cache.AddOrGetExisting(key, item ?? DBNull.Value, policy) ?? item;
4446
QueryCacheManager.AddCacheTag(key, tags);
4547
}
48+
else
49+
{
50+
if (item == DBNull.Value)
51+
{
52+
item = null;
53+
}
54+
}
4655

4756
return (T) item;
4857
}
@@ -68,9 +77,17 @@ public static T FromCache<T>(this QueryDeferred<T> query, DateTimeOffset absolut
6877
if (item == null)
6978
{
7079
item = query.Execute();
71-
item = QueryCacheManager.Cache.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
80+
81+
item = QueryCacheManager.Cache.AddOrGetExisting(key, item ?? DBNull.Value, absoluteExpiration) ?? item;
7282
QueryCacheManager.AddCacheTag(key, tags);
7383
}
84+
else
85+
{
86+
if (item == DBNull.Value)
87+
{
88+
item = null;
89+
}
90+
}
7491

7592
return (T) item;
7693
}
@@ -111,9 +128,17 @@ public static T FromCache<T>(this QueryDeferred<T> query, MemoryCacheEntryOption
111128
if (!QueryCacheManager.Cache.TryGetValue(key, out item))
112129
{
113130
item = query.Execute();
114-
item = QueryCacheManager.Cache.Set(key, item, options);
131+
132+
item = QueryCacheManager.Cache.Set(key, item ?? DBNull.Value, options);
115133
QueryCacheManager.AddCacheTag(key, tags);
116134
}
135+
else
136+
{
137+
if (item == DBNull.Value)
138+
{
139+
item = null;
140+
}
141+
}
117142

118143
return (T)item;
119144
}

src/Z.EntityFramework.Plus.EF5.NET40/QueryCache/Extensions/QueryDeferred`/FromCacheAsync.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

88
#if NET45
9+
using System;
910
using System.Threading.Tasks;
11+
1012
#if EF5 || EF6
11-
using System;
1213
using System.Runtime.Caching;
1314

1415
#elif EFCORE
@@ -44,9 +45,16 @@ public static Task<T> FromCacheAsync<T>(this QueryDeferred<T> query, CacheItemPo
4445
if (item == null)
4546
{
4647
item = query.Execute();
47-
item = QueryCacheManager.Cache.AddOrGetExisting(key, item, policy) ?? item;
48+
item = QueryCacheManager.Cache.AddOrGetExisting(key, item ?? DBNull.Value, policy) ?? item;
4849
QueryCacheManager.AddCacheTag(key, tags);
4950
}
51+
else
52+
{
53+
if (item == DBNull.Value)
54+
{
55+
item = null;
56+
}
57+
}
5058

5159
return (T) item;
5260
});
@@ -77,9 +85,16 @@ public static Task<T> FromCacheAsync<T>(this QueryDeferred<T> query, DateTimeOff
7785
if (item == null)
7886
{
7987
item = query.Execute();
80-
item = QueryCacheManager.Cache.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
88+
item = QueryCacheManager.Cache.AddOrGetExisting(key, item ?? DBNull.Value, absoluteExpiration) ?? item;
8189
QueryCacheManager.AddCacheTag(key, tags);
8290
}
91+
else
92+
{
93+
if (item == DBNull.Value)
94+
{
95+
item = null;
96+
}
97+
}
8398

8499
return (T) item;
85100
});
@@ -125,9 +140,16 @@ public static Task<T> FromCacheAsync<T>(this QueryDeferred<T> query, MemoryCache
125140
if (!QueryCacheManager.Cache.TryGetValue(key, out item))
126141
{
127142
item = query.Execute();
128-
item = QueryCacheManager.Cache.Set(key, item, options);
143+
item = QueryCacheManager.Cache.Set(key, item ?? DBNull.Value, options);
129144
QueryCacheManager.AddCacheTag(key, tags);
130145
}
146+
else
147+
{
148+
if (item == DBNull.Value)
149+
{
150+
item = null;
151+
}
152+
}
131153

132154
return (T)item;
133155
});

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
using System.Collections.Concurrent;
99
using System.Collections.Generic;
1010
using System.Data.Common;
11-
using System.Data.SqlClient;
1211
using System.Linq;
1312
using System.Text;
1413

1514
#if EF5
1615
using System.Runtime.Caching;
1716
using System.Data.EntityClient;
17+
using System.Data.SqlClient;
1818

1919
#elif EF6
2020
using System.Data.Entity.Core.EntityClient;
21+
using System.Data.SqlClient;
2122
using System.Runtime.Caching;
2223

2324
#elif EFCORE
@@ -182,6 +183,7 @@ public static string GetCacheKey(IQueryable query, string[] tags)
182183
}
183184

184185
sb.AppendLine(string.Join(";", tags));
186+
sb.AppendLine(query.Expression.ToString());
185187
sb.AppendLine(command.CommandText);
186188

187189
foreach (var parameter in queryContext.ParameterValues)
@@ -261,6 +263,7 @@ public static string GetCacheKey<T>(QueryDeferred<T> query, string[] tags)
261263
}
262264

263265
sb.AppendLine(string.Join(";", tags));
266+
sb.AppendLine(query.Expression.ToString());
264267
sb.AppendLine(command.CommandText);
265268

266269
foreach (var parameter in queryContext.ParameterValues)

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

src/Z.EntityFramework.Plus.EF5/QueryCache/Extensions/QueryDeferred`/FromCache.cs

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

8-
#if EF5 || EF6
98
using System;
9+
10+
#if EF5 || EF6
1011
using System.Runtime.Caching;
1112

1213
#elif EFCORE
@@ -40,9 +41,17 @@ public static T FromCache<T>(this QueryDeferred<T> query, CacheItemPolicy policy
4041
if (item == null)
4142
{
4243
item = query.Execute();
43-
item = QueryCacheManager.Cache.AddOrGetExisting(key, item, policy) ?? item;
44+
45+
item = QueryCacheManager.Cache.AddOrGetExisting(key, item ?? DBNull.Value, policy) ?? item;
4446
QueryCacheManager.AddCacheTag(key, tags);
4547
}
48+
else
49+
{
50+
if (item == DBNull.Value)
51+
{
52+
item = null;
53+
}
54+
}
4655

4756
return (T) item;
4857
}
@@ -68,9 +77,17 @@ public static T FromCache<T>(this QueryDeferred<T> query, DateTimeOffset absolut
6877
if (item == null)
6978
{
7079
item = query.Execute();
71-
item = QueryCacheManager.Cache.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
80+
81+
item = QueryCacheManager.Cache.AddOrGetExisting(key, item ?? DBNull.Value, absoluteExpiration) ?? item;
7282
QueryCacheManager.AddCacheTag(key, tags);
7383
}
84+
else
85+
{
86+
if (item == DBNull.Value)
87+
{
88+
item = null;
89+
}
90+
}
7491

7592
return (T) item;
7693
}
@@ -111,9 +128,17 @@ public static T FromCache<T>(this QueryDeferred<T> query, MemoryCacheEntryOption
111128
if (!QueryCacheManager.Cache.TryGetValue(key, out item))
112129
{
113130
item = query.Execute();
114-
item = QueryCacheManager.Cache.Set(key, item, options);
131+
132+
item = QueryCacheManager.Cache.Set(key, item ?? DBNull.Value, options);
115133
QueryCacheManager.AddCacheTag(key, tags);
116134
}
135+
else
136+
{
137+
if (item == DBNull.Value)
138+
{
139+
item = null;
140+
}
141+
}
117142

118143
return (T)item;
119144
}

src/Z.EntityFramework.Plus.EF5/QueryCache/Extensions/QueryDeferred`/FromCacheAsync.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

88
#if NET45
9+
using System;
910
using System.Threading.Tasks;
11+
1012
#if EF5 || EF6
11-
using System;
1213
using System.Runtime.Caching;
1314

1415
#elif EFCORE
@@ -44,9 +45,16 @@ public static Task<T> FromCacheAsync<T>(this QueryDeferred<T> query, CacheItemPo
4445
if (item == null)
4546
{
4647
item = query.Execute();
47-
item = QueryCacheManager.Cache.AddOrGetExisting(key, item, policy) ?? item;
48+
item = QueryCacheManager.Cache.AddOrGetExisting(key, item ?? DBNull.Value, policy) ?? item;
4849
QueryCacheManager.AddCacheTag(key, tags);
4950
}
51+
else
52+
{
53+
if (item == DBNull.Value)
54+
{
55+
item = null;
56+
}
57+
}
5058

5159
return (T) item;
5260
});
@@ -77,9 +85,16 @@ public static Task<T> FromCacheAsync<T>(this QueryDeferred<T> query, DateTimeOff
7785
if (item == null)
7886
{
7987
item = query.Execute();
80-
item = QueryCacheManager.Cache.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
88+
item = QueryCacheManager.Cache.AddOrGetExisting(key, item ?? DBNull.Value, absoluteExpiration) ?? item;
8189
QueryCacheManager.AddCacheTag(key, tags);
8290
}
91+
else
92+
{
93+
if (item == DBNull.Value)
94+
{
95+
item = null;
96+
}
97+
}
8398

8499
return (T) item;
85100
});
@@ -125,9 +140,16 @@ public static Task<T> FromCacheAsync<T>(this QueryDeferred<T> query, MemoryCache
125140
if (!QueryCacheManager.Cache.TryGetValue(key, out item))
126141
{
127142
item = query.Execute();
128-
item = QueryCacheManager.Cache.Set(key, item, options);
143+
item = QueryCacheManager.Cache.Set(key, item ?? DBNull.Value, options);
129144
QueryCacheManager.AddCacheTag(key, tags);
130145
}
146+
else
147+
{
148+
if (item == DBNull.Value)
149+
{
150+
item = null;
151+
}
152+
}
131153

132154
return (T)item;
133155
});

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
using System.Collections.Concurrent;
99
using System.Collections.Generic;
1010
using System.Data.Common;
11-
using System.Data.SqlClient;
1211
using System.Linq;
1312
using System.Text;
1413

1514
#if EF5
1615
using System.Runtime.Caching;
1716
using System.Data.EntityClient;
17+
using System.Data.SqlClient;
1818

1919
#elif EF6
2020
using System.Data.Entity.Core.EntityClient;
21+
using System.Data.SqlClient;
2122
using System.Runtime.Caching;
2223

2324
#elif EFCORE
@@ -182,6 +183,7 @@ public static string GetCacheKey(IQueryable query, string[] tags)
182183
}
183184

184185
sb.AppendLine(string.Join(";", tags));
186+
sb.AppendLine(query.Expression.ToString());
185187
sb.AppendLine(command.CommandText);
186188

187189
foreach (var parameter in queryContext.ParameterValues)
@@ -261,6 +263,7 @@ public static string GetCacheKey<T>(QueryDeferred<T> query, string[] tags)
261263
}
262264

263265
sb.AppendLine(string.Join(";", tags));
266+
sb.AppendLine(query.Expression.ToString());
264267
sb.AppendLine(command.CommandText);
265268

266269
foreach (var parameter in queryContext.ParameterValues)

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

0 commit comments

Comments
 (0)