Skip to content

Commit c6677a0

Browse files
Support caching queries with autodiscovered types (nhibernate#3184)
See nhibernate#3169
1 parent d182220 commit c6677a0

15 files changed

+1716
-261
lines changed

src/NHibernate.Test/Async/CacheTest/JsonSerializerCacheFixture.cs

Lines changed: 536 additions & 0 deletions
Large diffs are not rendered by default.

src/NHibernate.Test/Async/QueryTest/MultiCriteriaFixture.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()
139139

140140
await (CreateItemsAsync());
141141

142-
await (DoMutiQueryAndAssertAsync());
142+
await (DoMultiQueryAndAssertAsync());
143143

144144
Assert.AreEqual(1, cacheHashtable.Count);
145145
}
@@ -148,19 +148,20 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()
148148
public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
149149
{
150150
await (CreateItemsAsync());
151-
//set the query in the cache
152-
await (DoMutiQueryAndAssertAsync());
151+
// Set the query in the cache.
152+
await (DoMultiQueryAndAssertAsync());
153153

154154
var cacheHashtable = MultipleQueriesFixtureAsync.GetHashTableUsedAsQueryCache(Sfi);
155-
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
156-
var cachedQuery = (IList)cachedListEntry[1];
155+
var cachedListEntry = (IList) new ArrayList(cacheHashtable.Values)[0];
156+
// The first element is a timestamp, then only we have the cached data.
157+
var cachedQuery = (IList) cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
157158

158-
var firstQueryResults = (IList)cachedQuery[0];
159+
var firstQueryResults = (IList) cachedQuery[0];
159160
firstQueryResults.Clear();
160161
firstQueryResults.Add(3);
161162
firstQueryResults.Add(4);
162163

163-
var secondQueryResults = (IList)cachedQuery[1];
164+
var secondQueryResults = (IList) cachedQuery[1];
164165
secondQueryResults[0] = 2;
165166

166167
using (var s = Sfi.OpenSession())
@@ -172,9 +173,9 @@ public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
172173
.Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount()));
173174
multiCriteria.SetCacheable(true);
174175
var results = await (multiCriteria.ListAsync());
175-
var items = (IList)results[0];
176+
var items = (IList) results[0];
176177
Assert.AreEqual(2, items.Count);
177-
var count = (int)((IList)results[1])[0];
178+
var count = (int) ((IList) results[1])[0];
178179
Assert.AreEqual(2L, count);
179180
}
180181
}
@@ -184,12 +185,12 @@ public async Task CanUpdateStatisticsWhenGetMultiQueryFromSecondLevelCacheAsync(
184185
{
185186
await (CreateItemsAsync());
186187

187-
await (DoMutiQueryAndAssertAsync());
188+
await (DoMultiQueryAndAssertAsync());
188189
Assert.AreEqual(0, Sfi.Statistics.QueryCacheHitCount);
189190
Assert.AreEqual(1, Sfi.Statistics.QueryCacheMissCount);
190191
Assert.AreEqual(1, Sfi.Statistics.QueryCachePutCount);
191192

192-
await (DoMutiQueryAndAssertAsync());
193+
await (DoMultiQueryAndAssertAsync());
193194
Assert.AreEqual(1, Sfi.Statistics.QueryCacheHitCount);
194195
Assert.AreEqual(1, Sfi.Statistics.QueryCacheMissCount);
195196
Assert.AreEqual(1, Sfi.Statistics.QueryCachePutCount);
@@ -390,7 +391,7 @@ public async Task CanNotRetrieveDetachedCriteriaResultWithUnknownKeyAsync()
390391
}
391392
}
392393

393-
private async Task DoMutiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
394+
private async Task DoMultiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
394395
{
395396
using (var s = OpenSession())
396397
{
@@ -401,9 +402,9 @@ public async Task CanNotRetrieveDetachedCriteriaResultWithUnknownKeyAsync()
401402
.Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount()));
402403
multiCriteria.SetCacheable(true);
403404
var results = await (multiCriteria.ListAsync(cancellationToken));
404-
var items = (IList)results[0];
405+
var items = (IList) results[0];
405406
Assert.AreEqual(89, items.Count);
406-
var count = (int)((IList)results[1])[0];
407+
var count = (int) ((IList) results[1])[0];
407408
Assert.AreEqual(99L, count);
408409
}
409410
}

