55
66namespace NHibernate . Test . Linq
77{
8- [ TestFixture ]
9- public class QueryCacheableTests : LinqTestCase
10- {
11- protected override void Configure ( Configuration cfg )
12- {
13- cfg . SetProperty ( Environment . UseQueryCache , "true" ) ;
14- cfg . SetProperty ( Environment . GenerateStatistics , "true" ) ;
15- base . Configure ( cfg ) ;
16- }
17-
18- [ Test ]
19- public void QueryIsCacheable ( )
20- {
21- Sfi . Statistics . Clear ( ) ;
22- Sfi . QueryCache . Clear ( ) ;
23-
24- var x = ( from c in db . Customers
25- select c ) . Cacheable ( ) . ToList ( ) ;
26-
27- var x2 = ( from c in db . Customers
28- select c ) . Cacheable ( ) . ToList ( ) ;
29-
30- Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 1 ) ) ;
31- Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 1 ) ) ;
32- Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 1 ) ) ;
33- }
34-
35- [ Test ]
36- public void QueryIsCacheable2 ( )
37- {
38- Sfi . Statistics . Clear ( ) ;
39- Sfi . QueryCache . Clear ( ) ;
40-
41- var x = ( from c in db . Customers
42- select c ) . Cacheable ( ) . ToList ( ) ;
43-
44- var x2 = ( from c in db . Customers
45- select c ) . ToList ( ) ;
46-
47- Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 2 ) ) ;
48- Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 1 ) ) ;
49- Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 0 ) ) ;
50- }
51-
52- [ Test ]
53- public void QueryIsCacheable3 ( )
54- {
55- Sfi . Statistics . Clear ( ) ;
56- Sfi . QueryCache . Clear ( ) ;
57-
58- var x = ( from c in db . Customers . Cacheable ( )
59- select c ) . ToList ( ) ;
60-
61- var x2 = ( from c in db . Customers
62- select c ) . ToList ( ) ;
63-
64- Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 2 ) ) ;
65- Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 1 ) ) ;
66- Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 0 ) ) ;
67- }
68-
69- [ Test ]
70- public void QueryIsCacheableWithRegion ( )
71- {
72- Sfi . Statistics . Clear ( ) ;
73- Sfi . QueryCache . Clear ( ) ;
74-
75- var x = ( from c in db . Customers
76- select c ) . Cacheable ( ) . CacheRegion ( "test" ) . ToList ( ) ;
77-
78- var x2 = ( from c in db . Customers
79- select c ) . Cacheable ( ) . CacheRegion ( "test" ) . ToList ( ) ;
80-
81- var x3 = ( from c in db . Customers
82- select c ) . Cacheable ( ) . CacheRegion ( "other" ) . ToList ( ) ;
83-
84- Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 2 ) ) ;
85- Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 2 ) ) ;
86- Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 1 ) ) ;
87- }
88-
89- [ Test ]
90- public void CacheableBeforeOtherClauses ( )
91- {
92- Sfi . Statistics . Clear ( ) ;
93- Sfi . QueryCache . Clear ( ) ;
94-
95- db . Customers . Cacheable ( ) . Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 ) . ToList ( ) ;
96- db . Customers . Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 ) . ToList ( ) ;
97-
98- Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 2 ) ) ;
99- Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 1 ) ) ;
100- Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 0 ) ) ;
101- }
102-
103- [ Test ]
104- public void CacheableRegionBeforeOtherClauses ( )
105- {
106- Sfi . Statistics . Clear ( ) ;
107- Sfi . QueryCache . Clear ( ) ;
108-
109- db . Customers . Cacheable ( ) . CacheRegion ( "test" ) . Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 ) . ToList ( ) ;
110- db . Customers . Cacheable ( ) . CacheRegion ( "test" ) . Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 ) . ToList ( ) ;
111- db . Customers . Cacheable ( ) . CacheRegion ( "other" ) . Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 ) . ToList ( ) ;
112-
113- Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 2 ) ) ;
114- Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 2 ) ) ;
115- Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 1 ) ) ;
116- }
8+ [ TestFixture ]
9+ public class QueryCacheableTests : LinqTestCase
10+ {
11+ protected override void Configure ( Configuration cfg )
12+ {
13+ cfg . SetProperty ( Environment . UseQueryCache , "true" ) ;
14+ cfg . SetProperty ( Environment . GenerateStatistics , "true" ) ;
15+ base . Configure ( cfg ) ;
16+ }
17+
18+ [ Test ]
19+ public void QueryIsCacheable ( )
20+ {
21+ Sfi . Statistics . Clear ( ) ;
22+ Sfi . QueryCache . Clear ( ) ;
23+
24+ var x = ( from c in db . Customers
25+ select c )
26+ . SetOptions ( o => o . SetCacheable ( true ) )
27+ . ToList ( ) ;
28+
29+ var x2 = ( from c in db . Customers
30+ select c )
31+ . SetOptions ( o => o . SetCacheable ( true ) )
32+ . ToList ( ) ;
33+
34+ Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 1 ) , "Unexpected execution count" ) ;
35+ Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 1 ) , "Unexpected cache put count" ) ;
36+ Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 1 ) , "Unexpected cache hit count" ) ;
37+ }
38+
39+ [ Test ]
40+ public void QueryIsCacheable2 ( )
41+ {
42+ Sfi . Statistics . Clear ( ) ;
43+ Sfi . QueryCache . Clear ( ) ;
44+
45+ var x = ( from c in db . Customers
46+ select c )
47+ . SetOptions ( o => o . SetCacheable ( true ) )
48+ . ToList ( ) ;
49+
50+ var x2 = ( from c in db . Customers
51+ select c ) . ToList ( ) ;
52+
53+ Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 2 ) , "Unexpected execution count" ) ;
54+ Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 1 ) , "Unexpected cache put count" ) ;
55+ Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 0 ) , "Unexpected cache hit count" ) ;
56+ }
57+
58+ [ Test ]
59+ public void QueryIsCacheable3 ( )
60+ {
61+ Sfi . Statistics . Clear ( ) ;
62+ Sfi . QueryCache . Clear ( ) ;
63+
64+ var x = ( from c in db . Customers . SetOptions ( o => o . SetCacheable ( true ) )
65+ select c ) . ToList ( ) ;
66+
67+ var x2 = ( from c in db . Customers
68+ select c ) . ToList ( ) ;
69+
70+ Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 2 ) , "Unexpected execution count" ) ;
71+ Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 1 ) , "Unexpected cache put count" ) ;
72+ Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 0 ) , "Unexpected cache hit count" ) ;
73+ }
74+
75+ [ Test ]
76+ public void QueryIsCacheableWithRegion ( )
77+ {
78+ Sfi . Statistics . Clear ( ) ;
79+ Sfi . QueryCache . Clear ( ) ;
80+
81+ var x = ( from c in db . Customers
82+ select c )
83+ . SetOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "test" ) )
84+ . ToList ( ) ;
85+
86+ var x2 = ( from c in db . Customers
87+ select c )
88+ . SetOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "test" ) )
89+ . ToList ( ) ;
90+
91+ var x3 = ( from c in db . Customers
92+ select c )
93+ . SetOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "other" ) )
94+ . ToList ( ) ;
95+
96+ Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 2 ) , "Unexpected execution count" ) ;
97+ Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 2 ) , "Unexpected cache put count" ) ;
98+ Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 1 ) , "Unexpected cache hit count" ) ;
99+ }
100+
101+ [ Test ]
102+ public void CacheableBeforeOtherClauses ( )
103+ {
104+ Sfi . Statistics . Clear ( ) ;
105+ Sfi . QueryCache . Clear ( ) ;
106+
107+ db . Customers
108+ . SetOptions ( o => o . SetCacheable ( true ) )
109+ . Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 ) . ToList ( ) ;
110+ db . Customers . Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 ) . ToList ( ) ;
111+
112+ Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 2 ) , "Unexpected execution count" ) ;
113+ Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 1 ) , "Unexpected cache put count" ) ;
114+ Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 0 ) , "Unexpected cache hit count" ) ;
115+ }
116+
117+ [ Test ]
118+ public void CacheableRegionBeforeOtherClauses ( )
119+ {
120+ Sfi . Statistics . Clear ( ) ;
121+ Sfi . QueryCache . Clear ( ) ;
122+
123+ db . Customers
124+ . SetOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "test" ) )
125+ . Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 )
126+ . ToList ( ) ;
127+ db . Customers
128+ . SetOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "test" ) )
129+ . Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 )
130+ . ToList ( ) ;
131+ db . Customers
132+ . SetOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "other" ) )
133+ . Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 )
134+ . ToList ( ) ;
135+
136+ Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 2 ) , "Unexpected execution count" ) ;
137+ Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 2 ) , "Unexpected cache put count" ) ;
138+ Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 1 ) , "Unexpected cache hit count" ) ;
139+ }
140+
141+ [ Test ]
142+ public void CacheableRegionSwitched ( )
143+ {
144+ Sfi . Statistics . Clear ( ) ;
145+ Sfi . QueryCache . Clear ( ) ;
146+
147+ db . Customers
148+ . Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 )
149+ . SetOptions ( o => o . SetCacheable ( true ) . SetCacheRegion ( "test" ) )
150+ . ToList ( ) ;
151+ db . Customers
152+ . Where ( c => c . ContactName != c . CompanyName ) . Take ( 1 )
153+ . SetOptions ( o => o . SetCacheRegion ( "test" ) . SetCacheable ( true ) )
154+ . ToList ( ) ;
155+
156+ Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 1 ) , "Unexpected execution count" ) ;
157+ Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 1 ) , "Unexpected cache put count" ) ;
158+ Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 1 ) , "Unexpected cache hit count" ) ;
159+ }
117160
118161 [ Test ]
119162 public void GroupByQueryIsCacheable ( )
@@ -124,8 +167,8 @@ public void GroupByQueryIsCacheable()
124167 var c = db
125168 . Customers
126169 . GroupBy ( x => x . Address . Country )
127- . Select ( x=> x . Key )
128- . Cacheable ( )
170+ . Select ( x => x . Key )
171+ . SetOptions ( o => o . SetCacheable ( true ) )
129172 . ToList ( ) ;
130173
131174 c = db
@@ -138,12 +181,12 @@ public void GroupByQueryIsCacheable()
138181 . Customers
139182 . GroupBy ( x => x . Address . Country )
140183 . Select ( x => x . Key )
141- . Cacheable ( )
184+ . SetOptions ( o => o . SetCacheable ( true ) )
142185 . ToList ( ) ;
143186
144- Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 2 ) ) ;
145- Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 1 ) ) ;
146- Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 1 ) ) ;
187+ Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 2 ) , "Unexpected execution count" ) ;
188+ Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 1 ) , "Unexpected cache put count" ) ;
189+ Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 1 ) , "Unexpected cache hit count" ) ;
147190 }
148191
149192 [ Test ]
@@ -153,7 +196,8 @@ public void GroupByQueryIsCacheable2()
153196 Sfi . QueryCache . Clear ( ) ;
154197
155198 var c = db
156- . Customers . Cacheable ( )
199+ . Customers
200+ . SetOptions ( o => o . SetCacheable ( true ) )
157201 . GroupBy ( x => x . Address . Country )
158202 . Select ( x => x . Key )
159203 . ToList ( ) ;
@@ -165,14 +209,15 @@ public void GroupByQueryIsCacheable2()
165209 . ToList ( ) ;
166210
167211 c = db
168- . Customers . Cacheable ( )
212+ . Customers
213+ . SetOptions ( o => o . SetCacheable ( true ) )
169214 . GroupBy ( x => x . Address . Country )
170215 . Select ( x => x . Key )
171216 . ToList ( ) ;
172217
173- Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 2 ) ) ;
174- Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 1 ) ) ;
175- Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 1 ) ) ;
218+ Assert . That ( Sfi . Statistics . QueryExecutionCount , Is . EqualTo ( 2 ) , "Unexpected execution count" ) ;
219+ Assert . That ( Sfi . Statistics . QueryCachePutCount , Is . EqualTo ( 1 ) , "Unexpected cache put count" ) ;
220+ Assert . That ( Sfi . Statistics . QueryCacheHitCount , Is . EqualTo ( 1 ) , "Unexpected cache hit count" ) ;
176221 }
177222 }
178223}
0 commit comments