Browse Source

Backport the caching changes to OpenIddict 1.x

pull/670/head
Kévin Chalet 8 years ago
parent
commit
e0d65df51a
  1. 42
      src/OpenIddict.Core/Stores/OpenIddictApplicationStore.cs
  2. 14
      src/OpenIddict.Core/Stores/OpenIddictScopeStore.cs
  3. 31
      src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs
  4. 41
      src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs
  5. 27
      src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs
  6. 41
      src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs
  7. 12
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs
  8. 41
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs
  9. 13
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictScopeStore.cs
  10. 41
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs

42
src/OpenIddict.Core/Stores/OpenIddictApplicationStore.cs

@ -390,15 +390,17 @@ namespace OpenIddict.Core
// To mitigate that, the resulting array is stored in the memory cache.
var key = string.Concat(nameof(GetPermissionsAsync), "\x1e", application.Permissions);
var permissions = Cache.Get(key) as ImmutableArray<string>?;
if (permissions == null)
var permissions = Cache.GetOrCreate(key, entry =>
{
permissions = Cache.Set(key, JArray.Parse(application.Permissions)
entry.SetPriority(CacheItemPriority.High)
.SetSlidingExpiration(TimeSpan.FromMinutes(1));
return JArray.Parse(application.Permissions)
.Select(element => (string) element)
.ToImmutableArray());
}
.ToImmutableArray();
});
return new ValueTask<ImmutableArray<string>>(permissions.GetValueOrDefault());
return new ValueTask<ImmutableArray<string>>(permissions);
}
/// <summary>
@ -426,15 +428,17 @@ namespace OpenIddict.Core
// To mitigate that, the resulting array is stored in the memory cache.
var key = string.Concat(nameof(GetPostLogoutRedirectUrisAsync), "\x1e", application.PostLogoutRedirectUris);
var addresses = Cache.Get(key) as ImmutableArray<string>?;
if (addresses == null)
var addresses = Cache.GetOrCreate(key, entry =>
{
addresses = Cache.Set(key, JArray.Parse(application.PostLogoutRedirectUris)
entry.SetPriority(CacheItemPriority.High)
.SetSlidingExpiration(TimeSpan.FromMinutes(1));
return JArray.Parse(application.PostLogoutRedirectUris)
.Select(element => (string) element)
.ToImmutableArray());
}
.ToImmutableArray();
});
return new ValueTask<ImmutableArray<string>>(addresses.GetValueOrDefault());
return new ValueTask<ImmutableArray<string>>(addresses);
}
/// <summary>
@ -486,15 +490,17 @@ namespace OpenIddict.Core
// To mitigate that, the resulting array is stored in the memory cache.
var key = string.Concat(nameof(GetRedirectUrisAsync), "\x1e", application.RedirectUris);
var addresses = Cache.Get(key) as ImmutableArray<string>?;
if (addresses == null)
var addresses = Cache.GetOrCreate(key, entry =>
{
addresses = Cache.Set(key, JArray.Parse(application.RedirectUris)
entry.SetPriority(CacheItemPriority.High)
.SetSlidingExpiration(TimeSpan.FromMinutes(1));
return JArray.Parse(application.RedirectUris)
.Select(element => (string) element)
.ToImmutableArray());
}
.ToImmutableArray();
});
return new ValueTask<ImmutableArray<string>>(addresses.GetValueOrDefault());
return new ValueTask<ImmutableArray<string>>(addresses);
}
/// <summary>

14
src/OpenIddict.Core/Stores/OpenIddictScopeStore.cs

@ -303,15 +303,17 @@ namespace OpenIddict.Core
// To mitigate that, the resulting array is stored in the memory cache.
var key = string.Concat(nameof(GetResourcesAsync), "\x1e", scope.Resources);
var resources = Cache.Get(key) as ImmutableArray<string>?;
if (resources == null)
var resources = Cache.GetOrCreate(key, entry =>
{
resources = Cache.Set(key, JArray.Parse(scope.Resources)
entry.SetPriority(CacheItemPriority.High)
.SetSlidingExpiration(TimeSpan.FromMinutes(1));
return JArray.Parse(scope.Resources)
.Select(element => (string) element)
.ToImmutableArray());
}
.ToImmutableArray();
});
return new ValueTask<ImmutableArray<string>>(resources.GetValueOrDefault());
return new ValueTask<ImmutableArray<string>>(resources);
}
/// <summary>

31
src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs

