Skip to content

Replace ICache interface by a CacheBase class #1777

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

Merged
merged 2 commits into from
Oct 31, 2018
Merged
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
2 changes: 0 additions & 2 deletions src/AsyncGenerator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,6 @@ methodRules:
name: Lock
- containingType: NHibernate.Cache.ICache
name: Unlock
- containingType: NHibernate.Cache.IBatchableReadOnlyCache
- containingType: NHibernate.Cache.IBatchableCache
name: Cache
- filters:
- containingType: NHibernate.Action.IAfterTransactionCompletionProcess
Expand Down
28 changes: 9 additions & 19 deletions src/NHibernate.Test/Async/CacheTest/BatchableCacheFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -537,17 +537,12 @@ public async Task MultiplePutReadWriteItemTestAsync()
public async Task UpdateTimestampsCacheTestAsync()
{
var timestamp = Sfi.UpdateTimestampsCache;
var fieldReadonly = typeof(UpdateTimestampsCache).GetField(
"_batchReadOnlyUpdateTimestamps",
BindingFlags.NonPublic | BindingFlags.Instance);
Assert.That(fieldReadonly, Is.Not.Null, "Unable to find _batchReadOnlyUpdateTimestamps field");
Assert.That(fieldReadonly.GetValue(timestamp), Is.Not.Null, "_batchReadOnlyUpdateTimestamps is null");
var field = typeof(UpdateTimestampsCache).GetField(
"_batchUpdateTimestamps",
"_updateTimestamps",
BindingFlags.NonPublic | BindingFlags.Instance);
Assert.That(field, Is.Not.Null, "Unable to find _batchUpdateTimestamps field");
Assert.That(field, Is.Not.Null, "Unable to find _updateTimestamps field");
var cache = (BatchableCache) field.GetValue(timestamp);
Assert.That(cache, Is.Not.Null, "_batchUpdateTimestamps is null");
Assert.That(cache, Is.Not.Null, "Cache field");

await (cache.ClearAsync(CancellationToken.None));
cache.ClearStatistics();
Expand Down Expand Up @@ -606,25 +601,20 @@ public async Task QueryCacheTestAsync()
Assert.Ignore($"{Sfi.ConnectionProvider.Driver} does not support multiple queries");

var queryCache = Sfi.GetQueryCache(null);
var readonlyField = typeof(StandardQueryCache).GetField(
"_batchableReadOnlyCache",
BindingFlags.NonPublic | BindingFlags.Instance);
Assert.That(readonlyField, Is.Not.Null, "Unable to find _batchableReadOnlyCache field");
Assert.That(readonlyField.GetValue(queryCache), Is.Not.Null, "_batchableReadOnlyCache is null");
var field = typeof(StandardQueryCache).GetField(
"_batchableCache",
"_cache",
BindingFlags.NonPublic | BindingFlags.Instance);
Assert.That(field, Is.Not.Null, "Unable to find _batchableCache field");
Assert.That(field, Is.Not.Null, "Unable to find _cache field");
var cache = (BatchableCache) field.GetValue(queryCache);
Assert.That(cache, Is.Not.Null, "_batchableCache is null");
Assert.That(cache, Is.Not.Null, "_cache is null");

var timestamp = Sfi.UpdateTimestampsCache;
var tsField = typeof(UpdateTimestampsCache).GetField(
"_batchUpdateTimestamps",
"_updateTimestamps",
BindingFlags.NonPublic | BindingFlags.Instance);
Assert.That(tsField, Is.Not.Null, "Unable to find _batchUpdateTimestamps field");
Assert.That(tsField, Is.Not.Null, "Unable to find _updateTimestamps field");
var tsCache = (BatchableCache) tsField.GetValue(timestamp);
Assert.That(tsCache, Is.Not.Null, "_batchUpdateTimestamps is null");
Assert.That(tsCache, Is.Not.Null, "_updateTimestamps is null");

await (cache.ClearAsync(CancellationToken.None));
cache.ClearStatistics();
Expand Down
8 changes: 4 additions & 4 deletions src/NHibernate.Test/Async/CacheTest/CacheFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private CacheKey CreateCacheKey(string text)

public async Task DoTestCacheAsync(ICacheProvider cacheProvider, CancellationToken cancellationToken = default(CancellationToken))
{
ICache cache = cacheProvider.BuildCache(typeof(String).FullName, new Dictionary<string, string>());
var cache = cacheProvider.BuildCache(typeof(String).FullName, new Dictionary<string, string>());

long longBefore = Timestamper.Next();

Expand Down Expand Up @@ -141,7 +141,7 @@ private CacheKey CreateCacheKey(string text)
Assert.AreEqual("baz", await (ccs.GetAsync(fooKey, longLongAfter, cancellationToken)));
}

private async Task DoTestMinValueTimestampOnStrategyAsync(ICache cache, ICacheConcurrencyStrategy strategy, CancellationToken cancellationToken = default(CancellationToken))
private async Task DoTestMinValueTimestampOnStrategyAsync(CacheBase cache, ICacheConcurrencyStrategy strategy, CancellationToken cancellationToken = default(CancellationToken))
{
CacheKey key = CreateCacheKey("key");
strategy.Cache = cache;
Expand All @@ -154,7 +154,7 @@ private CacheKey CreateCacheKey(string text)
[Test]
public async Task MinValueTimestampAsync()
{
ICache cache = new HashtableCacheProvider().BuildCache("region", new Dictionary<string, string>());
var cache = new HashtableCacheProvider().BuildCache("region", new Dictionary<string, string>());
ICacheConcurrencyStrategy strategy = new ReadWriteCache();
strategy.Cache = cache;

Expand All @@ -163,4 +163,4 @@ public async Task MinValueTimestampAsync()
await (DoTestMinValueTimestampOnStrategyAsync(cache, new ReadOnlyCache()));
}
}
}
}
94 changes: 40 additions & 54 deletions src/NHibernate.Test/Async/CacheTest/Caches/BatchableCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,36 @@
//------------------------------------------------------------------------------


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NHibernate.Cache;

