diff --git a/src/OpenIddict.Core/Stores/OpenIddictApplicationStore.cs b/src/OpenIddict.Core/Stores/OpenIddictApplicationStore.cs index 08142fd5..b4981416 100644 --- a/src/OpenIddict.Core/Stores/OpenIddictApplicationStore.cs +++ b/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?; - 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>(permissions.GetValueOrDefault()); + return new ValueTask>(permissions); } /// @@ -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?; - 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>(addresses.GetValueOrDefault()); + return new ValueTask>(addresses); } /// @@ -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?; - 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>(addresses.GetValueOrDefault()); + return new ValueTask>(addresses); } /// diff --git a/src/OpenIddict.Core/Stores/OpenIddictScopeStore.cs b/src/OpenIddict.Core/Stores/OpenIddictScopeStore.cs index 4f7771c4..046dda95 100644 --- a/src/OpenIddict.Core/Stores/OpenIddictScopeStore.cs +++ b/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?; - 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>(resources.GetValueOrDefault()); + return new ValueTask>(resources); } /// diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs index 46ef278d..e5d1717b 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs +++ b/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 { - 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 { - 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 } } - /// - /// Retrieves an application using its unique identifier. - /// - /// The unique identifier associated with the application. - /// The that can be used to abort the operation. - /// - /// A that can be used to monitor the asynchronous operation, - /// whose result returns the client application corresponding to the identifier. - /// - public override Task 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)); - } - /// /// Executes the specified query and returns the first element. /// diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs index 1a494d88..beb2303c 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs +++ b/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 { - 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 { - 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 } } - /// - /// Retrieves an authorization using its unique identifier. - /// - /// The unique identifier associated with the authorization. - /// The that can be used to abort the operation. - /// - /// A that can be used to monitor the asynchronous operation, - /// whose result returns the authorization corresponding to the identifier. - /// - public override Task 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() - 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); - } - /// /// Retrieves the optional application identifier associated with an authorization. /// diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs index f7459fbd..20aaab33 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs @@ -43,9 +43,7 @@ namespace OpenIddict.EntityFramework where TContext : DbContext where TKey : IEquatable { - 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 { - 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); } - /// - /// Retrieves a scope using its unique identifier. - /// - /// The unique identifier associated with the scope. - /// The that can be used to abort the operation. - /// - /// A that can be used to monitor the asynchronous operation, - /// whose result returns the scope corresponding to the identifier. - /// - public override Task 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)); - } - /// /// Executes the specified query and returns the first element. /// diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs index 7b8bcb1e..64f9fc5e 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs +++ b/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 { - 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 { - 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); } - /// - /// Retrieves a token using its unique identifier. - /// - /// The unique identifier associated with the token. - /// The that can be used to abort the operation. - /// - /// A that can be used to monitor the asynchronous operation, - /// whose result returns the token corresponding to the unique identifier. - /// - public override Task 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() - 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); - } - /// /// Retrieves the optional application identifier associated with a token. /// diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs index 26fcbb16..62adf89c 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs +++ b/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 { - 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 { - public OpenIddictApplicationStore( - [NotNull] TContext context, - [NotNull] IMemoryCache cache) + public OpenIddictApplicationStore([NotNull] TContext context, [NotNull] IMemoryCache cache) : base(cache) { if (context == null) diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs index 356bb7d5..bc00816f 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs +++ b/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 { - 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 { - 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)); } - /// - /// Retrieves an authorization using its unique identifier. - /// - /// The unique identifier associated with the authorization. - /// The that can be used to abort the operation. - /// - /// A that can be used to monitor the asynchronous operation, - /// whose result returns the authorization corresponding to the identifier. - /// - public override Task 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() - 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); - } - /// /// Executes the specified query and returns the first element. /// diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictScopeStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictScopeStore.cs index f2386b9a..53bff672 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictScopeStore.cs +++ b/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 : OpenIddictScopeStore 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 { - 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 { - public OpenIddictScopeStore( - [NotNull] TContext context, - [NotNull] IMemoryCache cache) + public OpenIddictScopeStore([NotNull] TContext context, [NotNull] IMemoryCache cache) : base(cache) { if (context == null) diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs index 4f72ff21..7a957880 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs +++ b/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 { - 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 { - 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)); } - /// - /// Retrieves a token using its unique identifier. - /// - /// The unique identifier associated with the token. - /// The that can be used to abort the operation. - /// - /// A that can be used to monitor the asynchronous operation, - /// whose result returns the token corresponding to the unique identifier. - /// - public override Task 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() - 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); - } - /// /// Executes the specified query and returns the first element. ///