@ -29,9 +29,7 @@ namespace OpenIddict.EntityFramework
OpenIddictToken, TContext, string>
where TContext : DbContext
{
public OpenIddictApplicationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictApplicationStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(context, cache)
{
}
@ -49,9 +47,7 @@ namespace OpenIddict.EntityFramework
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictApplicationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictApplicationStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(context, cache)
{
}
@ -74,9 +70,7 @@ namespace OpenIddict.EntityFramework
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictApplicationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictApplicationStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(cache)
{
if (context == null)
@ -216,25 +210,6 @@ namespace OpenIddict.EntityFramework
}
}
/// <summary>
/// Retrieves an application using its unique identifier.
/// </summary>
/// <param name="identifier">The unique identifier associated with the application.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
/// <returns>
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation,
/// whose result returns the client application corresponding to the identifier.
/// </returns>
public override Task<TApplication> FindByIdAsync([NotNull] string identifier, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(identifier))
{
throw new ArgumentException("The identifier cannot be null or empty.", nameof(identifier));
}
return Applications.FindAsync(cancellationToken, ConvertIdentifierFromString(identifier));
}
/// <summary>
/// Executes the specified query and returns the first element.
/// </summary>

41
src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs

@ -29,9 +29,7 @@ namespace OpenIddict.EntityFramework
OpenIddictToken, TContext, string>
where TContext : DbContext
{
public OpenIddictAuthorizationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictAuthorizationStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(context, cache)
{
}
@ -49,9 +47,7 @@ namespace OpenIddict.EntityFramework
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictAuthorizationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictAuthorizationStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(context, cache)
{
}
@ -74,9 +70,7 @@ namespace OpenIddict.EntityFramework
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictAuthorizationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictAuthorizationStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(cache)
{
if (context == null)
@ -198,35 +192,6 @@ namespace OpenIddict.EntityFramework
}
}
/// <summary>
/// Retrieves an authorization using its unique identifier.
/// </summary>
/// <param name="identifier">The unique identifier associated with the authorization.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
/// <returns>
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation,
/// whose result returns the authorization corresponding to the identifier.
/// </returns>
public override Task<TAuthorization> FindByIdAsync([NotNull] string identifier, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(identifier))
{
throw new ArgumentException("The identifier cannot be null or empty.", nameof(identifier));
}
var authorization = (from entry in Context.ChangeTracker.Entries<TAuthorization>()
where entry.Entity != null
where entry.Entity.Id.Equals(ConvertIdentifierFromString(identifier))
select entry.Entity).FirstOrDefault();
if (authorization != null)
{
return Task.FromResult(authorization);
}
return base.FindByIdAsync(identifier, cancellationToken);
}
/// <summary>
/// Retrieves the optional application identifier associated with an authorization.
/// </summary>

27
src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs

@ -43,9 +43,7 @@ namespace OpenIddict.EntityFramework
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictScopeStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictScopeStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(context, cache)
{
}
@ -63,9 +61,7 @@ namespace OpenIddict.EntityFramework
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictScopeStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictScopeStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(cache)
{
if (context == null)
@ -146,25 +142,6 @@ namespace OpenIddict.EntityFramework
return Context.SaveChangesAsync(cancellationToken);
}
/// <summary>
/// Retrieves a scope using its unique identifier.
/// </summary>
/// <param name="identifier">The unique identifier associated with the scope.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
/// <returns>
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation,
/// whose result returns the scope corresponding to the identifier.
/// </returns>
public override Task<TScope> FindByIdAsync([NotNull] string identifier, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(identifier))
{
throw new ArgumentException("The identifier cannot be null or empty.", nameof(identifier));
}
return Scopes.FindAsync(cancellationToken, ConvertIdentifierFromString(identifier));
}
/// <summary>
/// Executes the specified query and returns the first element.
/// </summary>

41
src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs

@ -29,9 +29,7 @@ namespace OpenIddict.EntityFramework
OpenIddictAuthorization, TContext, string>
where TContext : DbContext
{
public OpenIddictTokenStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictTokenStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(context, cache)
{
}
@ -49,9 +47,7 @@ namespace OpenIddict.EntityFramework
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictTokenStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictTokenStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(context, cache)
{
}
@ -74,9 +70,7 @@ namespace OpenIddict.EntityFramework
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictTokenStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictTokenStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(cache)
{
if (context == null)
@ -165,35 +159,6 @@ namespace OpenIddict.EntityFramework
return Context.SaveChangesAsync(cancellationToken);
}
/// <summary>
/// Retrieves a token using its unique identifier.
/// </summary>
/// <param name="identifier">The unique identifier associated with the token.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
/// <returns>
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation,
/// whose result returns the token corresponding to the unique identifier.
/// </returns>
public override Task<TToken> FindByIdAsync([NotNull] string identifier, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(identifier))
{
throw new ArgumentException("The identifier cannot be null or empty.", nameof(identifier));
}
var token = (from entry in Context.ChangeTracker.Entries<TToken>()
where entry.Entity != null
where entry.Entity.Id.Equals(ConvertIdentifierFromString(identifier))
select entry.Entity).FirstOrDefault();
if (token != null)
{
return Task.FromResult(token);
}
return base.FindByIdAsync(identifier, cancellationToken);
}
/// <summary>
/// Retrieves the optional application identifier associated with a token.
/// </summary>

