Skip to content

Commit 10f9746

Browse files
committed
Handle cacheable queries
1 parent f17a147 commit 10f9746

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

src/NHibernate/Async/Multi/QueryBatch.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,33 +139,29 @@ protected async Task ExecuteBatchedAsync(CancellationToken cancellationToken)
139139
}
140140

141141
var rowCount = 0;
142+
CacheBatcher cacheBatcher = null;
142143
try
143144
{
144145
if (resultSetsCommand.HasQueries)
145146
{
147+
cacheBatcher = new CacheBatcher(Session);
146148
using (var reader = await (resultSetsCommand.GetReaderAsync(Timeout, cancellationToken)).ConfigureAwait(false))
147149
{
148-
var cacheBatcher = new CacheBatcher(Session);
149150
foreach (var query in _queries)
150151
{
151152
if (query.CachingInformation != null)
152153
{
153-
foreach (var cachingInfo in query.CachingInformation.Where(ci => ci.IsCacheable))
154+
foreach (var cachingInfo in query.CachingInformation)
154155
{
155156
cachingInfo.SetCacheBatcher(cacheBatcher);
156157
}
157158
}
158159

159160
rowCount += await (query.ProcessResultsSetAsync(reader, cancellationToken)).ConfigureAwait(false);
160161
}
161-
await (cacheBatcher.ExecuteBatchAsync(cancellationToken)).ConfigureAwait(false);
162162
}
163163
}
164164

165-
// Query cacheable results must be cached untransformed: the put does not need to wait for
166-
// the ProcessResults.
167-
await (PutCacheableResultsAsync(cancellationToken)).ConfigureAwait(false);
168-
169165
foreach (var query in _queries)
170166
{
171167
//TODO 6.0: Replace with query.ProcessResults();
@@ -174,6 +170,17 @@ protected async Task ExecuteBatchedAsync(CancellationToken cancellationToken)
174170
else
175171
query.ProcessResults();
176172
}
173+
174+
var executeBatchTask = cacheBatcher?.ExecuteBatchAsync(cancellationToken);
175+
176+
if (executeBatchTask != null)
177+
178+
{
179+
180+
await (executeBatchTask).ConfigureAwait(false);
181+
182+
}
183+
await (PutCacheableResultsAsync(cancellationToken)).ConfigureAwait(false);
177184
}
178185
catch (OperationCanceledException) { throw; }
179186
catch (Exception sqle)

src/NHibernate/Async/Multi/QueryBatchItemBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ public async Task ProcessResultsAsync(CancellationToken cancellationToken)
131131
cancellationToken.ThrowIfCancellationRequested();
132132
ThrowIfNotInitialized();
133133

134-
await (InitializeEntitiesAndCollectionsAsync(_reader, _hydratedObjects, cancellationToken)).ConfigureAwait(false);
134+
using (Session.SwitchCacheMode(_cacheMode))
135+
await (InitializeEntitiesAndCollectionsAsync(_reader, _hydratedObjects, cancellationToken)).ConfigureAwait(false);
136+
135137
for (var i = 0; i < _queryInfos.Count; i++)
136138
{
137139
var queryInfo = _queryInfos[i];

src/NHibernate/Multi/QueryBatch.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,33 +151,29 @@ protected void ExecuteBatched()
151151
}
152152

153153
var rowCount = 0;
154+
CacheBatcher cacheBatcher = null;
154155
try
155156
{
156157
if (resultSetsCommand.HasQueries)
157158
{
159+
cacheBatcher = new CacheBatcher(Session);
158160
using (var reader = resultSetsCommand.GetReader(Timeout))
159161
{
160-
var cacheBatcher = new CacheBatcher(Session);
161162
foreach (var query in _queries)
162163
{
163164
if (query.CachingInformation != null)
164165
{
165-
foreach (var cachingInfo in query.CachingInformation.Where(ci => ci.IsCacheable))
166+
foreach (var cachingInfo in query.CachingInformation)
166167
{
167168
cachingInfo.SetCacheBatcher(cacheBatcher);
168169
}
169170
}
170171

171172
rowCount += query.ProcessResultsSet(reader);
172173
}
173-
cacheBatcher.ExecuteBatch();
174174
}
175175
}
176176

177-
// Query cacheable results must be cached untransformed: the put does not need to wait for
178-
// the ProcessResults.
179-
PutCacheableResults();
180-
181177
foreach (var query in _queries)
182178
{
183179
//TODO 6.0: Replace with query.ProcessResults();
@@ -186,6 +182,9 @@ protected void ExecuteBatched()
186182
else
187183
query.ProcessResults();
188184
}
185+
186+
cacheBatcher?.ExecuteBatch();
187+
PutCacheableResults();
189188
}
190189
catch (Exception sqle)
191190
{

src/NHibernate/Multi/QueryBatchItemBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ public void ProcessResults()
274274
{
275275
ThrowIfNotInitialized();
276276

277-
InitializeEntitiesAndCollections(_reader, _hydratedObjects);
277+
using (Session.SwitchCacheMode(_cacheMode))
278+
InitializeEntitiesAndCollections(_reader, _hydratedObjects);
279+
278280
for (var i = 0; i < _queryInfos.Count; i++)
279281
{
280282
var queryInfo = _queryInfos[i];

0 commit comments

Comments
 (0)