Skip to content

Avoid NRE in cache strategies #2554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: 5.3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions src/NHibernate/Async/Cache/NonstrictReadWriteCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//------------------------------------------------------------------------------


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -31,7 +32,7 @@ public async Task<object> GetAsync(CacheKey key, long txTimestamp, CancellationT
log.Debug("Cache lookup: {0}", key);
}

var result = await (Cache.GetAsync(key, cancellationToken)).ConfigureAwait(false);
var result = await (InternalCache.GetAsync(key, cancellationToken)).ConfigureAwait(false);
if (log.IsDebugEnabled())
{
log.Debug(result != null ? "Cache hit: {0}" : "Cache miss: {0}", key);
Expand All @@ -48,7 +49,7 @@ public async Task<object[]> GetManyAsync(CacheKey[] keys, long timestamp, Cancel
log.Debug("Cache lookup: {0}", string.Join(",", keys.AsEnumerable()));
}

var results = await (_cache.GetManyAsync(keys, cancellationToken)).ConfigureAwait(false);
var results = await (InternalCache.GetManyAsync(keys, cancellationToken)).ConfigureAwait(false);
if (log.IsDebugEnabled())
{
log.Debug("Cache hit: {0}", string.Join(",", keys.Where((k, i) => results != null)));
Expand Down Expand Up @@ -83,10 +84,12 @@ public async Task<bool[]> PutManyAsync(
checkKeyIndexes.Add(i);
}
}

var cache = InternalCache;
var skipKeyIndexes = new HashSet<int>();
if (checkKeys.Any())
{
var objects = await (_cache.GetManyAsync(checkKeys.ToArray(), cancellationToken)).ConfigureAwait(false);
var objects = await (cache.GetManyAsync(checkKeys.ToArray(), cancellationToken)).ConfigureAwait(false);
for (var i = 0; i < objects.Length; i++)
{
if (objects[i] != null)
Expand Down Expand Up @@ -118,7 +121,7 @@ public async Task<bool[]> PutManyAsync(
putValues[j++] = values[i];
result[i] = true;
}
await (_cache.PutManyAsync(putKeys, putValues, cancellationToken)).ConfigureAwait(false);
await (cache.PutManyAsync(putKeys, putValues, cancellationToken)).ConfigureAwait(false);
return result;
}

Expand All @@ -135,7 +138,8 @@ public async Task<bool> PutAsync(CacheKey key, object value, long txTimestamp, o
return false;
}

if (minimalPut && await (Cache.GetAsync(key, cancellationToken)).ConfigureAwait(false) != null)
var cache = InternalCache;
if (minimalPut && await (cache.GetAsync(key, cancellationToken)).ConfigureAwait(false) != null)
{
if (log.IsDebugEnabled())
{
Expand All @@ -147,7 +151,7 @@ public async Task<bool> PutAsync(CacheKey key, object value, long txTimestamp, o
{
log.Debug("Caching: {0}", key);
}
await (Cache.PutAsync(key, value, cancellationToken)).ConfigureAwait(false);
await (cache.PutAsync(key, value, cancellationToken)).ConfigureAwait(false);
return true;
}

Expand All @@ -164,7 +168,7 @@ public Task<ISoftLock> LockAsync(CacheKey key, object version, CancellationToken
{
return Task.FromResult<ISoftLock>(Lock(key, version));
}
catch (System.Exception ex)
catch (Exception ex)
{
return Task.FromException<ISoftLock>(ex);
}
Expand All @@ -182,9 +186,9 @@ public Task RemoveAsync(CacheKey key, CancellationToken cancellationToken)
{
log.Debug("Removing: {0}", key);
}
return Cache.RemoveAsync(key, cancellationToken);
return InternalCache.RemoveAsync(key, cancellationToken);
}
catch (System.Exception ex)
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
Expand All @@ -202,9 +206,9 @@ public Task ClearAsync(CancellationToken cancellationToken)
{
log.Debug("Clearing");
}
return Cache.ClearAsync(cancellationToken);
return InternalCache.ClearAsync(cancellationToken);
}
catch (System.Exception ex)
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
Expand All @@ -225,9 +229,9 @@ public Task EvictAsync(CacheKey key, CancellationToken cancellationToken)
{
log.Debug("Invalidating: {0}", key);
}
return Cache.RemoveAsync(key, cancellationToken);
return InternalCache.RemoveAsync(key, cancellationToken);
}
catch (System.Exception ex)
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
Expand Down Expand Up @@ -259,9 +263,9 @@ public Task ReleaseAsync(CacheKey key, ISoftLock @lock, CancellationToken cancel
log.Debug("Invalidating (again): {0}", key);
}

return Cache.RemoveAsync(key, cancellationToken);
return InternalCache.RemoveAsync(key, cancellationToken);
}
catch (System.Exception ex)
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
Expand Down Expand Up @@ -290,7 +294,7 @@ public Task<bool> AfterInsertAsync(CacheKey key, object value, object version, C
{
return Task.FromResult<bool>(AfterInsert(key, value, version));
}
catch (System.Exception ex)
catch (Exception ex)
{
return Task.FromException<bool>(ex);
}
Expand Down
19 changes: 11 additions & 8 deletions src/NHibernate/Async/Cache/ReadOnlyCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public partial class ReadOnlyCache : IBatchableCacheConcurrencyStrategy
public async Task<object> GetAsync(CacheKey key, long timestamp, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var result = await (Cache.GetAsync(key, cancellationToken)).ConfigureAwait(false);
var result = await (InternalCache.GetAsync(key, cancellationToken)).ConfigureAwait(false);
if (log.IsDebugEnabled())
{
log.Debug(result != null ? "Cache hit: {0}" : "Cache miss: {0}", key);
Expand All @@ -41,7 +41,7 @@ public async Task<object[]> GetManyAsync(CacheKey[] keys, long timestamp, Cancel
log.Debug("Cache lookup: {0}", string.Join(",", keys.AsEnumerable()));
}

var results = await (_cache.GetManyAsync(keys, cancellationToken)).ConfigureAwait(false);
var results = await (InternalCache.GetManyAsync(keys, cancellationToken)).ConfigureAwait(false);
if (log.IsDebugEnabled())
{
log.Debug("Cache hit: {0}", string.Join(",", keys.Where((k, i) => results[i] != null)));
Expand Down Expand Up @@ -92,10 +92,12 @@ public async Task<bool[]> PutManyAsync(
checkKeyIndexes.Add(i);
}
}

var cache = InternalCache;
var skipKeyIndexes = new HashSet<int>();
if (checkKeys.Any())
{
var objects = await (_cache.GetManyAsync(checkKeys.ToArray(), cancellationToken)).ConfigureAwait(false);
var objects = await (cache.GetManyAsync(checkKeys.ToArray(), cancellationToken)).ConfigureAwait(false);
for (var i = 0; i < objects.Length; i++)
{
if (objects[i] != null)
Expand Down Expand Up @@ -127,7 +129,7 @@ public async Task<bool[]> PutManyAsync(
putValues[j++] = values[i];
result[i] = true;
}
await (_cache.PutManyAsync(putKeys, putValues, cancellationToken)).ConfigureAwait(false);
await (cache.PutManyAsync(putKeys, putValues, cancellationToken)).ConfigureAwait(false);
return result;
}

Expand All @@ -141,7 +143,8 @@ public async Task<bool> PutAsync(CacheKey key, object value, long timestamp, obj
return false;
}

if (minimalPut && await (Cache.GetAsync(key, cancellationToken)).ConfigureAwait(false) != null)
var cache = InternalCache;
if (minimalPut && await (cache.GetAsync(key, cancellationToken)).ConfigureAwait(false) != null)
{
if (log.IsDebugEnabled())
{
Expand All @@ -153,7 +156,7 @@ public async Task<bool> PutAsync(CacheKey key, object value, long timestamp, obj
{
log.Debug("Caching: {0}", key);
}
await (Cache.PutAsync(key, value, cancellationToken)).ConfigureAwait(false);
await (cache.PutAsync(key, value, cancellationToken)).ConfigureAwait(false);
return true;
}

Expand Down Expand Up @@ -183,7 +186,7 @@ public Task ClearAsync(CancellationToken cancellationToken)
{
return Task.FromCanceled<object>(cancellationToken);
}
return Cache.ClearAsync(cancellationToken);
return InternalCache.ClearAsync(cancellationToken);
}

public Task RemoveAsync(CacheKey key, CancellationToken cancellationToken)
Expand All @@ -192,7 +195,7 @@ public Task RemoveAsync(CacheKey key, CancellationToken cancellationToken)
{
return Task.FromCanceled<object>(cancellationToken);
}
return Cache.RemoveAsync(key, cancellationToken);
return InternalCache.RemoveAsync(key, cancellationToken);
}

/// <summary>
Expand Down
Loading