12
src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs

@ -31,9 +31,7 @@ namespace OpenIddict.EntityFrameworkCore
OpenIddictToken, TContext, string>
where TContext : DbContext
{
public OpenIddictApplicationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictApplicationStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(context, cache)
{
}
@ -51,9 +49,7 @@ namespace OpenIddict.EntityFrameworkCore
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictApplicationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictApplicationStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(context, cache)
{
}
@ -76,9 +72,7 @@ namespace OpenIddict.EntityFrameworkCore
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictApplicationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictApplicationStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(cache)
{
if (context == null)

41
src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs

@ -31,9 +31,7 @@ namespace OpenIddict.EntityFrameworkCore
OpenIddictToken, TContext, string>
where TContext : DbContext
{
public OpenIddictAuthorizationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictAuthorizationStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(context, cache)
{
}
@ -51,9 +49,7 @@ namespace OpenIddict.EntityFrameworkCore
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictAuthorizationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictAuthorizationStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(context, cache)
{
}
@ -76,9 +72,7 @@ namespace OpenIddict.EntityFrameworkCore
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictAuthorizationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictAuthorizationStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(cache)
{
if (context == null)
@ -359,35 +353,6 @@ namespace OpenIddict.EntityFrameworkCore
Authorizations, Applications, ConvertIdentifierFromString(client), subject, status, type).ToListAsync(cancellationToken));
}
/// <summary>
/// Retrieves an authorization using its unique identifier.
/// </summary>
/// <param name="identifier">The unique identifier associated with the authorization.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
/// <returns>
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation,
/// whose result returns the authorization corresponding to the identifier.
/// </returns>
public override Task<TAuthorization> FindByIdAsync([NotNull] string identifier, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(identifier))
{
throw new ArgumentException("The identifier cannot be null or empty.", nameof(identifier));
}
var authorization = (from entry in Context.ChangeTracker.Entries<TAuthorization>()
where entry.Entity != null &&
entry.Entity.Id.Equals(ConvertIdentifierFromString(identifier))
select entry.Entity).FirstOrDefault();
if (authorization != null)
{
return Task.FromResult(authorization);
}
return base.FindByIdAsync(identifier, cancellationToken);
}
/// <summary>
/// Executes the specified query and returns the first element.
/// </summary>

13
src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictScopeStore.cs

@ -12,7 +12,6 @@ using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using OpenIddict.Core;
using OpenIddict.Models;
namespace OpenIddict.EntityFrameworkCore
@ -25,9 +24,7 @@ namespace OpenIddict.EntityFrameworkCore
public class OpenIddictScopeStore<TContext> : OpenIddictScopeStore<OpenIddictScope, TContext, string>
where TContext : DbContext
{
public OpenIddictScopeStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictScopeStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(context, cache)
{
}
@ -43,9 +40,7 @@ namespace OpenIddict.EntityFrameworkCore
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictScopeStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictScopeStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(context, cache)
{
}
@ -63,9 +58,7 @@ namespace OpenIddict.EntityFrameworkCore
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictScopeStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictScopeStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(cache)
{
if (context == null)

41
src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs

@ -31,9 +31,7 @@ namespace OpenIddict.EntityFrameworkCore
OpenIddictAuthorization, TContext, string>
where TContext : DbContext
{
public OpenIddictTokenStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictTokenStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(context, cache)
{
}
@ -51,9 +49,7 @@ namespace OpenIddict.EntityFrameworkCore
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictTokenStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictTokenStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(context, cache)
{
}
@ -76,9 +72,7 @@ namespace OpenIddict.EntityFrameworkCore
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictTokenStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
public OpenIddictTokenStore([NotNull] TContext context, [NotNull] IMemoryCache cache)
: base(cache)
{
if (context == null)
@ -229,35 +223,6 @@ namespace OpenIddict.EntityFrameworkCore
Authorizations, Tokens, ConvertIdentifierFromString(identifier)).ToListAsync(cancellationToken));
}
/// <summary>
/// Retrieves a token using its unique identifier.
/// </summary>
/// <param name="identifier">The unique identifier associated with the token.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
/// <returns>
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation,
/// whose result returns the token corresponding to the unique identifier.
/// </returns>
public override Task<TToken> FindByIdAsync([NotNull] string identifier, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(identifier))
{
throw new ArgumentException("The identifier cannot be null or empty.", nameof(identifier));
}
var token = (from entry in Context.ChangeTracker.Entries<TToken>()
where entry.Entity != null &&
entry.Entity.Id.Equals(ConvertIdentifierFromString(identifier))
select entry.Entity).FirstOrDefault();
if (token != null)
{
return Task.FromResult(token);
}
return base.FindByIdAsync(identifier, cancellationToken);
}
/// <summary>
/// Executes the specified query and returns the first element.
/// </summary>

Loading…
Cancel
Save