Browse Source

Cache permissions, redirect_uris and post_logout_redirect_uris in memory

pull/555/head
Kévin Chalet 8 years ago
parent
commit
f503fb7e7a
  1. 1
      src/OpenIddict.Core/OpenIddict.Core.csproj
  2. 58
      src/OpenIddict.Core/Stores/OpenIddictApplicationStore.cs
  3. 16
      src/OpenIddict.Core/Stores/OpenIddictAuthorizationStore.cs
  4. 16
      src/OpenIddict.Core/Stores/OpenIddictScopeStore.cs
  5. 16
      src/OpenIddict.Core/Stores/OpenIddictTokenStore.cs
  6. 20
      src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs
  7. 20
      src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs
  8. 20
      src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs
  9. 20
      src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs
  10. 20
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs
  11. 20
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs
  12. 20
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictScopeStore.cs
  13. 20
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs

1
src/OpenIddict.Core/OpenIddict.Core.csproj

@ -19,6 +19,7 @@
<ItemGroup>
<PackageReference Include="CryptoHelper.StrongName" Version="$(CryptoHelperVersion)" />
<PackageReference Include="JetBrains.Annotations" Version="$(JetBrainsVersion)" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Options" Version="$(AspNetCoreVersion)" />

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

@ -11,6 +11,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenIddict.Models;
@ -31,6 +32,21 @@ namespace OpenIddict.Core
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new()
where TKey : IEquatable<TKey>
{
protected OpenIddictApplicationStore([NotNull] IMemoryCache cache)
{
if (cache == null)
{
throw new ArgumentNullException(nameof(cache));
}
Cache = cache;
}
/// <summary>
/// Gets the memory cached associated with the current store.
/// </summary>
protected IMemoryCache Cache { get; }
/// <summary>
/// Determines the number of applications that exist in the database.
/// </summary>
@ -350,7 +366,19 @@ namespace OpenIddict.Core
return Task.FromResult(ImmutableArray.Create<string>());
}
return Task.FromResult(JArray.Parse(application.Permissions).Select(element => (string) element).ToImmutableArray());
// Note: parsing the stringified permissions is an expensive operation.
// 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)
{
permissions = Cache.Set(key, JArray.Parse(application.Permissions)
.Select(element => (string) element)
.ToImmutableArray());
}
return Task.FromResult(permissions.GetValueOrDefault());
}
/// <summary>
@ -374,7 +402,19 @@ namespace OpenIddict.Core
return Task.FromResult(ImmutableArray.Create<string>());
}
return Task.FromResult(JArray.Parse(application.PostLogoutRedirectUris).Select(element => (string) element).ToImmutableArray());
// Note: parsing the stringified addresses is an expensive operation.
// 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)
{
addresses = Cache.Set(key, JArray.Parse(application.PostLogoutRedirectUris)
.Select(element => (string) element)
.ToImmutableArray());
}
return Task.FromResult(addresses.GetValueOrDefault());
}
/// <summary>
@ -422,7 +462,19 @@ namespace OpenIddict.Core
return Task.FromResult(ImmutableArray.Create<string>());
}
return Task.FromResult(JArray.Parse(application.RedirectUris).Select(element => (string) element).ToImmutableArray());
// Note: parsing the stringified addresses is an expensive operation.
// 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)
{
addresses = Cache.Set(key, JArray.Parse(application.RedirectUris)
.Select(element => (string) element)
.ToImmutableArray());
}
return Task.FromResult(addresses.GetValueOrDefault());
}
/// <summary>

16
src/OpenIddict.Core/Stores/OpenIddictAuthorizationStore.cs