src/NHibernate.Test/Async/QueryTest/MultipleMixedQueriesFixture.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,20 @@ public void NH_1085_WillGiveReasonableErrorIfBadParameterNameAsync()
7878
public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
7979
{
8080
await (CreateItemsAsync());
81-
//set the query in the cache
82-
await (DoMutiQueryAndAssertAsync());
81+
// Set the query in the cache.
82+
await (DoMultiQueryAndAssertAsync());
8383

8484
var cacheHashtable = MultipleQueriesFixtureAsync.GetHashTableUsedAsQueryCache(Sfi);
85-
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
86-
var cachedQuery = (IList)cachedListEntry[1];
85+
var cachedListEntry = (IList) new ArrayList(cacheHashtable.Values)[0];
86+
// The first element is a timestamp, then only we have the cached data.
87+
var cachedQuery = (IList) cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
8788

88-
var firstQueryResults = (IList)cachedQuery[0];
89+
var firstQueryResults = (IList) cachedQuery[0];
8990
firstQueryResults.Clear();
9091
firstQueryResults.Add(3);
9192
firstQueryResults.Add(4);
9293

93-
var secondQueryResults = (IList)cachedQuery[1];
94+
var secondQueryResults = (IList) cachedQuery[1];
9495
secondQueryResults[0] = 2L;
9596

9697
using (var s = Sfi.OpenSession())
@@ -103,9 +104,9 @@ public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
103104
.SetInt32(0, 50));
104105
multiQuery.SetCacheable(true);
105106
var results = await (multiQuery.ListAsync());
106-
var items = (IList)results[0];
107+
var items = (IList) results[0];
107108
Assert.AreEqual(2, items.Count);
108-
var count = (long)((IList)results[1])[0];
109+
var count = (long) ((IList) results[1])[0];
109110
Assert.AreEqual(2L, count);
110111
}
111112
}
@@ -185,12 +186,12 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()
185186

186187
await (CreateItemsAsync());
187188

188-
await (DoMutiQueryAndAssertAsync());
189+
await (DoMultiQueryAndAssertAsync());
189190

190191
Assert.AreEqual(1, cacheHashtable.Count);
191192
}
192193

193-
private async Task DoMutiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
194+
private async Task DoMultiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
194195
{
195196
using (var s = OpenSession())
196197
{
@@ -202,9 +203,9 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()
202203
.SetInt32(0, 50));
203204
multiQuery.SetCacheable(true);
204205
var results = await (multiQuery.ListAsync(cancellationToken));
205-
var items = (IList)results[0];
206+
var items = (IList) results[0];
206207
Assert.AreEqual(89, items.Count);
207-
var count = (long)((IList)results[1])[0];
208+
var count = (long) ((IList) results[1])[0];
208209
Assert.AreEqual(99L, count);
209210
}
210211
}

src/NHibernate.Test/Async/QueryTest/MultipleQueriesFixture.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,20 @@ public void NH_1085_WillGiveReasonableErrorIfBadParameterNameAsync()
8181
public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
8282
{
8383
await (CreateItemsAsync());
84-
//set the query in the cache
85-
await (DoMutiQueryAndAssertAsync());
84+
// Set the query in the cache.
85+
await (DoMultiQueryAndAssertAsync());
8686

8787
var cacheHashtable = GetHashTableUsedAsQueryCache(Sfi);
88-
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
89-
var cachedQuery = (IList)cachedListEntry[1];
88+
var cachedListEntry = (IList) new ArrayList(cacheHashtable.Values)[0];
89+
// The first element is a timestamp, then only we have the cached data.
90+
var cachedQuery = (IList) cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
9091

91-
var firstQueryResults = (IList)cachedQuery[0];
92+
var firstQueryResults = (IList) cachedQuery[0];
9293
firstQueryResults.Clear();
9394
firstQueryResults.Add(3);
9495
firstQueryResults.Add(4);
9596

96-
var secondQueryResults = (IList)cachedQuery[1];
97+
var secondQueryResults = (IList) cachedQuery[1];
9798
secondQueryResults[0] = 2L;
9899

99100
using (var s = Sfi.OpenSession())
@@ -106,9 +107,9 @@ public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
106107
.SetInt32(0, 50));
107108
multiQuery.SetCacheable(true);
108109
var results = await (multiQuery.ListAsync());
109-
var items = (IList)results[0];
110+
var items = (IList) results[0];
110111
Assert.AreEqual(2, items.Count);
111-
var count = (long)((IList)results[1])[0];
112+
var count = (long) ((IList) results[1])[0];
112113
Assert.AreEqual(2L, count);
113114
}
114115
}
@@ -187,12 +188,12 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()
187188

188189
await (CreateItemsAsync());
189190

190-
await (DoMutiQueryAndAssertAsync());
191+
await (DoMultiQueryAndAssertAsync());
191192

192193
Assert.AreEqual(1, cacheHashtable.Count);
193194
}
194195

195-
private async Task DoMutiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
196+
private async Task DoMultiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
196197
{
197198
using (var s = OpenSession())
198199
{
@@ -204,9 +205,9 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()
204205
.SetInt32(0, 50));
205206
multiQuery.SetCacheable(true);
206207
var results = await (multiQuery.ListAsync(cancellationToken));
207-
var items = (IList)results[0];
208+
var items = (IList) results[0];
208209
Assert.AreEqual(89, items.Count);
209-
var count = (long)((IList)results[1])[0];
210+
var count = (long) ((IList) results[1])[0];
210211
Assert.AreEqual(99L, count);
211212
}
212213
}

0 commit comments

Comments
 (0)