Browse Source

Fix naming inconsistencies in the core caches and remove usages of Lazy

pull/717/head
Kévin Chalet 8 years ago
committed by GitHub
parent
commit
d183afeeff
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      src/OpenIddict.Core/Caches/OpenIddictApplicationCache.cs
  2. 36
      src/OpenIddict.Core/Caches/OpenIddictAuthorizationCache.cs
  3. 20
      src/OpenIddict.Core/Caches/OpenIddictScopeCache.cs
  4. 20
      src/OpenIddict.Core/Caches/OpenIddictTokenCache.cs

20
src/OpenIddict.Core/Caches/OpenIddictApplicationCache.cs

@ -24,7 +24,7 @@ namespace OpenIddict.Core
public class OpenIddictApplicationCache<TApplication> : IOpenIddictApplicationCache<TApplication>, IDisposable where TApplication : class public class OpenIddictApplicationCache<TApplication> : IOpenIddictApplicationCache<TApplication>, IDisposable where TApplication : class
{ {
private readonly MemoryCache _cache; private readonly MemoryCache _cache;
private readonly ConcurrentDictionary<string, Lazy<CancellationTokenSource>> _signals; private readonly ConcurrentDictionary<string, CancellationTokenSource> _signals;
private readonly IOpenIddictApplicationStore<TApplication> _store; private readonly IOpenIddictApplicationStore<TApplication> _store;
public OpenIddictApplicationCache( public OpenIddictApplicationCache(
@ -36,7 +36,7 @@ namespace OpenIddict.Core
SizeLimit = options.CurrentValue.EntityCacheLimit SizeLimit = options.CurrentValue.EntityCacheLimit
}); });
_signals = new ConcurrentDictionary<string, Lazy<CancellationTokenSource>>(StringComparer.Ordinal); _signals = new ConcurrentDictionary<string, CancellationTokenSource>(StringComparer.Ordinal);
_store = resolver.Get<TApplication>(); _store = resolver.Get<TApplication>();
} }
@ -121,7 +121,7 @@ namespace OpenIddict.Core
{ {
foreach (var signal in _signals) foreach (var signal in _signals)
{ {
signal.Value.Value.Dispose(); signal.Value.Dispose();
} }
_cache.Dispose(); _cache.Dispose();
@ -378,9 +378,9 @@ namespace OpenIddict.Core
throw new InvalidOperationException("The application identifier cannot be extracted."); throw new InvalidOperationException("The application identifier cannot be extracted.");
} }
if (_signals.TryGetValue(identifier, out Lazy<CancellationTokenSource> signal)) if (_signals.TryGetValue(identifier, out CancellationTokenSource signal))
{ {
signal.Value.Cancel(); signal.Cancel();
_signals.TryRemove(identifier, out signal); _signals.TryRemove(identifier, out signal);
} }
@ -410,15 +410,9 @@ namespace OpenIddict.Core
throw new InvalidOperationException("The application identifier cannot be extracted."); throw new InvalidOperationException("The application identifier cannot be extracted.");
} }
var signal = _signals.GetOrAdd(identifier, delegate var signal = _signals.GetOrAdd(identifier, _ => new CancellationTokenSource());
{
// Note: a Lazy<CancellationTokenSource> is used here to ensure only one CancellationTokenSource
// can be created. Not doing so would result in expiration signals being potentially linked to
// multiple sources, with a single one of them being eventually tracked and thus, cancelable.
return new Lazy<CancellationTokenSource>(() => new CancellationTokenSource());
});
return new CancellationChangeToken(signal.Value.Token); return new CancellationChangeToken(signal.Token);
} }
} }
} }

36
src/OpenIddict.Core/Caches/OpenIddictAuthorizationCache.cs

