-
Notifications
You must be signed in to change notification settings - Fork 933
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add monitor based sync only locker for ReadWrite cache (#2944)
- Loading branch information
Showing
16 changed files
with
299 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using NHibernate.Cache; | ||
using NHibernate.Cfg; | ||
using NUnit.Framework; | ||
using Environment = NHibernate.Cfg.Environment; | ||
|
||
namespace NHibernate.Test.CacheTest | ||
{ | ||
[TestFixture] | ||
public class SyncOnlyCacheFixture : CacheFixture | ||
{ | ||
protected override void Configure(Configuration cfg) | ||
{ | ||
base.Configure(cfg); | ||
cfg.SetProperty(Environment.CacheReadWriteLockFactory, "sync"); | ||
} | ||
|
||
[Test] | ||
public void AsyncOperationsThrow() | ||
{ | ||
var cache = new HashtableCacheProvider().BuildCache("region", new Dictionary<string, string>()); | ||
var strategy = CreateCache(cache); | ||
CacheKey key = CreateCacheKey("key"); | ||
var stamp = Timestamper.Next(); | ||
Assert.ThrowsAsync<InvalidOperationException>( | ||
() => | ||
strategy.PutAsync(key, "value", stamp, 0, null, false, default(CancellationToken))); | ||
Assert.ThrowsAsync<InvalidOperationException>(() => strategy.GetAsync(key, stamp, default(CancellationToken))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using NHibernate.Util; | ||
|
||
namespace NHibernate.Cache | ||
{ | ||
/// <summary> | ||
/// Implementors provide a locking mechanism for the cache. | ||
/// </summary> | ||
public interface ICacheLock : IDisposable | ||
{ | ||
/// <summary> | ||
/// Acquire synchronously a read lock. | ||
/// </summary> | ||
/// <returns>A read lock.</returns> | ||
IDisposable ReadLock(); | ||
|
||
/// <summary> | ||
/// Acquire synchronously a write lock. | ||
/// </summary> | ||
/// <returns>A write lock.</returns> | ||
IDisposable WriteLock(); | ||
|
||
/// <summary> | ||
/// Acquire asynchronously a read lock. | ||
/// </summary> | ||
/// <returns>A read lock.</returns> | ||
Task<IDisposable> ReadLockAsync(); | ||
|
||
/// <summary> | ||
/// Acquire asynchronously a write lock. | ||
/// </summary> | ||
/// <returns>A write lock.</returns> | ||
Task<IDisposable> WriteLockAsync(); | ||
} | ||
|
||
/// <summary> | ||
/// Define a factory for cache locks. | ||
/// </summary> | ||
public interface ICacheReadWriteLockFactory | ||
{ | ||
/// <summary> | ||
/// Create a cache lock provider. | ||
/// </summary> | ||
ICacheLock Create(); | ||
} | ||
|
||
internal class AsyncCacheReadWriteLockFactory : ICacheReadWriteLockFactory | ||
{ | ||
public ICacheLock Create() | ||
{ | ||
return new AsyncReaderWriterLock(); | ||
} | ||
} | ||
|
||
internal class SyncCacheReadWriteLockFactory : ICacheReadWriteLockFactory | ||
{ | ||
public ICacheLock Create() | ||
{ | ||
return new SyncCacheLock(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace NHibernate.Cache | ||
{ | ||
class SyncCacheLock : ICacheLock | ||
{ | ||
class MonitorLock : IDisposable | ||
{ | ||
private readonly object _lockObj; | ||
|
||
public MonitorLock(object lockObj) | ||
{ | ||
Monitor.Enter(lockObj); | ||
_lockObj = lockObj; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Monitor.Exit(_lockObj); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
|
||
public IDisposable ReadLock() | ||
{ | ||
return new MonitorLock(this); | ||
} | ||
|
||
public IDisposable WriteLock() | ||
{ | ||
return new MonitorLock(this); | ||
} | ||
|
||
public Task<IDisposable> ReadLockAsync() | ||
{ | ||
throw AsyncNotSupporteException(); | ||
} | ||
|
||
public Task<IDisposable> WriteLockAsync() | ||
{ | ||
throw AsyncNotSupporteException(); | ||
} | ||
|
||
private static InvalidOperationException AsyncNotSupporteException() | ||
{ | ||
return new InvalidOperationException("This locker supports only sync operations. Change 'cache.read_write_lock_factory' setting to `async` to support async operations."); | ||
} | ||
} | ||
} |
Oops, something went wrong.