16
16
namespace NHibernate . Test . Linq
17
17
{
18
18
using System . Threading . Tasks ;
19
- using System . Threading ;
20
19
[ TestFixture ]
21
20
public class QueryCacheableTestsAsync : LinqTestCase
22
21
{
@@ -31,16 +30,16 @@ protected override void Configure(Configuration cfg)
31
30
public async Task QueryIsCacheableAsync ( )
32
31
{
33
32
Sfi . Statistics . Clear ( ) ;
34
- await ( Sfi . QueryCache . ClearAsync ( CancellationToken . None ) ) ;
33
+ await ( Sfi . EvictQueriesAsync ( ) ) ;
35
34
36
35
var x = await ( ( from c in db . Customers
37
36
select c )
38
- . SetOptions ( o => o . SetCacheable ( true ) )
37
+ . WithOptions ( o => o . SetCacheable ( true ) )
39
38
. ToListAsync ( ) ) ;
40
39
41
40
var x2 = await ( ( from c in db . Customers
42
41
select c )
43
- . SetOptions ( o => o . SetCacheable ( true ) )
42
+ . WithOptions ( o => o . SetCacheable ( true ) )
44
43
. ToListAsync ( ) ) ;
45
44
46
45
Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 1 ) , "Unexpected execution count" ) ;
@@ -52,11 +51,11 @@ public async Task QueryIsCacheableAsync()
52
51
public async Task QueryIsCacheable2Async ( )
53
52
{
54
53
Sfi . Statistics . Clear ( ) ;
55
- await ( Sfi . QueryCache . ClearAsync ( CancellationToken . None ) ) ;
54
+ await ( Sfi . EvictQueriesAsync ( ) ) ;
56
55
57
56
var x = await ( ( from c in db . Customers
58
57
select c )
59
- . SetOptions ( o => o . SetCacheable ( true ) )
58
+ . WithOptions ( o => o . SetCacheable ( true ) )
60
59
. ToListAsync ( ) ) ;
61
60
62
61
var x2 = await ( ( from c in db . Customers
@@ -71,9 +70,9 @@ public async Task QueryIsCacheable2Async()
71
70
public async Task QueryIsCacheable3Async ( )
72
71
{
73
72
Sfi . Statistics . Clear ( ) ;
74
- await ( Sfi . QueryCache . ClearAsync ( CancellationToken . None ) ) ;
73
+ await ( Sfi . EvictQueriesAsync ( ) ) ;
75
74
76
- var x = await ( ( from c in db . Customers . SetOptions ( o => o . SetCacheable ( true ) )
75
+ var x = await ( ( from c in db . Customers . WithOptions ( o => o . SetCacheable ( true ) )
77
76
select c ) . ToListAsync ( ) ) ;
78
77
79
78
var x2 = await ( ( from c in db . Customers
@@ -88,21 +87,23 @@ public async Task QueryIsCacheable3Async()
88
87
public async Task QueryIsCacheableWithRegionAsync ( )
89
88
{
90
89
Sfi . Statistics . Clear ( ) ;
91
- await ( Sfi . QueryCache . ClearAsync ( CancellationToken . None ) ) ;
90
+ await ( Sfi . EvictQueriesAsync ( ) ) ;
91
+ await ( Sfi . EvictQueriesAsync ( "test" ) ) ;
92
+ await ( Sfi . EvictQueriesAsync ( "other" ) ) ;
92
93
93
94
var x = await ( ( from c in db . Customers
94
95
select c )
95
- . SetOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "test" ) )
96
+ . WithOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "test" ) )
96
97
. ToListAsync ( ) ) ;
97
98
98
99
var x2 = await ( ( from c in db . Customers
99
100
select c )
100
- . SetOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "test" ) )
101
+ . WithOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "test" ) )
101
102
. ToListAsync ( ) ) ;
102
103
103
104
var x3 = await ( ( from c in db . Customers
104
105
select c )
105
- . SetOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "other" ) )
106
+ . WithOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "other" ) )
106
107
. ToListAsync ( ) ) ;
107
108
108
109
Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 2 ) , "Unexpected execution count" ) ;
@@ -114,10 +115,10 @@ public async Task QueryIsCacheableWithRegionAsync()
114
115
public async Task CacheableBeforeOtherClausesAsync ( )
115
116
{
116
117
Sfi . Statistics . Clear ( ) ;
117
- await ( Sfi . QueryCache . ClearAsync ( CancellationToken . None ) ) ;
118
+ await ( Sfi . EvictQueriesAsync ( ) ) ;
118
119
119
120
await ( db . Customers
120
- . SetOptions ( o => o . SetCacheable ( true ) )
121
+ . WithOptions ( o => o . SetCacheable ( true ) )
121
122
. Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 ) . ToListAsync ( ) ) ;
122
123
await ( db . Customers . Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 ) . ToListAsync ( ) ) ;
123
124
@@ -130,18 +131,20 @@ public async Task CacheableBeforeOtherClausesAsync()
130
131
public async Task CacheableRegionBeforeOtherClausesAsync ( )
131
132
{
132
133
Sfi . Statistics . Clear ( ) ;
133
- await ( Sfi . QueryCache . ClearAsync ( CancellationToken . None ) ) ;
134
+ await ( Sfi . EvictQueriesAsync ( ) ) ;
135
+ await ( Sfi . EvictQueriesAsync ( "test" ) ) ;
136
+ await ( Sfi . EvictQueriesAsync ( "other" ) ) ;
134
137
135
138
await ( db . Customers
136
- . SetOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "test" ) )
139
+ . WithOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "test" ) )
137
140
. Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 )
138
141
. ToListAsync ( ) ) ;
139
142
await ( db . Customers
140
- . SetOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "test" ) )
143
+ . WithOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "test" ) )
141
144
. Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 )
142
145
. ToListAsync ( ) ) ;
143
146
await ( db . Customers
144
- . SetOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "other" ) )
147
+ . WithOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "other" ) )
145
148
. Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 )
146
149
. ToListAsync ( ) ) ;
147
150
@@ -154,15 +157,17 @@ public async Task CacheableRegionBeforeOtherClausesAsync()
154
157
public async Task CacheableRegionSwitchedAsync ( )
155
158
{
156
159
Sfi . Statistics . Clear ( ) ;
157
- await ( Sfi . QueryCache . ClearAsync ( CancellationToken . None ) ) ;
160
+ await ( Sfi . EvictQueriesAsync ( ) ) ;
161
+ await ( Sfi . EvictQueriesAsync ( "test" ) ) ;
158
162
159
163
await ( db . Customers
160
164
. Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 )
161
- . SetOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "test" ) )
165
+ . WithOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "test" ) )
162
166
. ToListAsync ( ) ) ;
167
+
163
168
await ( db . Customers
164
169
. Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 )
165
- . SetOptions ( o => o . SetCacheRegion ( "test" ) . SetCacheable ( true ) )
170
+ . WithOptions ( o => o . SetCacheRegion ( "test" ) . SetCacheable ( true ) )
166
171
. ToListAsync ( ) ) ;
167
172
168
173
Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 1 ) , "Unexpected execution count" ) ;
@@ -174,13 +179,13 @@ public async Task CacheableRegionSwitchedAsync()
174
179
public async Task GroupByQueryIsCacheableAsync ( )
175
180
{
176
181
Sfi . Statistics . Clear ( ) ;
177
- await ( Sfi . QueryCache . ClearAsync ( CancellationToken . None ) ) ;
182
+ await ( Sfi . EvictQueriesAsync ( ) ) ;
178
183
179
184
var c = await ( db
180
185
. Customers
181
186
. GroupBy ( x => x . Address . Country )
182
187
. Select ( x => x . Key )
183
- . SetOptions ( o => o . SetCacheable ( true ) )
188
+ . WithOptions ( o => o . SetCacheable ( true ) )
184
189
. ToListAsync ( ) ) ;
185
190
186
191
c = await ( db
@@ -193,7 +198,7 @@ public async Task GroupByQueryIsCacheableAsync()
193
198
. Customers
194
199
. GroupBy ( x => x . Address . Country )
195
200
. Select ( x => x . Key )
196
- . SetOptions ( o => o . SetCacheable ( true ) )
201
+ . WithOptions ( o => o . SetCacheable ( true ) )
197
202
. ToListAsync ( ) ) ;
198
203
199
204
Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 2 ) , "Unexpected execution count" ) ;
@@ -205,11 +210,11 @@ public async Task GroupByQueryIsCacheableAsync()
205
210
public async Task GroupByQueryIsCacheable2Async ( )
206
211
{
207
212
Sfi . Statistics . Clear ( ) ;
208
- await ( Sfi . QueryCache . ClearAsync ( CancellationToken . None ) ) ;
213
+ await ( Sfi . EvictQueriesAsync ( ) ) ;
209
214
210
215
var c = await ( db
211
216
. Customers
212
- . SetOptions ( o => o . SetCacheable ( true ) )
217
+ . WithOptions ( o => o . SetCacheable ( true ) )
213
218
. GroupBy ( x => x . Address . Country )
214
219
. Select ( x => x . Key )
215
220
. ToListAsync ( ) ) ;
@@ -222,7 +227,7 @@ public async Task GroupByQueryIsCacheable2Async()
222
227
223
228
c = await ( db
224
229
. Customers
225
- . SetOptions ( o => o . SetCacheable ( true ) )
230
+ . WithOptions ( o => o . SetCacheable ( true ) )
226
231
. GroupBy ( x => x . Address . Country )
227
232
. Select ( x => x . Key )
228
233
. ToListAsync ( ) ) ;
@@ -231,5 +236,50 @@ public async Task GroupByQueryIsCacheable2Async()
231
236
Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 1 ) , "Unexpected cache put count" ) ;
232
237
Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 1 ) , "Unexpected cache hit count" ) ;
233
238
}
239
+
240
+ [ Test ]
241
+ public async Task CanBeCombinedWithFetchAsync ( )
242
+ {
243
+ //NH-2587
244
+ //NH-3982 (GH-1372)
245
+
246
+ Sfi . Statistics . Clear ( ) ;
247
+ await ( Sfi . EvictQueriesAsync ( ) ) ;
248
+
249
+ await ( db . Customers
250
+ . WithOptions ( o => o . SetCacheable ( true ) )
251
+ . ToListAsync ( ) ) ;
252
+
253
+ await ( db . Orders
254
+ . WithOptions ( o => o . SetCacheable ( true ) )
255
+ . ToListAsync ( ) ) ;
256
+
257
+ await ( db . Customers
258
+ . WithOptions ( o => o . SetCacheable ( true ) )
259
+ . Fetch ( x => x . Orders )
260
+ . ToListAsync ( ) ) ;
261
+
262
+ await ( db . Orders
263
+ . WithOptions ( o => o . SetCacheable ( true ) )
264
+ . Fetch ( x => x . OrderLines )
265
+ . ToListAsync ( ) ) ;
266
+
267
+ var customer = await ( db . Customers
268
+ . WithOptions ( o => o . SetCacheable ( true ) )
269
+ . Fetch ( x => x . Address )
270
+ . Where ( x => x . CustomerId == "VINET" )
271
+ . SingleOrDefaultAsync ( ) ) ;
272
+
273
+ customer = await ( db . Customers
274
+ . WithOptions ( o => o . SetCacheable ( true ) )
275
+ . Fetch ( x => x . Address )
276
+ . Where ( x => x . CustomerId == "VINET" )
277
+ . SingleOrDefaultAsync ( ) ) ;
278
+
279
+ Assert . That ( NHibernateUtil . IsInitialized ( customer . Address ) , Is . True , "Expected the fetched Address to be initialized" ) ;
280
+ Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 5 ) , "Unexpected execution count" ) ;
281
+ Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 5 ) , "Unexpected cache put count" ) ;
282
+ Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 1 ) , "Unexpected cache hit count" ) ;
283
+ }
234
284
}
235
285
}
0 commit comments