diff --git a/src/NHibernate/Async/Cache/StandardQueryCache.cs b/src/NHibernate/Async/Cache/StandardQueryCache.cs index 8d656f86160..ab7d4f4547e 100644 --- a/src/NHibernate/Async/Cache/StandardQueryCache.cs +++ b/src/NHibernate/Async/Cache/StandardQueryCache.cs @@ -470,6 +470,9 @@ protected virtual Task IsUpToDateAsync(ISet spaces, long timestamp { return Task.FromCanceled(cancellationToken); } + if (spaces.Count == 0) + return Task.FromResult(true); + return _updateTimestampsCache.IsUpToDateAsync(spaces, timestamp, cancellationToken); } } diff --git a/src/NHibernate/Async/Cache/UpdateTimestampsCache.cs b/src/NHibernate/Async/Cache/UpdateTimestampsCache.cs index e5ba6ce0e45..f97f25401be 100644 --- a/src/NHibernate/Async/Cache/UpdateTimestampsCache.cs +++ b/src/NHibernate/Async/Cache/UpdateTimestampsCache.cs @@ -116,13 +116,9 @@ private Task SetSpacesTimestampAsync(IReadOnlyCollection spaces, long ts if (spaces.Count == 0) return Task.CompletedTask; - var timestamps = new object[spaces.Count]; - for (var i = 0; i < timestamps.Length; i++) - { - timestamps[i] = ts; - } - - return _updateTimestamps.PutManyAsync(spaces.ToArray(), timestamps, cancellationToken); + return _updateTimestamps.PutManyAsync( + spaces.ToArray(), + ArrayHelper.Fill(ts, spaces.Count), cancellationToken); } catch (Exception ex) { @@ -139,13 +135,7 @@ public virtual async Task IsUpToDateAsync(ISet spaces, long timest if (spaces.Count == 0) return true; - var keys = new object[spaces.Count]; - var index = 0; - foreach (var space in spaces) - { - keys[index++] = space; - } - var lastUpdates = await (_updateTimestamps.GetManyAsync(keys, cancellationToken)).ConfigureAwait(false); + var lastUpdates = await (_updateTimestamps.GetManyAsync(spaces.ToArray(), cancellationToken)).ConfigureAwait(false); return lastUpdates.All(lastUpdate => !IsOutdated(lastUpdate as long?, timestamp)); } } @@ -156,7 +146,9 @@ public virtual async Task AreUpToDateAsync(ISet[] spaces, long[] cancellationToken.ThrowIfCancellationRequested(); using (await _areUpToDate.LockAsync()) { - var results = new bool[spaces.Length]; + if (spaces.Length == 0) + return Array.Empty(); + var allSpaces = new HashSet(); foreach (var sp in spaces) { @@ -164,28 +156,17 @@ public virtual async Task AreUpToDateAsync(ISet[] spaces, long[] } if (allSpaces.Count == 0) - { - for (var i = 0; i < spaces.Length; i++) - { - results[i] = true; - } + return ArrayHelper.Fill(true, spaces.Length); - return results; - } + var keys = allSpaces.ToArray(); - var keys = new object[allSpaces.Count]; var index = 0; - foreach (var space in allSpaces) - { - keys[index++] = space; - } - - index = 0; var lastUpdatesBySpace = (await (_updateTimestamps .GetManyAsync(keys, cancellationToken)).ConfigureAwait(false)) .ToDictionary(u => keys[index++], u => u as long?); + var results = new bool[spaces.Length]; for (var i = 0; i < spaces.Length; i++) { var timestamp = timestamps[i]; diff --git a/src/NHibernate/Async/Engine/ActionQueue.cs b/src/NHibernate/Async/Engine/ActionQueue.cs index 521fafc16c5..3a0ab25b1f5 100644 --- a/src/NHibernate/Async/Engine/ActionQueue.cs +++ b/src/NHibernate/Async/Engine/ActionQueue.cs @@ -45,7 +45,7 @@ private Task PreInvalidateCachesAsync(CancellationToken cancellationToken) { return Task.FromCanceled(cancellationToken); } - if (session.Factory.Settings.IsQueryCacheEnabled) + if (session.Factory.Settings.IsQueryCacheEnabled && executedSpaces.Count > 0) { return session.Factory.UpdateTimestampsCache.PreInvalidateAsync(executedSpaces, cancellationToken); } @@ -165,7 +165,7 @@ public async Task AfterTransactionCompletionAsync(bool success, CancellationToke private async Task InvalidateCachesAsync(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); - if (session.Factory.Settings.IsQueryCacheEnabled) + if (session.Factory.Settings.IsQueryCacheEnabled && executedSpaces.Count > 0) { await (session.Factory.UpdateTimestampsCache.InvalidateAsync(executedSpaces, cancellationToken)).ConfigureAwait(false); } diff --git a/src/NHibernate/Cache/StandardQueryCache.cs b/src/NHibernate/Cache/StandardQueryCache.cs index 5ccdfdb7a0a..8cda99f3815 100644 --- a/src/NHibernate/Cache/StandardQueryCache.cs +++ b/src/NHibernate/Cache/StandardQueryCache.cs @@ -551,6 +551,9 @@ private static ICacheAssembler[] GuessTypes(IList cacheable) protected virtual bool IsUpToDate(ISet spaces, long timestamp) { + if (spaces.Count == 0) + return true; + return _updateTimestampsCache.IsUpToDate(spaces, timestamp); } } diff --git a/src/NHibernate/Cache/UpdateTimestampsCache.cs b/src/NHibernate/Cache/UpdateTimestampsCache.cs index 21f37cf8d25..40369e4ac97 100644 --- a/src/NHibernate/Cache/UpdateTimestampsCache.cs +++ b/src/NHibernate/Cache/UpdateTimestampsCache.cs @@ -88,13 +88,9 @@ private void SetSpacesTimestamp(IReadOnlyCollection spaces, long ts) if (spaces.Count == 0) return; - var timestamps = new object[spaces.Count]; - for (var i = 0; i < timestamps.Length; i++) - { - timestamps[i] = ts; - } - - _updateTimestamps.PutMany(spaces.ToArray(), timestamps); + _updateTimestamps.PutMany( + spaces.ToArray(), + ArrayHelper.Fill(ts, spaces.Count)); } [MethodImpl(MethodImplOptions.Synchronized)] @@ -103,20 +99,16 @@ public virtual bool IsUpToDate(ISet spaces, long timestamp /* H2.1 has L if (spaces.Count == 0) return true; - var keys = new object[spaces.Count]; - var index = 0; - foreach (var space in spaces) - { - keys[index++] = space; - } - var lastUpdates = _updateTimestamps.GetMany(keys); + var lastUpdates = _updateTimestamps.GetMany(spaces.ToArray()); return lastUpdates.All(lastUpdate => !IsOutdated(lastUpdate as long?, timestamp)); } [MethodImpl(MethodImplOptions.Synchronized)] public virtual bool[] AreUpToDate(ISet[] spaces, long[] timestamps) { - var results = new bool[spaces.Length]; + if (spaces.Length == 0) + return Array.Empty(); + var allSpaces = new HashSet(); foreach (var sp in spaces) { @@ -124,28 +116,17 @@ public virtual bool[] AreUpToDate(ISet[] spaces, long[] timestamps) } if (allSpaces.Count == 0) - { - for (var i = 0; i < spaces.Length; i++) - { - results[i] = true; - } + return ArrayHelper.Fill(true, spaces.Length); - return results; - } + var keys = allSpaces.ToArray(); - var keys = new object[allSpaces.Count]; var index = 0; - foreach (var space in allSpaces) - { - keys[index++] = space; - } - - index = 0; var lastUpdatesBySpace = _updateTimestamps .GetMany(keys) .ToDictionary(u => keys[index++], u => u as long?); + var results = new bool[spaces.Length]; for (var i = 0; i < spaces.Length; i++) { var timestamp = timestamps[i]; @@ -163,7 +144,7 @@ public virtual void Destroy() // not the responsibility of this class. } - private bool IsOutdated(long? lastUpdate, long timestamp) + private static bool IsOutdated(long? lastUpdate, long timestamp) { if (!lastUpdate.HasValue) { diff --git a/src/NHibernate/Engine/ActionQueue.cs b/src/NHibernate/Engine/ActionQueue.cs index e717adfb426..36a76591edd 100644 --- a/src/NHibernate/Engine/ActionQueue.cs +++ b/src/NHibernate/Engine/ActionQueue.cs @@ -172,7 +172,7 @@ private void ExecuteActions(List list) where T: IExecutable private void PreInvalidateCaches() { - if (session.Factory.Settings.IsQueryCacheEnabled) + if (session.Factory.Settings.IsQueryCacheEnabled && executedSpaces.Count > 0) { session.Factory.UpdateTimestampsCache.PreInvalidate(executedSpaces); } @@ -294,7 +294,7 @@ public void AfterTransactionCompletion(bool success) private void InvalidateCaches() { - if (session.Factory.Settings.IsQueryCacheEnabled) + if (session.Factory.Settings.IsQueryCacheEnabled && executedSpaces.Count > 0) { session.Factory.UpdateTimestampsCache.Invalidate(executedSpaces); }