-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Asset Key Value Store for entity framework. (#83)
* Asset Key Value Store for entity framework. * Add missing files.
- Loading branch information
1 parent
709397f
commit 3ef5ac7
Showing
43 changed files
with
768 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
assets/Squidex.Assets.EntityFramework/AssetsServiceExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// ========================================================================== | ||
// Squidex Headless CMS | ||
// ========================================================================== | ||
// Copyright (c) Squidex UG (haftungsbeschraenkt) | ||
// All rights reserved. Licensed under the MIT license. | ||
// ========================================================================== | ||
|
||
using System.Text.Json; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Squidex.Assets; | ||
using Squidex.Assets.EntityFramework; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public static class AssetsServiceExtensions | ||
{ | ||
public static IServiceCollection AddEntityFrameworkAssetKeyValueStore<TContext, TEntity>(this IServiceCollection services) | ||
where TContext : DbContext | ||
where TEntity : class | ||
{ | ||
services.AddSingletonAs<EFAssetKeyValueStore<TContext, TEntity>>() | ||
.As<IAssetKeyValueStore<TEntity>>().AsSelf(); | ||
|
||
services.AddDbContextFactory<TContext>(); | ||
services.TryAddSingleton(JsonSerializerOptions.Default); | ||
|
||
return services; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
assets/Squidex.Assets.EntityFramework/EFAssetKeyValueEntity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// ========================================================================== | ||
// Squidex Headless CMS | ||
// ========================================================================== | ||
// Copyright (c) Squidex UG (haftungsbeschraenkt) | ||
// All rights reserved. Licensed under the MIT license. | ||
// ========================================================================== | ||
|
||
using System.ComponentModel.DataAnnotations; | ||
using System.Text.Json; | ||
|
||
namespace Squidex.Assets.EntityFramework; | ||
|
||
public sealed class EFAssetKeyValueEntity<T> where T : class | ||
{ | ||
[Key] | ||
public string Key { get; set; } | ||
|
||
public string Value { get; set; } | ||
|
||
public DateTimeOffset Expires { get; set; } | ||
|
||
public static EFAssetKeyValueEntity<T> Create(string key, T value, DateTimeOffset expires, | ||
JsonSerializerOptions options) | ||
{ | ||
var serialized = JsonSerializer.Serialize(value, options); | ||
|
||
return new EFAssetKeyValueEntity<T> { Key = key, Value = serialized, Expires = expires.ToUniversalTime() }; | ||
} | ||
|
||
public void Update(T value, JsonSerializerOptions options) | ||
{ | ||
Value = JsonSerializer.Serialize(value, options); | ||
} | ||
|
||
public T GetValue(JsonSerializerOptions options) | ||
{ | ||
return JsonSerializer.Deserialize<T>(Value, options) ?? | ||
throw new JsonException("Failed to deserialize json."); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
assets/Squidex.Assets.EntityFramework/EFAssetKeyValueStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// ========================================================================== | ||
// Squidex Headless CMS | ||
// ========================================================================== | ||
// Copyright (c) Squidex UG (haftungsbeschraenkt) | ||
// All rights reserved. Licensed under the MIT license. | ||
// ========================================================================== | ||
|
||
using System.Runtime.CompilerServices; | ||
using System.Text.Json; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Squidex.Assets.EntityFramework; | ||
|
||
public sealed class EFAssetKeyValueStore<TContext, TEntity>( | ||
IDbContextFactory<TContext> dbContextFactory, | ||
JsonSerializerOptions jsonSerializerOptions) | ||
: IAssetKeyValueStore<TEntity> | ||
where TContext : DbContext | ||
where TEntity : class | ||
{ | ||
public async Task DeleteAsync(string key, | ||
CancellationToken ct = default) | ||
{ | ||
ArgumentException.ThrowIfNullOrWhiteSpace(key); | ||
|
||
await using var dbContext = await dbContextFactory.CreateDbContextAsync(ct); | ||
|
||
await dbContext.Set<EFAssetKeyValueEntity<TEntity>>().Where(x => x.Key == key) | ||
.ExecuteDeleteAsync(ct); | ||
} | ||
|
||
public async Task<TEntity?> GetAsync(string key, | ||
CancellationToken ct = default) | ||
{ | ||
ArgumentException.ThrowIfNullOrWhiteSpace(key); | ||
|
||
await using var dbContext = await dbContextFactory.CreateDbContextAsync(ct); | ||
|
||
var entity = await dbContext.Set<EFAssetKeyValueEntity<TEntity>>().Where(x => x.Key == key) | ||
.FirstOrDefaultAsync(ct); | ||
|
||
if (entity == null) | ||
{ | ||
return default; | ||
} | ||
|
||
return entity?.GetValue(jsonSerializerOptions); | ||
} | ||
|
||
public async IAsyncEnumerable<(string Key, TEntity Value)> GetExpiredEntriesAsync(DateTimeOffset now, | ||
[EnumeratorCancellation] CancellationToken ct = default) | ||
{ | ||
await using var dbContext = await dbContextFactory.CreateDbContextAsync(ct); | ||
|
||
var query = dbContext.Set<EFAssetKeyValueEntity<TEntity>>().Where(x => x.Expires < now); | ||
|
||
foreach (var entity in query) | ||
{ | ||
yield return (entity.Key, entity.GetValue(jsonSerializerOptions)); | ||
} | ||
} | ||
|
||
public async Task SetAsync(string key, TEntity value, DateTimeOffset expiration, | ||
CancellationToken ct = default) | ||
{ | ||
ArgumentException.ThrowIfNullOrWhiteSpace(key); | ||
|
||
await using var dbContext = await dbContextFactory.CreateDbContextAsync(ct); | ||
|
||
var entity = EFAssetKeyValueEntity<TEntity>.Create(key, value, expiration, jsonSerializerOptions); | ||
try | ||
{ | ||
await dbContext.Set<EFAssetKeyValueEntity<TEntity>>().AddAsync(entity, ct); | ||
await dbContext.SaveChangesAsync(); | ||
} | ||
catch (DbUpdateException) | ||
{ | ||
dbContext.Entry(entity).State = EntityState.Modified; | ||
await dbContext.SaveChangesAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// ========================================================================== | ||
// Squidex Headless CMS | ||
// ========================================================================== | ||
// Copyright (c) Squidex UG (haftungsbeschraenkt) | ||
// All rights reserved. Licensed under the MIT license. | ||
// ========================================================================== | ||
|
||
using Squidex.Assets.EntityFramework; | ||
|
||
namespace Microsoft.EntityFrameworkCore; | ||
|
||
public static class EFSchema | ||
{ | ||
public static ModelBuilder AddAssetKeyValueStore<T>(this ModelBuilder modelBuilder) where T : class | ||
{ | ||
modelBuilder.Entity<EFAssetKeyValueEntity<T>>(b => | ||
{ | ||
b.ToTable($"AssetKeyValueStore_{typeof(T).Name}"); | ||
|
||
b.HasIndex(x => x.Expires); | ||
}); | ||
|
||
return modelBuilder; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
assets/Squidex.Assets.EntityFramework/Squidex.Assets.EntityFramework.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<LangVersion>Latest</LangVersion> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="logo-squared.png" Pack="true" PackagePath="\" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Meziantou.Analyzer" Version="2.0.179"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.11" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.11" /> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="RefactoringEssentials" Version="5.6.0" PrivateAssets="all" /> | ||
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="all" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<AdditionalFiles Include="..\..\stylecop.json" Link="stylecop.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\hosting\Squidex.Hosting.Abstractions\Squidex.Hosting.Abstractions.csproj" /> | ||
<ProjectReference Include="..\Squidex.Assets\Squidex.Assets.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.