@ -24,7 +24,7 @@ namespace OpenIddict.Core
public class OpenIddictAuthorizationCache<TAuthorization> : IOpenIddictAuthorizationCache<TAuthorization>, IDisposable where TAuthorization : class public class OpenIddictAuthorizationCache<TAuthorization> : IOpenIddictAuthorizationCache<TAuthorization>, IDisposable where TAuthorization : class
{ {
private readonly MemoryCache _cache; private readonly MemoryCache _cache;
private readonly ConcurrentDictionary<string, Lazy<CancellationTokenSource>> _signals; private readonly ConcurrentDictionary<string, CancellationTokenSource> _signals;
private readonly IOpenIddictAuthorizationStore<TAuthorization> _store; private readonly IOpenIddictAuthorizationStore<TAuthorization> _store;
public OpenIddictAuthorizationCache( public OpenIddictAuthorizationCache(
@ -36,7 +36,7 @@ namespace OpenIddict.Core
SizeLimit = options.CurrentValue.EntityCacheLimit SizeLimit = options.CurrentValue.EntityCacheLimit
}); });
_signals = new ConcurrentDictionary<string, Lazy<CancellationTokenSource>>(StringComparer.Ordinal); _signals = new ConcurrentDictionary<string, CancellationTokenSource>(StringComparer.Ordinal);
_store = resolver.Get<TAuthorization>(); _store = resolver.Get<TAuthorization>();
} }
@ -97,7 +97,7 @@ namespace OpenIddict.Core
Subject = await _store.GetSubjectAsync(authorization, cancellationToken) Subject = await _store.GetSubjectAsync(authorization, cancellationToken)
}); });
var signal = await CreateExpirationTokenAsync(authorization, cancellationToken); var signal = await CreateExpirationSignalAsync(authorization, cancellationToken);
if (signal == null) if (signal == null)
{ {
throw new InvalidOperationException("An error occurred while creating an expiration signal."); throw new InvalidOperationException("An error occurred while creating an expiration signal.");
@ -122,7 +122,7 @@ namespace OpenIddict.Core
{ {
foreach (var signal in _signals) foreach (var signal in _signals)
{ {
signal.Value.Value.Dispose(); signal.Value.Dispose();
} }
_cache.Dispose(); _cache.Dispose();
@ -175,7 +175,7 @@ namespace OpenIddict.Core
{ {
foreach (var authorization in authorizations) foreach (var authorization in authorizations)
{ {
var signal = await CreateExpirationTokenAsync(authorization, cancellationToken); var signal = await CreateExpirationSignalAsync(authorization, cancellationToken);
if (signal == null) if (signal == null)
{ {
throw new InvalidOperationException("An error occurred while creating an expiration signal."); throw new InvalidOperationException("An error occurred while creating an expiration signal.");
@ -248,7 +248,7 @@ namespace OpenIddict.Core
{ {
foreach (var authorization in authorizations) foreach (var authorization in authorizations)
{ {
var signal = await CreateExpirationTokenAsync(authorization, cancellationToken); var signal = await CreateExpirationSignalAsync(authorization, cancellationToken);
if (signal == null) if (signal == null)
{ {
throw new InvalidOperationException("An error occurred while creating an expiration signal."); throw new InvalidOperationException("An error occurred while creating an expiration signal.");
@ -328,7 +328,7 @@ namespace OpenIddict.Core
{ {
foreach (var authorization in authorizations) foreach (var authorization in authorizations)
{ {
var signal = await CreateExpirationTokenAsync(authorization, cancellationToken); var signal = await CreateExpirationSignalAsync(authorization, cancellationToken);
if (signal == null) if (signal == null)
{ {
throw new InvalidOperationException("An error occurred while creating an expiration signal."); throw new InvalidOperationException("An error occurred while creating an expiration signal.");
@ -441,7 +441,7 @@ namespace OpenIddict.Core
{ {
foreach (var authorization in authorizations) foreach (var authorization in authorizations)
{ {
var signal = await CreateExpirationTokenAsync(authorization, cancellationToken); var signal = await CreateExpirationSignalAsync(authorization, cancellationToken);
if (signal == null) if (signal == null)
{ {
throw new InvalidOperationException("An error occurred while creating an expiration signal."); throw new InvalidOperationException("An error occurred while creating an expiration signal.");
@ -498,7 +498,7 @@ namespace OpenIddict.Core
{ {
if (authorization != null) if (authorization != null)
{ {
var signal = await CreateExpirationTokenAsync(authorization, cancellationToken); var signal = await CreateExpirationSignalAsync(authorization, cancellationToken);
if (signal == null) if (signal == null)
{ {
throw new InvalidOperationException("An error occurred while creating an expiration signal."); throw new InvalidOperationException("An error occurred while creating an expiration signal.");
@ -556,7 +556,7 @@ namespace OpenIddict.Core
{ {
foreach (var authorization in authorizations) foreach (var authorization in authorizations)
{ {
var signal = await CreateExpirationTokenAsync(authorization, cancellationToken); var signal = await CreateExpirationSignalAsync(authorization, cancellationToken);
if (signal == null) if (signal == null)
{ {
throw new InvalidOperationException("An error occurred while creating an expiration signal."); throw new InvalidOperationException("An error occurred while creating an expiration signal.");
@ -596,9 +596,9 @@ namespace OpenIddict.Core
throw new InvalidOperationException("The application identifier cannot be extracted."); throw new InvalidOperationException("The application identifier cannot be extracted.");
} }
if (_signals.TryGetValue(identifier, out Lazy<CancellationTokenSource> signal)) if (_signals.TryGetValue(identifier, out CancellationTokenSource signal))
{ {
signal.Value.Cancel(); signal.Cancel();
_signals.TryRemove(identifier, out signal); _signals.TryRemove(identifier, out signal);
} }
@ -614,7 +614,7 @@ namespace OpenIddict.Core
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation, /// A <see cref="Task"/> that can be used to monitor the asynchronous operation,
/// whose result returns an expiration signal for the specified authorization. /// whose result returns an expiration signal for the specified authorization.
/// </returns> /// </returns>
protected virtual async Task<IChangeToken> CreateExpirationTokenAsync( protected virtual async Task<IChangeToken> CreateExpirationSignalAsync(
[NotNull] TAuthorization authorization, CancellationToken cancellationToken) [NotNull] TAuthorization authorization, CancellationToken cancellationToken)
{ {
if (authorization == null) if (authorization == null)
@ -628,15 +628,9 @@ namespace OpenIddict.Core
throw new InvalidOperationException("The authorization identifier cannot be extracted."); throw new InvalidOperationException("The authorization identifier cannot be extracted.");
} }
var signal = _signals.GetOrAdd(identifier, delegate var signal = _signals.GetOrAdd(identifier, _ => new CancellationTokenSource());
{
// Note: a Lazy<CancellationTokenSource> is used here to ensure only one CancellationTokenSource
// can be created. Not doing so would result in expiration signals being potentially linked to
// multiple sources, with a single one of them being eventually tracked and thus, cancelable.
return new Lazy<CancellationTokenSource>(() => new CancellationTokenSource());
});
return new CancellationChangeToken(signal.Value.Token); return new CancellationChangeToken(signal.Token);
} }
} }
} }

20
src/OpenIddict.Core/Caches/OpenIddictScopeCache.cs

@ -25,7 +25,7 @@ namespace OpenIddict.Core
public class OpenIddictScopeCache<TScope> : IOpenIddictScopeCache<TScope>, IDisposable where TScope : class public class OpenIddictScopeCache<TScope> : IOpenIddictScopeCache<TScope>, IDisposable where TScope : class
{ {
private readonly MemoryCache _cache; private readonly MemoryCache _cache;
private readonly ConcurrentDictionary<string, Lazy<CancellationTokenSource>> _signals; private readonly ConcurrentDictionary<string, CancellationTokenSource> _signals;
private readonly IOpenIddictScopeStore<TScope> _store; private readonly IOpenIddictScopeStore<TScope> _store;
public OpenIddictScopeCache( public OpenIddictScopeCache(
@ -37,7 +37,7 @@ namespace OpenIddict.Core
SizeLimit = options.CurrentValue.EntityCacheLimit SizeLimit = options.CurrentValue.EntityCacheLimit
}); });
_signals = new ConcurrentDictionary<string, Lazy<CancellationTokenSource>>(StringComparer.Ordinal); _signals = new ConcurrentDictionary<string, CancellationTokenSource>(StringComparer.Ordinal);
_store = resolver.Get<TScope>(); _store = resolver.Get<TScope>();
} }
@ -113,7 +113,7 @@ namespace OpenIddict.Core
{ {
foreach (var signal in _signals) foreach (var signal in _signals)
{ {
signal.Value.Value.Dispose(); signal.Value.Dispose();
} }
_cache.Dispose(); _cache.Dispose();
@ -349,9 +349,9 @@ namespace OpenIddict.Core
throw new InvalidOperationException("The application identifier cannot be extracted."); throw new InvalidOperationException("The application identifier cannot be extracted.");
} }
if (_signals.TryGetValue(identifier, out Lazy<CancellationTokenSource> signal)) if (_signals.TryGetValue(identifier, out CancellationTokenSource signal))
{ {
signal.Value.Cancel(); signal.Cancel();
_signals.TryRemove(identifier, out signal); _signals.TryRemove(identifier, out signal);
} }
@ -380,15 +380,9 @@ namespace OpenIddict.Core
throw new InvalidOperationException("The scope identifier cannot be extracted."); throw new InvalidOperationException("The scope identifier cannot be extracted.");
} }
var signal = _signals.GetOrAdd(identifier, delegate var signal = _signals.GetOrAdd(identifier, _ => new CancellationTokenSource());
{
// Note: a Lazy<CancellationTokenSource> is used here to ensure only one CancellationTokenSource
// can be created. Not doing so would result in expiration signals being potentially linked to
// multiple sources, with a single one of them being eventually tracked and thus, cancelable.
return new Lazy<CancellationTokenSource>(() => new CancellationTokenSource());
});
return new CancellationChangeToken(signal.Value.Token); return new CancellationChangeToken(signal.Token);
} }
} }
} }

20
src/OpenIddict.Core/Caches/OpenIddictTokenCache.cs

@ -24,7 +24,7 @@ namespace OpenIddict.Core
public class OpenIddictTokenCache<TToken> : IOpenIddictTokenCache<TToken>, IDisposable where TToken : class public class OpenIddictTokenCache<TToken> : IOpenIddictTokenCache<TToken>, IDisposable where TToken : class
{ {
private readonly MemoryCache _cache; private readonly MemoryCache _cache;
private readonly ConcurrentDictionary<string, Lazy<CancellationTokenSource>> _signals; private readonly ConcurrentDictionary<string, CancellationTokenSource> _signals;
private readonly IOpenIddictTokenStore<TToken> _store; private readonly IOpenIddictTokenStore<TToken> _store;
public OpenIddictTokenCache( public OpenIddictTokenCache(
@ -36,7 +36,7 @@ namespace OpenIddict.Core
SizeLimit = options.CurrentValue.EntityCacheLimit SizeLimit = options.CurrentValue.EntityCacheLimit
}); });
_signals = new ConcurrentDictionary<string, Lazy<CancellationTokenSource>>(StringComparer.Ordinal); _signals = new ConcurrentDictionary<string, CancellationTokenSource>(StringComparer.Ordinal);
_store = resolver.Get<TToken>(); _store = resolver.Get<TToken>();
} }
@ -145,7 +145,7 @@ namespace OpenIddict.Core
{ {
foreach (var signal in _signals) foreach (var signal in _signals)
{ {
signal.Value.Value.Dispose(); signal.Value.Dispose();
} }
_cache.Dispose(); _cache.Dispose();
@ -679,9 +679,9 @@ namespace OpenIddict.Core
throw new InvalidOperationException("The application identifier cannot be extracted."); throw new InvalidOperationException("The application identifier cannot be extracted.");
} }
if (_signals.TryGetValue(identifier, out Lazy<CancellationTokenSource> signal)) if (_signals.TryGetValue(identifier, out CancellationTokenSource signal))
{ {
signal.Value.Cancel(); signal.Cancel();
_signals.TryRemove(identifier, out signal); _signals.TryRemove(identifier, out signal);
} }
@ -710,15 +710,9 @@ namespace OpenIddict.Core
throw new InvalidOperationException("The token identifier cannot be extracted."); throw new InvalidOperationException("The token identifier cannot be extracted.");
} }
var signal = _signals.GetOrAdd(identifier, delegate var signal = _signals.GetOrAdd(identifier, _ => new CancellationTokenSource());
{
// Note: a Lazy<CancellationTokenSource> is used here to ensure only one CancellationTokenSource
// can be created. Not doing so would result in expiration signals being potentially linked to
// multiple sources, with a single one of them being eventually tracked and thus, cancelable.
return new Lazy<CancellationTokenSource>(() => new CancellationTokenSource());
});
return new CancellationChangeToken(signal.Value.Token); return new CancellationChangeToken(signal.Token);
} }
} }
} }

Loading…
Cancel
Save