diff --git a/PARR.API/Controllers/V1/TestController.cs b/PARR.API/Controllers/V1/TestController.cs index 4ad09778..6270b967 100644 --- a/PARR.API/Controllers/V1/TestController.cs +++ b/PARR.API/Controllers/V1/TestController.cs @@ -9,7 +9,7 @@ using PARR.API.Contracts.V1.Responses.Base; using PARR.API.Controllers.V1.Base; using PARR.API.Extensions; using PARR.API.Services.Interfaces; -using PARR.DAL.CacheServices; +using PARR.DAL.Cache.Services.Base; using PARR.DAL.Contracts; using PARR.DAL.DomainModels; using PARR.DAL.DomainServices.Interfaces; diff --git a/PARR.API/Services/Implementations/AuthService.cs b/PARR.API/Services/Implementations/AuthService.cs index 33972180..4d96c5e9 100644 --- a/PARR.API/Services/Implementations/AuthService.cs +++ b/PARR.API/Services/Implementations/AuthService.cs @@ -4,7 +4,7 @@ using PARR.API.Domain.InfluxDbModels; using PARR.API.Services.Interfaces; using PARR.API.Settings; using PARR.Constants; -using PARR.DAL.CacheServices; +using PARR.DAL.Cache.Services.Base; using PARR.DAL.InfluxDbServices; using PARR.DAL.Services.Interfaces; using PARR.DAL.Services.Interfaces.Unit; diff --git a/PARR.DAL/Cache/Models/Base/IBaseCache.cs b/PARR.DAL/Cache/Models/Base/IBaseCache.cs new file mode 100644 index 00000000..1e9c1cbb --- /dev/null +++ b/PARR.DAL/Cache/Models/Base/IBaseCache.cs @@ -0,0 +1,14 @@ +namespace PARR.DAL.Cache.Models.Base +{ + /// + /// Базовый интерфейс для КЭШ моделей + /// + public interface IBaseCache + { + public T Data { get; set; } + + public DateTimeOffset Timestamp { get; set; } + + public string Source { get; set; } + } +} diff --git a/PARR.DAL/Cache/Models/MatchingStatus.cs b/PARR.DAL/Cache/Models/MatchingStatus.cs new file mode 100644 index 00000000..83d59f48 --- /dev/null +++ b/PARR.DAL/Cache/Models/MatchingStatus.cs @@ -0,0 +1,27 @@ +using PARR.Constants; +using PARR.DAL.Cache.Models.Base; + +namespace PARR.DAL.Cache.Models +{ + /// + /// КЭШ модель, состояние matching`a + /// + public class MatchingStatus : IBaseCache + { + public required MatchingStatusDto Data { get; set; } + + public DateTimeOffset Timestamp { get; set; } + + public required string Source { get; set; } + } + + + public class MatchingStatusDto + { + public DateTimeOffset DateStart { get; set; } + + public required TemplateMatcherActionEnum Action { get; set; } + + public string? Comment { get; set; } + } +} diff --git a/PARR.DAL/CacheServices/IRedisCacheService.cs b/PARR.DAL/Cache/Services/Base/IRedisCacheService.cs similarity index 84% rename from PARR.DAL/CacheServices/IRedisCacheService.cs rename to PARR.DAL/Cache/Services/Base/IRedisCacheService.cs index 4176205f..881fdf8a 100644 --- a/PARR.DAL/CacheServices/IRedisCacheService.cs +++ b/PARR.DAL/Cache/Services/Base/IRedisCacheService.cs @@ -1,4 +1,4 @@ -namespace PARR.DAL.CacheServices +namespace PARR.DAL.Cache.Services.Base { public interface IRedisCacheService { @@ -49,5 +49,13 @@ /// /// Task DeleteCachedDataAsync(string key); + + /// + /// Сгенерировать уникальный ключ + /// + /// + /// + /// + string GetKey(string[] keyParts, bool isUseHash = false); } } diff --git a/PARR.DAL/CacheServices/RedisCacheService.cs b/PARR.DAL/Cache/Services/Base/RedisCacheService.cs similarity index 68% rename from PARR.DAL/CacheServices/RedisCacheService.cs rename to PARR.DAL/Cache/Services/Base/RedisCacheService.cs index 352462d1..bcf82be9 100644 --- a/PARR.DAL/CacheServices/RedisCacheService.cs +++ b/PARR.DAL/Cache/Services/Base/RedisCacheService.cs @@ -1,7 +1,8 @@ using Microsoft.Extensions.Caching.Distributed; +using PARR.DAL.Migrations; using System.Text.Json; -namespace PARR.DAL.CacheServices +namespace PARR.DAL.Cache.Services.Base { internal class RedisCacheService : IRedisCacheService { @@ -69,5 +70,27 @@ namespace PARR.DAL.CacheServices cache.Remove(key); } + + public string GetKey(string[] keyParts, bool isUseHash = false) + { + if (keyParts.Length == 0) + { + throw new ArgumentNullException("keyParts не может быть пустым"); + } + + var keyStr = string.Join("_", keyParts); + + if (isUseHash) + { + using var sha256 = System.Security.Cryptography.SHA256.Create(); + var hashedBytes = sha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(keyStr)); + + // return Convert.ToBase64String(hashedBytes).Replace('+', '-').Replace('/', '_').Substring(0, 16); + return Convert.ToBase64String(hashedBytes).Substring(0, 16); + } + + return keyStr; + } + } } diff --git a/PARR.DAL/Cache/Services/CacheMatchingStatusService.cs b/PARR.DAL/Cache/Services/CacheMatchingStatusService.cs new file mode 100644 index 00000000..2293edac --- /dev/null +++ b/PARR.DAL/Cache/Services/CacheMatchingStatusService.cs @@ -0,0 +1,45 @@ +using PARR.DAL.Cache.Models; +using PARR.DAL.Cache.Services.Base; + +namespace PARR.DAL.Cache.Services +{ + internal class CacheMatchingStatusService : ICacheMatchingStatusService + { + private readonly IRedisCacheService redisCacheService; + + public CacheMatchingStatusService(IRedisCacheService redisCacheService) + { + this.redisCacheService = redisCacheService; + } + + public Task DeleteCacheByJobGroupIdAsync(Guid jobGroupId) + { + throw new NotImplementedException(); + } + + public Task DeleteCacheByJobIdAsync(Guid jobId) + { + throw new NotImplementedException(); + } + + public Task GetDataByJobGroupIdAsync(Guid jobGroupId) + { + throw new NotImplementedException(); + } + + public Task GetDataByJobIdAsync(Guid jobId) + { + throw new NotImplementedException(); + } + + public Task SetCahcheByJobGroupIdAsync(Guid jobGroupId, MatchingStatusDto data) + { + throw new NotImplementedException(); + } + + public Task SetCahcheByJobIdAsync(Guid jobId, MatchingStatusDto data) + { + throw new NotImplementedException(); + } + } +} diff --git a/PARR.DAL/Cache/Services/ICacheMatchingStatusService.cs b/PARR.DAL/Cache/Services/ICacheMatchingStatusService.cs new file mode 100644 index 00000000..c0802569 --- /dev/null +++ b/PARR.DAL/Cache/Services/ICacheMatchingStatusService.cs @@ -0,0 +1,19 @@ +using PARR.DAL.Cache.Models; + +namespace PARR.DAL.Cache.Services +{ + public interface ICacheMatchingStatusService + { + Task GetDataByJobIdAsync(Guid jobId); + + Task GetDataByJobGroupIdAsync(Guid jobGroupId); + + Task SetCahcheByJobIdAsync(Guid jobId, MatchingStatusDto data); + + Task SetCahcheByJobGroupIdAsync(Guid jobGroupId, MatchingStatusDto data); + + Task DeleteCacheByJobIdAsync(Guid jobId); + + Task DeleteCacheByJobGroupIdAsync(Guid jobGroupId); + } +} diff --git a/PARR.DAL/DomainServices/Implementations/UnitFilterService.cs b/PARR.DAL/DomainServices/Implementations/UnitFilterService.cs index 128546e2..9048bc13 100644 --- a/PARR.DAL/DomainServices/Implementations/UnitFilterService.cs +++ b/PARR.DAL/DomainServices/Implementations/UnitFilterService.cs @@ -1,6 +1,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; -using PARR.DAL.CacheServices; +using PARR.DAL.Cache.Services.Base; using PARR.DAL.Contracts; using PARR.DAL.DomainServices.Interfaces; using PARR.DAL.Models.Job; diff --git a/PARR.DAL/DomainServices/Shortcodes/ShortcodesService.cs b/PARR.DAL/DomainServices/Shortcodes/ShortcodesService.cs index 6c6bd562..2cff538a 100644 --- a/PARR.DAL/DomainServices/Shortcodes/ShortcodesService.cs +++ b/PARR.DAL/DomainServices/Shortcodes/ShortcodesService.cs @@ -1,7 +1,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using PARR.Constants; -using PARR.DAL.CacheServices; +using PARR.DAL.Cache.Services.Base; using PARR.DAL.Contracts; using PARR.DAL.DomainModels; using PARR.DAL.DomainServices.Interfaces; diff --git a/PARR.DAL/ParrDalInstaller.cs b/PARR.DAL/ParrDalInstaller.cs index 6a6884ed..a64a40d6 100644 --- a/PARR.DAL/ParrDalInstaller.cs +++ b/PARR.DAL/ParrDalInstaller.cs @@ -1,7 +1,8 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using PARR.DAL.CacheServices; +using PARR.DAL.Cache.Services; +using PARR.DAL.Cache.Services.Base; using PARR.DAL.Configurations.DbSettings; using PARR.DAL.Context; using PARR.DAL.Contracts; @@ -39,7 +40,7 @@ namespace PARR.DAL .UseNpgsql(configuration.GetConnectionString("DefaultConnection")) ); - #region Redis + #region Redis + cache services services.AddStackExchangeRedisCache(opt => { @@ -52,6 +53,8 @@ namespace PARR.DAL services.AddTransient(); + services.AddTransient(); + #endregion #region InfluxDb @@ -139,6 +142,8 @@ namespace PARR.DAL services.AddTransient(); services.AddTransient(); #endregion + + } /// diff --git a/PARR.DAL/Services/Implementations/WeekendDayService.cs b/PARR.DAL/Services/Implementations/WeekendDayService.cs index 8bb97949..549e8bdc 100644 --- a/PARR.DAL/Services/Implementations/WeekendDayService.cs +++ b/PARR.DAL/Services/Implementations/WeekendDayService.cs @@ -1,6 +1,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; -using PARR.DAL.CacheServices; +using PARR.DAL.Cache.Services.Base; using PARR.DAL.Context; using PARR.DAL.Contracts; using PARR.DAL.Models;