feat(Web): Verteilter Sql Server-Cache hinzugefügt.

- Bat-Datei erstellt, um Tabelle für Cache zu erstellen.
 - Sql-Datei zum Erstellen einer Tabelle für den Cache erstellt
This commit is contained in:
Developer 02 2024-11-29 14:08:07 +01:00
parent 9d1a2e7254
commit 2a963a1861
5 changed files with 73 additions and 2 deletions

View File

@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
namespace EnvelopeGenerator.Web.Controllers.Test
{
[Route("api/[controller]")]
[ApiController]
public class TestCacheController : ControllerBase
{
private readonly IDistributedCache _cache;
public TestCacheController(IDistributedCache cache)
{
_cache = cache;
}
[HttpPost]
public async Task<IActionResult> SetCacheAsync(string key, string value)
{
var options = new DistributedCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromMinutes(5));
await _cache.SetStringAsync(key, value, options);
return Ok();
}
[HttpGet]
public async Task<IActionResult> GetCacheAsync(string key)
{
var value = await _cache.GetStringAsync(key);
return value is null ? BadRequest() : Ok(value);
}
}
}

View File

@ -57,6 +57,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.15" />
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="7.0.20" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.2.5" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.3.0" />
@ -120,6 +121,12 @@
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Update="Scripts\create-sql-cache.bat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\create-sql-cache.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -1,4 +1,3 @@
using DigitalData.UserManager.Infrastructure.Repositories;
using EnvelopeGenerator.Application.Contracts;
using EnvelopeGenerator.Application.Services;
using EnvelopeGenerator.Web.Services;
@ -9,7 +8,6 @@ using NLog.Web;
using DigitalData.Core.API;
using Microsoft.AspNetCore.Authentication.Cookies;
using EnvelopeGenerator.Web.Models;
using DigitalData.Core.DTO;
using System.Text.Encodings.Web;
using Ganss.Xss;
using Microsoft.Extensions.Options;
@ -81,6 +79,12 @@ try
//AddEF Core dbcontext
var connStr = config.GetConnectionString(Key.Default) ?? throw new InvalidOperationException("There is no default connection string in appsettings.json.");
builder.Services.AddDbContext<EGDbContext>(options => options.UseSqlServer(connStr));
builder.Services.AddDistributedSqlServerCache(options =>
{
options.ConnectionString = connStr;
options.SchemaName = "dbo";
options.TableName = "TBDD_CACHE";
});
// Add envelope generator services
builder.Services.AddEnvelopeGenerator(config);

View File

@ -0,0 +1,2 @@
dotnet sql-cache create "CONNECTION_STRING" dbo TBDD_CACHE
pause

View File

@ -0,0 +1,23 @@
USE [DD_ECM]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TBDD_CACHE](
[Id] [nvarchar](449) NOT NULL,
[Value] [varbinary](max) NOT NULL,
[ExpiresAtTime] [datetimeoffset](7) NOT NULL,
[SlidingExpirationInSeconds] [bigint] NULL,
[AbsoluteExpiration] [datetimeoffset](7) NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO