Skip to content

Commit 27a36e5

Browse files
committed
Handle cacheable queries
1 parent f17a147 commit 27a36e5

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

src/NHibernate/Async/Multi/QueryBatch.cs

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

141141
var rowCount = 0;
142+
CacheBatcher cacheBatcher = null;
142143
try
143144
{
144145
if (resultSetsCommand.HasQueries)
145146
{
146147
using (var reader = await (resultSetsCommand.GetReaderAsync(Timeout, cancellationToken)).ConfigureAwait(false))
147148
{
148-
var cacheBatcher = new CacheBatcher(Session);
149149
foreach (var query in _queries)
150150
{
151151
if (query.CachingInformation != null)
152152
{
153153
foreach (var cachingInfo in query.CachingInformation.Where(ci => ci.IsCacheable))
154154
{
155+
if(cacheBatcher == null)
156+
cacheBatcher = new CacheBatcher(Session);
155157
cachingInfo.SetCacheBatcher(cacheBatcher);
156158
}
157159
}
158160

159161
rowCount += await (query.ProcessResultsSetAsync(reader, cancellationToken)).ConfigureAwait(false);
160162
}
161-
await (cacheBatcher.ExecuteBatchAsync(cancellationToken)).ConfigureAwait(false);
162163
}
163164
}
164165

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-
169166
foreach (var query in _queries)
170167
{
171168
//TODO 6.0: Replace with query.ProcessResults();
@@ -174,6 +171,17 @@ protected async Task ExecuteBatchedAsync(CancellationToken cancellationToken)
174171
else
175172
query.ProcessResults();
176173
}
174+
175+
var executeBatchTask = cacheBatcher?.ExecuteBatchAsync(cancellationToken);
176+
177+
if (executeBatchTask != null)
178+
179+
{
180+
181+
await (executeBatchTask).ConfigureAwait(false);
182+
183+
}
184+
await (PutCacheableResultsAsync(cancellationToken)).ConfigureAwait(false);
177185
}
178186
catch (OperationCanceledException) { throw; }
179187
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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,33 +151,30 @@ protected void ExecuteBatched()
151151
}
152152

153153
var rowCount = 0;
154+
CacheBatcher cacheBatcher = null;
154155
try
155156
{
156157
if (resultSetsCommand.HasQueries)
157158
{
158159
using (var reader = resultSetsCommand.GetReader(Timeout))
159160
{
160-
var cacheBatcher = new CacheBatcher(Session);
161161
foreach (var query in _queries)
162162
{
163163
if (query.CachingInformation != null)
164164
{
165165
foreach (var cachingInfo in query.CachingInformation.Where(ci => ci.IsCacheable))
166166
{
167+
if(cacheBatcher == null)
168+
cacheBatcher = new CacheBatcher(Session);
167169
cachingInfo.SetCacheBatcher(cacheBatcher);
168170
}
169171
}
170172

171173
rowCount += query.ProcessResultsSet(reader);
172174
}
173-
cacheBatcher.ExecuteBatch();
174175
}
175176
}
176177

177-
// Query cacheable results must be cached untransformed: the put does not need to wait for
178-
// the ProcessResults.
179-
PutCacheableResults();
180-
181178
foreach (var query in _queries)
182179
{
183180
//TODO 6.0: Replace with query.ProcessResults();
@@ -186,6 +183,9 @@ protected void ExecuteBatched()
186183
else
187184
query.ProcessResults();
188185
}
186+
187+
cacheBatcher?.ExecuteBatch();
188+
PutCacheableResults();
189189
}
190190
catch (Exception sqle)
191191
{

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)