Skip to content

Commit

Permalink
registration of pg cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkoSekulic committed Nov 11, 2024
1 parent d133fff commit 868441a
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 16 deletions.
1 change: 1 addition & 0 deletions backend/packagegroups/NuGet.props
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<PackageReference Update="DotNetEnv" Version="3.1.1" />
<PackageReference Update="NuGet.Versioning" Version="6.11.1" />
<PackageReference Update="DistributedLock.Postgres" Version="1.2.0" />
<PackageReference Update="Community.Microsoft.Extensions.Caching.PostgreSql" Version="4.0.6" />
</ItemGroup>

<ItemGroup Label="Packages used for testing">
Expand Down
1 change: 1 addition & 0 deletions backend/src/Designer/Designer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<PackageReference Include="Altinn.Common.AccessTokenClient" />
<PackageReference Include="Altinn.Platform.Storage.Interface" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" />
<PackageReference Include="Community.Microsoft.Extensions.Caching.PostgreSql" />
<PackageReference Include="CompilerAttributes" />
<PackageReference Include="DistributedLock.Postgres" />
<PackageReference Include="DotNetEnv" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ public class PostgreSQLSettings : ISettingsMarker
/// Password for app user for the postgres db
/// </summary>
public string DesignerDbPwd { get; set; }

public string FormattedConnectionString()
{
return string.Format(ConnectionString, DesignerDbPwd);
}
}
}
5 changes: 1 addition & 4 deletions backend/src/Designer/Infrastructure/ServiceRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ public static IServiceCollection RegisterServiceImplementations(this IServiceCol
services.AddDbContext<DesignerdbContext>(options =>
{
PostgreSQLSettings postgresSettings = configuration.GetSection(nameof(PostgreSQLSettings)).Get<PostgreSQLSettings>();
string connectionString = string.Format(
postgresSettings.ConnectionString,
postgresSettings.DesignerDbPwd);
options.UseNpgsql(connectionString);
options.UseNpgsql(postgresSettings.FormattedConnectionString());
});

services.AddScoped<IReleaseRepository, ORMReleaseRepository>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ public static IServiceCollection RegisterSynchronizationServices(this IServiceCo
services.AddSingleton<IDistributedLockProvider>(_ =>
{
PostgreSQLSettings postgresSettings = configuration.GetSection(nameof(PostgreSQLSettings)).Get<PostgreSQLSettings>();
string connectionString = string.Format(
postgresSettings.ConnectionString,
postgresSettings.DesignerDbPwd);
return new PostgresDistributedSynchronizationProvider(connectionString);
return new PostgresDistributedSynchronizationProvider(postgresSettings.FormattedConnectionString());
});
return services;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DROP TABLE IF EXISTS designer.distribuedcache;
DROP TABLE IF EXISTS designer.distributedcache;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE IF NOT EXISTS designer.distribuedcache
CREATE TABLE IF NOT EXISTS designer.distributedcache
(
"Id" text COLLATE pg_catalog."default" NOT NULL,
"Value" bytea,
Expand Down
14 changes: 14 additions & 0 deletions backend/src/Designer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Altinn.Studio.Designer.Services.Interfaces;
using Altinn.Studio.Designer.Tracing;
using Altinn.Studio.Designer.TypedHttpClients;
using Community.Microsoft.Extensions.Caching.PostgreSql;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Extensibility.EventCounterCollector;
using Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -264,6 +265,19 @@ void ConfigureServices(IServiceCollection services, IConfiguration configuration
services.AddFeatureManagement();
services.RegisterSynchronizationServices(configuration);

// Override the default distributed cache with a PostgreSQL implementation
services.AddDistributedPostgreSqlCache(setup =>
{
PostgreSQLSettings postgresSettings =
configuration.GetSection(nameof(PostgreSQLSettings)).Get<PostgreSQLSettings>();

setup.ConnectionString = postgresSettings.FormattedConnectionString();
setup.SchemaName = "designer";
setup.TableName = "distributedcache";
setup.DisableRemoveExpired = false;
setup.CreateInfrastructure = false;
setup.ExpiredItemsDeletionInterval = TimeSpan.FromMinutes(30);
});

if (!env.IsDevelopment())
{
Expand Down
4 changes: 1 addition & 3 deletions backend/src/Designer/Repository/DeploymentRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public class DeploymentRepository : IDeploymentRepository
/// </summary>
public DeploymentRepository(PostgreSQLSettings postgresSettings, ILogger<DeploymentRepository> logger)
{
_connectionString = string.Format(
postgresSettings.ConnectionString,
postgresSettings.DesignerDbPwd);
_connectionString = postgresSettings.FormattedConnectionString();
_logger = logger;
}

Expand Down
4 changes: 1 addition & 3 deletions backend/src/Designer/Repository/ReleaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ public class ReleaseRepository : IReleaseRepository
/// </summary>
public ReleaseRepository(PostgreSQLSettings postgresSettings, ILogger<ReleaseRepository> logger)
{
_connectionString = string.Format(
postgresSettings.ConnectionString,
postgresSettings.DesignerDbPwd);
_connectionString = postgresSettings.FormattedConnectionString();
_logger = logger;
}

Expand Down

0 comments on commit 868441a

Please sign in to comment.