feat(dal): IRedisCacheService
This commit is contained in:
@@ -48,6 +48,8 @@ namespace PARR.API.Contracts.V1
|
|||||||
|
|
||||||
public const string GetNextSchedule = Base + "/tests/next-schedule/" + paramApplicationInWorkId + "/" + paramDate;
|
public const string GetNextSchedule = Base + "/tests/next-schedule/" + paramApplicationInWorkId + "/" + paramDate;
|
||||||
|
|
||||||
|
public const string CreateCache = Base + "/tests/cache/";
|
||||||
|
|
||||||
public const string paramDate = "{lastRunDate}";
|
public const string paramDate = "{lastRunDate}";
|
||||||
public const string paramApplicationInWorkId = "{applicationInWorkId}";
|
public const string paramApplicationInWorkId = "{applicationInWorkId}";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using PARR.API.Contracts.V1;
|
|||||||
using PARR.API.Contracts.V1.Responses.Base;
|
using PARR.API.Contracts.V1.Responses.Base;
|
||||||
using PARR.API.Controllers.V1.Base;
|
using PARR.API.Controllers.V1.Base;
|
||||||
using PARR.API.Services.Interfaces;
|
using PARR.API.Services.Interfaces;
|
||||||
|
using PARR.DAL.CacheServices;
|
||||||
using PARR.DAL.TransformServices;
|
using PARR.DAL.TransformServices;
|
||||||
|
|
||||||
namespace PARR.API.Controllers.V1
|
namespace PARR.API.Controllers.V1
|
||||||
@@ -11,11 +12,13 @@ namespace PARR.API.Controllers.V1
|
|||||||
{
|
{
|
||||||
private readonly IClientService clientService;
|
private readonly IClientService clientService;
|
||||||
private readonly IEsppScheduleTransformService esppScheduleTransformService;
|
private readonly IEsppScheduleTransformService esppScheduleTransformService;
|
||||||
|
private readonly IRedisCacheService redisCacheService;
|
||||||
|
|
||||||
public TestController(IClientService clientService, IEsppScheduleTransformService esppScheduleTransformService)
|
public TestController(IClientService clientService, IEsppScheduleTransformService esppScheduleTransformService, IRedisCacheService redisCacheService)
|
||||||
{
|
{
|
||||||
this.clientService = clientService;
|
this.clientService = clientService;
|
||||||
this.esppScheduleTransformService = esppScheduleTransformService;
|
this.esppScheduleTransformService = esppScheduleTransformService;
|
||||||
|
this.redisCacheService = redisCacheService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -62,5 +65,25 @@ namespace PARR.API.Controllers.V1
|
|||||||
var result = await esppScheduleTransformService.GetNextScheduleAsync(applicationInWorkId, lastRunDate);
|
var result = await esppScheduleTransformService.GetNextScheduleAsync(applicationInWorkId, lastRunDate);
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[HttpPost(ApiRoutes.Test.CreateCache)]
|
||||||
|
public async Task<IActionResult> CreateCache([FromBody] CaheRequestTest request)
|
||||||
|
{
|
||||||
|
await redisCacheService.SetCachedDataAsync(request.Key, request, TimeSpan.FromMinutes(1));
|
||||||
|
|
||||||
|
var fromCache = await redisCacheService.GetCachedDataAsync<CaheRequestTest>(request.Key);
|
||||||
|
|
||||||
|
return Ok(new { fromCache });
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CaheRequestTest
|
||||||
|
{
|
||||||
|
public required string Key { get; set; }
|
||||||
|
|
||||||
|
public required string Value { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
{
|
{
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"RedisConnection": "10.99.253.216:6379"
|
||||||
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Debug",
|
"Default": "Debug",
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"DefaultConnection": "Server=10.99.253.184;Database=parr;User Id=app_parr; Password=PosdfkhT&)%sdfligL&%5546;"
|
"DefaultConnection": "Server=10.99.253.184;Database=parr;User Id=app_parr; Password=PosdfkhT&)%sdfligL&%5546;",
|
||||||
|
"RedisConnection": "parr-redis:6379"
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
|
|||||||
13
PARR.DAL/CacheServices/IRedisCacheService.cs
Normal file
13
PARR.DAL/CacheServices/IRedisCacheService.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
namespace PARR.DAL.CacheServices
|
||||||
|
{
|
||||||
|
public interface IRedisCacheService
|
||||||
|
{
|
||||||
|
T? GetCachedData<T>(string key);
|
||||||
|
|
||||||
|
Task<T?> GetCachedDataAsync<T>(string key);
|
||||||
|
|
||||||
|
void SetCachedData<T>(string key, T data, TimeSpan cacheDuration);
|
||||||
|
|
||||||
|
Task SetCachedDataAsync<T>(string key, T data, TimeSpan cacheDuration);
|
||||||
|
}
|
||||||
|
}
|
||||||
63
PARR.DAL/CacheServices/RedisCacheService.cs
Normal file
63
PARR.DAL/CacheServices/RedisCacheService.cs
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace PARR.DAL.CacheServices
|
||||||
|
{
|
||||||
|
internal class RedisCacheService : IRedisCacheService
|
||||||
|
{
|
||||||
|
private readonly IDistributedCache cache;
|
||||||
|
|
||||||
|
public RedisCacheService(IDistributedCache cache)
|
||||||
|
{
|
||||||
|
this.cache = cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<T?> GetCachedDataAsync<T>(string key)
|
||||||
|
{
|
||||||
|
var jsonData = await cache.GetStringAsync(key);
|
||||||
|
|
||||||
|
if (jsonData == null)
|
||||||
|
return default(T);
|
||||||
|
|
||||||
|
return JsonSerializer.Deserialize<T>(jsonData);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public T? GetCachedData<T>(string key)
|
||||||
|
{
|
||||||
|
var jsonData = cache.GetString(key);
|
||||||
|
|
||||||
|
if (jsonData == null)
|
||||||
|
return default(T);
|
||||||
|
|
||||||
|
return JsonSerializer.Deserialize<T>(jsonData);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void SetCachedData<T>(string key, T data, TimeSpan cacheDuration)
|
||||||
|
{
|
||||||
|
var options = new DistributedCacheEntryOptions
|
||||||
|
{
|
||||||
|
AbsoluteExpirationRelativeToNow = cacheDuration
|
||||||
|
};
|
||||||
|
|
||||||
|
var jsonData = JsonSerializer.Serialize(data);
|
||||||
|
cache.SetString(key, jsonData, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task SetCachedDataAsync<T>(string key, T data, TimeSpan cacheDuration)
|
||||||
|
{
|
||||||
|
var options = new DistributedCacheEntryOptions
|
||||||
|
{
|
||||||
|
AbsoluteExpirationRelativeToNow = cacheDuration
|
||||||
|
};
|
||||||
|
|
||||||
|
var jsonData = JsonSerializer.Serialize(data);
|
||||||
|
await cache.SetStringAsync(key, jsonData, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
2756
PARR.DAL/Migrations/20231130020915_TblHostsAddIndexes.Designer.cs
generated
Normal file
2756
PARR.DAL/Migrations/20231130020915_TblHostsAddIndexes.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
27
PARR.DAL/Migrations/20231130020915_TblHostsAddIndexes.cs
Normal file
27
PARR.DAL/Migrations/20231130020915_TblHostsAddIndexes.cs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace PARR.DAL.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class TblHostsAddIndexes : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_Hosts_Ek_IP",
|
||||||
|
table: "Hosts",
|
||||||
|
columns: new[] { "Ek", "IP" });
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropIndex(
|
||||||
|
name: "IX_Hosts_Ek_IP",
|
||||||
|
table: "Hosts");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1636,6 +1636,8 @@ namespace PARR.DAL.Migrations
|
|||||||
|
|
||||||
b.HasIndex("ResponseAreaCode");
|
b.HasIndex("ResponseAreaCode");
|
||||||
|
|
||||||
|
b.HasIndex("Ek", "IP");
|
||||||
|
|
||||||
b.ToTable("Hosts");
|
b.ToTable("Hosts");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
using PARR.DAL.Models.Base;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using PARR.DAL.Models.Base;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.ComponentModel.DataAnnotations.Schema;
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
|
||||||
namespace PARR.DAL.Models
|
namespace PARR.DAL.Models
|
||||||
{
|
{
|
||||||
[Table("Hosts")]
|
[Table("Hosts")]
|
||||||
|
[Index(nameof(Ek), nameof(IP))]
|
||||||
public class Host : IBase
|
public class Host : IBase
|
||||||
{
|
{
|
||||||
[Key]
|
[Key]
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using PARR.DAL.CacheServices;
|
||||||
using PARR.DAL.Configurations.DbSettings;
|
using PARR.DAL.Configurations.DbSettings;
|
||||||
using PARR.DAL.Context;
|
using PARR.DAL.Context;
|
||||||
using PARR.DAL.Contracts;
|
using PARR.DAL.Contracts;
|
||||||
@@ -30,6 +31,13 @@ namespace PARR.DAL
|
|||||||
.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
|
.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
|
||||||
);
|
);
|
||||||
|
|
||||||
|
services.AddStackExchangeRedisCache(opt =>
|
||||||
|
{
|
||||||
|
opt.Configuration = configuration.GetConnectionString("RedisConnection");
|
||||||
|
});
|
||||||
|
|
||||||
|
services.AddTransient<IRedisCacheService, RedisCacheService>();
|
||||||
|
|
||||||
// Entity services
|
// Entity services
|
||||||
services.AddTransient<IHostService, HostService>();
|
services.AddTransient<IHostService, HostService>();
|
||||||
services.AddTransient<IApplicationService, ApplicationService>();
|
services.AddTransient<IApplicationService, ApplicationService>();
|
||||||
|
|||||||
Reference in New Issue
Block a user