@ -12,6 +12,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenIddict.Models;
@ -32,6 +33,21 @@ namespace OpenIddict.Core
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new()
where TKey : IEquatable<TKey>
{
protected OpenIddictAuthorizationStore([NotNull] IMemoryCache cache)
{
if (cache == null)
{
throw new ArgumentNullException(nameof(cache));
}
Cache = cache;
}
/// <summary>
/// Gets the memory cached associated with the current store.
/// </summary>
protected IMemoryCache Cache { get; }
/// <summary>
/// Determines the number of authorizations that exist in the database.
/// </summary>

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

@ -11,6 +11,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenIddict.Models;
@ -27,6 +28,21 @@ namespace OpenIddict.Core
where TScope : OpenIddictScope<TKey>, new()
where TKey : IEquatable<TKey>
{
protected OpenIddictScopeStore([NotNull] IMemoryCache cache)
{
if (cache == null)
{
throw new ArgumentNullException(nameof(cache));
}
Cache = cache;
}
/// <summary>
/// Gets the memory cached associated with the current store.
/// </summary>
protected IMemoryCache Cache { get; }
/// <summary>
/// Determines the number of scopes that exist in the database.
/// </summary>

16
src/OpenIddict.Core/Stores/OpenIddictTokenStore.cs

@ -11,6 +11,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenIddict.Models;
@ -31,6 +32,21 @@ namespace OpenIddict.Core
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>, new()
where TKey : IEquatable<TKey>
{
protected OpenIddictTokenStore([NotNull] IMemoryCache cache)
{
if (cache == null)
{
throw new ArgumentNullException(nameof(cache));
}
Cache = cache;
}
/// <summary>
/// Gets the memory cached associated with the current store.
/// </summary>
protected IMemoryCache Cache { get; }
/// <summary>
/// Determines the number of tokens that exist in the database.
/// </summary>

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

@ -12,6 +12,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.Extensions.Caching.Memory;
using OpenIddict.Core;
using OpenIddict.Models;
@ -27,7 +28,12 @@ namespace OpenIddict.EntityFramework
OpenIddictToken, TContext, string>
where TContext : DbContext
{
public OpenIddictApplicationStore([NotNull] TContext context) : base(context) { }
public OpenIddictApplicationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(context, cache)
{
}
}
/// <summary>
@ -42,7 +48,12 @@ namespace OpenIddict.EntityFramework
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictApplicationStore([NotNull] TContext context) : base(context) { }
public OpenIddictApplicationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(context, cache)
{
}
}
/// <summary>
@ -62,7 +73,10 @@ namespace OpenIddict.EntityFramework
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictApplicationStore([NotNull] TContext context)
public OpenIddictApplicationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(cache)
{
if (context == null)
{

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

@ -12,6 +12,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.Extensions.Caching.Memory;
using OpenIddict.Core;
using OpenIddict.Models;
@ -27,7 +28,12 @@ namespace OpenIddict.EntityFramework
OpenIddictToken, TContext, string>
where TContext : DbContext
{
public OpenIddictAuthorizationStore([NotNull] TContext context) : base(context) { }
public OpenIddictAuthorizationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(context, cache)
{
}
}
/// <summary>
@ -42,7 +48,12 @@ namespace OpenIddict.EntityFramework
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictAuthorizationStore([NotNull] TContext context) : base(context) { }
public OpenIddictAuthorizationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(context, cache)
{
}
}
/// <summary>
@ -62,7 +73,10 @@ namespace OpenIddict.EntityFramework
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictAuthorizationStore([NotNull] TContext context)
public OpenIddictAuthorizationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(cache)
{
if (context == null)
{

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

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

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

@ -11,6 +11,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.Extensions.Caching.Memory;
using OpenIddict.Core;
using OpenIddict.Models;
@ -26,7 +27,12 @@ namespace OpenIddict.EntityFramework
OpenIddictAuthorization, TContext, string>
where TContext : DbContext
{
public OpenIddictTokenStore([NotNull] TContext context) : base(context) { }
public OpenIddictTokenStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(context, cache)
{
}
}
/// <summary>
@ -41,7 +47,12 @@ namespace OpenIddict.EntityFramework
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictTokenStore([NotNull] TContext context) : base(context) { }
public OpenIddictTokenStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(context, cache)
{
}
}
/// <summary>
@ -61,7 +72,10 @@ namespace OpenIddict.EntityFramework
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictTokenStore([NotNull] TContext context)
public OpenIddictTokenStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(cache)
{
if (context == null)
{

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

@ -12,6 +12,7 @@ using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using OpenIddict.Core;
using OpenIddict.Models;
@ -27,7 +28,12 @@ namespace OpenIddict.EntityFrameworkCore
OpenIddictToken, TContext, string>
where TContext : DbContext
{
public OpenIddictApplicationStore([NotNull] TContext context) : base(context) { }
public OpenIddictApplicationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(context, cache)
{
}
}
/// <summary>
@ -42,7 +48,12 @@ namespace OpenIddict.EntityFrameworkCore
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictApplicationStore([NotNull] TContext context) : base(context) { }
public OpenIddictApplicationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(context, cache)
{
}
}
/// <summary>
@ -62,7 +73,10 @@ namespace OpenIddict.EntityFrameworkCore
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictApplicationStore([NotNull] TContext context)
public OpenIddictApplicationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(cache)
{
if (context == null)
{

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

@ -12,6 +12,7 @@ using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using OpenIddict.Core;
using OpenIddict.Models;
@ -27,7 +28,12 @@ namespace OpenIddict.EntityFrameworkCore
OpenIddictToken, TContext, string>
where TContext : DbContext
{
public OpenIddictAuthorizationStore([NotNull] TContext context) : base(context) { }
public OpenIddictAuthorizationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(context, cache)
{
}
}
/// <summary>
@ -42,7 +48,12 @@ namespace OpenIddict.EntityFrameworkCore
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictAuthorizationStore([NotNull] TContext context) : base(context) { }
public OpenIddictAuthorizationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(context, cache)
{
}
}
/// <summary>
@ -62,7 +73,10 @@ namespace OpenIddict.EntityFrameworkCore
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictAuthorizationStore([NotNull] TContext context)
public OpenIddictAuthorizationStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(cache)
{
if (context == null)
{

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

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

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

@ -11,6 +11,7 @@ using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using OpenIddict.Core;
using OpenIddict.Models;
@ -26,7 +27,12 @@ namespace OpenIddict.EntityFrameworkCore
OpenIddictAuthorization, TContext, string>
where TContext : DbContext
{
public OpenIddictTokenStore([NotNull] TContext context) : base(context) { }
public OpenIddictTokenStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(context, cache)
{
}
}
/// <summary>
@ -41,7 +47,12 @@ namespace OpenIddict.EntityFrameworkCore
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictTokenStore([NotNull] TContext context) : base(context) { }
public OpenIddictTokenStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(context, cache)
{
}
}
/// <summary>
@ -61,7 +72,10 @@ namespace OpenIddict.EntityFrameworkCore
where TContext : DbContext
where TKey : IEquatable<TKey>
{
public OpenIddictTokenStore([NotNull] TContext context)
public OpenIddictTokenStore(
[NotNull] TContext context,
[NotNull] IMemoryCache cache)
: base(cache)
{
if (context == null)
{

Loading…
Cancel
Save