Created
February 21, 2022 19:29
-
-
Save bahusoid/958cd0e95a116350ef05b2ca9513d4ef to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using NHibernate.Cache; | |
namespace YourNamespace | |
{ | |
public class LegacyCacheLock : 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 CreateLock(); | |
} | |
public IDisposable WriteLock() | |
{ | |
return CreateLock(); | |
} | |
public Task<IDisposable> WriteLockAsync() | |
{ | |
return CreateAsyncLock(); | |
} | |
public Task<IDisposable> ReadLockAsync() | |
{ | |
return CreateAsyncLock(); | |
} | |
private IDisposable CreateLock() | |
{ | |
return new MonitorLock(this); | |
} | |
private Task<IDisposable> CreateAsyncLock() | |
{ | |
try | |
{ | |
return Task.FromResult<IDisposable>(CreateLock()); | |
} | |
catch (Exception ex) | |
{ | |
return Task.FromException<IDisposable>(ex); | |
} | |
} | |
} | |
public class LegacyCacheReadWriteLockFactory : ICacheReadWriteLockFactory | |
{ | |
public ICacheLock Create() | |
{ | |
return new LegacyCacheLock(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment