Removes usages of UpgradeableReadLock

This commit is contained in:
Shannon
2021-04-20 18:02:23 +10:00
parent bf7a3251d8
commit dc08997040
5 changed files with 43 additions and 43 deletions

View File

@@ -110,19 +110,34 @@ namespace Umbraco.Core.Cache
Lazy<object> result;
using (var lck = new UpgradeableReadLock(_locker))
try
{
_locker.EnterUpgradeableReadLock();
result = MemoryCache.Get(key) as Lazy<object>;
if (result == null || FastDictionaryAppCacheBase.GetSafeLazyValue(result, true) == null) // get non-created as NonCreatedValue & exceptions as null
{
result = FastDictionaryAppCacheBase.GetSafeLazy(factory);
var policy = GetPolicy(timeout, isSliding, removedCallback, dependentFiles);
lck.UpgradeToWriteLock();
//NOTE: This does an add or update
MemoryCache.Set(key, result, policy);
try
{
_locker.EnterWriteLock();
//NOTE: This does an add or update
MemoryCache.Set(key, result, policy);
}
finally
{
if (_locker.IsWriteLockHeld)
_locker.ExitWriteLock();
}
}
}
finally
{
if (_locker.IsUpgradeableReadLockHeld)
_locker.ExitUpgradeableReadLock();
}
//return result.Value;

View File

@@ -139,8 +139,10 @@ namespace Umbraco.Core.Cache
var value = result == null ? null : GetSafeLazyValue(result);
if (value != null) return value;
using (var lck = new UpgradeableReadLock(_locker))
try
{
_locker.EnterUpgradeableReadLock();
result = _cache.Get(key) as Lazy<object>; // null if key not found
// cannot create value within the lock, so if result.IsValueCreated is false, just
@@ -153,15 +155,28 @@ namespace Umbraco.Core.Cache
var absolute = isSliding ? System.Web.Caching.Cache.NoAbsoluteExpiration : (timeout == null ? System.Web.Caching.Cache.NoAbsoluteExpiration : DateTime.Now.Add(timeout.Value));
var sliding = isSliding == false ? System.Web.Caching.Cache.NoSlidingExpiration : (timeout ?? System.Web.Caching.Cache.NoSlidingExpiration);
lck.UpgradeToWriteLock();
try
{
_locker.EnterWriteLock();
// create a cache dependency if one is needed.
var dependency = dependentFiles != null && dependentFiles.Length > 0 ? new CacheDependency(dependentFiles) : null;
// create a cache dependency if one is needed.
var dependency = dependentFiles != null && dependentFiles.Length > 0 ? new CacheDependency(dependentFiles) : null;
//NOTE: 'Insert' on System.Web.Caching.Cache actually does an add or update!
_cache.Insert(key, result, dependency, absolute, sliding, priority, removedCallback);
//NOTE: 'Insert' on System.Web.Caching.Cache actually does an add or update!
_cache.Insert(key, result, dependency, absolute, sliding, priority, removedCallback);
}
finally
{
if (_locker.IsWriteLockHeld)
_locker.ExitWriteLock();
}
}
}
finally
{
if (_locker.IsUpgradeableReadLockHeld)
_locker.ExitUpgradeableReadLock();
}
// using GetSafeLazy and GetSafeLazyValue ensures that we don't cache
// exceptions (but try again and again) and silently eat them - however at

View File

@@ -1,20 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Umbraco.Core
{
/// <summary>
/// Provides a convenience methodology for implementing locked access to resources.
/// </summary>
/// <remarks>
/// <para>Intended as an infrastructure class.</para>
/// <para>This is a very inefficient way to lock as it allocates one object each time we lock,
/// so it's OK to use this class for things that happen once, where it is convenient, but not
/// for performance-critical code!</para>
/// </remarks>
[Obsolete("Use ReaderWriterLockSlim directly. This will be removed in future versions.")]
public class ReadLock : IDisposable
{
private readonly ReaderWriterLockSlim _rwLock;

View File

@@ -1,17 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Umbraco.Core
{
/// <summary>
/// Provides a convenience methodology for implementing locked access to resources.
/// </summary>
/// <remarks>
/// Intended as an infrastructure class.
/// </remarks>
[Obsolete("Use ReaderWriterLockSlim directly. This will be removed in future versions.")]
public class UpgradeableReadLock : IDisposable
{
private readonly ReaderWriterLockSlim _rwLock;

View File

@@ -1,20 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Umbraco.Core
{
/// <summary>
/// Provides a convenience methodology for implementing locked access to resources.
/// </summary>
/// <remarks>
/// <para>Intended as an infrastructure class.</para>
/// <para>This is a very inefficient way to lock as it allocates one object each time we lock,
/// so it's OK to use this class for things that happen once, where it is convenient, but not
/// for performance-critical code!</para>
/// </remarks>
[Obsolete("Use ReaderWriterLockSlim directly. This will be removed in future versions.")]
public class WriteLock : IDisposable
{
private readonly ReaderWriterLockSlim _rwLock;