namespace NHibernate.Test.CacheTest.Caches
{
public partial class BatchableCache : ICache, IBatchableCache
using System.Threading.Tasks;
using System.Threading;
public partial class BatchableCache : CacheBase
{

public Task PutManyAsync(object[] keys, object[] values, CancellationToken cancellationToken)
public override Task<object[]> GetManyAsync(object[] keys, CancellationToken cancellationToken)
{
try
{
GetMultipleCalls.Add(keys);
var result = new object[keys.Length];
for (var i = 0; i < keys.Length; i++)
{
result[i] = _hashtable[keys[i]];
}
return Task.FromResult<object[]>(result);
}
catch (System.Exception ex)
{
return Task.FromException<object[]>(ex);
}
}

public override Task PutManyAsync(object[] keys, object[] values, CancellationToken cancellationToken)
{
try
{
Expand All @@ -33,130 +48,101 @@ public Task PutManyAsync(object[] keys, object[] values, CancellationToken cance
}
return Task.CompletedTask;
}
catch (Exception ex)
catch (System.Exception ex)
{
return Task.FromException<object>(ex);
}
}

public Task<object> LockManyAsync(object[] keys, CancellationToken cancellationToken)
public override Task<object> LockManyAsync(object[] keys, CancellationToken cancellationToken)
{
try
{
LockMultipleCalls.Add(keys);
return Task.FromResult<object>(null);
}
catch (Exception ex)
catch (System.Exception ex)
{
return Task.FromException<object>(ex);
}
}

public Task UnlockManyAsync(object[] keys, object lockValue, CancellationToken cancellationToken)
public override Task UnlockManyAsync(object[] keys, object lockValue, CancellationToken cancellationToken)
{
try
{
UnlockMultipleCalls.Add(keys);
return Task.CompletedTask;
}
catch (Exception ex)
catch (System.Exception ex)
{
return Task.FromException<object>(ex);
}
}

#region ICache Members

/// <summary></summary>
public Task<object> GetAsync(object key, CancellationToken cancellationToken)
public override Task<object> GetAsync(object key, CancellationToken cancellationToken)
{
try
{
GetCalls.Add(key);
return Task.FromResult<object>(_hashtable[key]);
}
catch (Exception ex)
catch (System.Exception ex)
{
return Task.FromException<object>(ex);
}
}

public Task<object[]> GetManyAsync(object[] keys, CancellationToken cancellationToken)
{
try
{
GetMultipleCalls.Add(keys);
var result = new object[keys.Length];
for (var i = 0; i < keys.Length; i++)
{
result[i] = _hashtable[keys[i]];
}
return Task.FromResult<object[]>(result);
}
catch (Exception ex)
{
return Task.FromException<object[]>(ex);
}
}

/// <summary></summary>
public Task PutAsync(object key, object value, CancellationToken cancellationToken)
public override Task PutAsync(object key, object value, CancellationToken cancellationToken)
{
try
{
PutCalls.Add(key);
_hashtable[key] = value;
return Task.CompletedTask;
}
catch (Exception ex)
catch (System.Exception ex)
{
return Task.FromException<object>(ex);
}
}

/// <summary></summary>
public Task RemoveAsync(object key, CancellationToken cancellationToken)
public override Task RemoveAsync(object key, CancellationToken cancellationToken)
{
try
{
_hashtable.Remove(key);
return Task.CompletedTask;
}
catch (Exception ex)
catch (System.Exception ex)
{
return Task.FromException<object>(ex);
}
}

/// <summary></summary>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
public Task ClearAsync(CancellationToken cancellationToken)
public override Task ClearAsync(CancellationToken cancellationToken)
{
try
{
_hashtable.Clear();
return Task.CompletedTask;
}
catch (Exception ex)
catch (System.Exception ex)
{
return Task.FromException<object>(ex);
}
}

/// <summary></summary>
public Task LockAsync(object key, CancellationToken cancellationToken)
public override Task<object> LockAsync(object key, CancellationToken cancellationToken)
{
return Task.CompletedTask;
// local cache, so we use synchronization
// local cache, no need to actually lock.
return Task.FromResult<object>(null);
}

/// <summary></summary>
public Task UnlockAsync(object key, CancellationToken cancellationToken)
public override Task UnlockAsync(object key, object lockValue, CancellationToken cancellationToken)
{
return Task.CompletedTask;
// local cache, so we use synchronization
// local cache, no need to actually lock.
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using NHibernate.Cache;
#if !NETFX
using NHibernate.Util;
#endif

namespace NHibernate.Test.NHSpecificTest.NH2898
{
using System.Threading.Tasks;
using System.Threading;
public partial class BinaryFormatterCache : ICache
public partial class BinaryFormatterCache : CacheBase
{

public Task<object> GetAsync(object key, CancellationToken cancellationToken)
public override Task<object> GetAsync(object key, CancellationToken cancellationToken)
{
try
{
Expand All @@ -46,7 +48,7 @@ public Task<object> GetAsync(object key, CancellationToken cancellationToken)
}
}

public Task PutAsync(object key, object value, CancellationToken cancellationToken)
public override Task PutAsync(object key, object value, CancellationToken cancellationToken)
{
try
{
Expand All @@ -69,7 +71,7 @@ public Task PutAsync(object key, object value, CancellationToken cancellationTok
}
}

public Task RemoveAsync(object key, CancellationToken cancellationToken)
public override Task RemoveAsync(object key, CancellationToken cancellationToken)
{
try
{
Expand All @@ -82,7 +84,7 @@ public Task RemoveAsync(object key, CancellationToken cancellationToken)
}
}

public Task ClearAsync(CancellationToken cancellationToken)
public override Task ClearAsync(CancellationToken cancellationToken)
{
try
{
Expand All @@ -95,16 +97,16 @@ public Task ClearAsync(CancellationToken cancellationToken)
}
}

public Task LockAsync(object key, CancellationToken cancellationToken)
public override Task<object> LockAsync(object key, CancellationToken cancellationToken)
{
return Task.CompletedTask;
// local cache, so we use synchronization
return Task.FromResult<object>(null);
}

public Task UnlockAsync(object key, CancellationToken cancellationToken)
public override Task UnlockAsync(object key, object lockValue, CancellationToken cancellationToken)
{
return Task.CompletedTask;
// local cache, so we use synchronization
}
}
}
}
Loading