feat(dal): CacheService - структура + CacheMatchingStatusService
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
14
PARR.DAL/Cache/Models/Base/IBaseCache.cs
Normal file
14
PARR.DAL/Cache/Models/Base/IBaseCache.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace PARR.DAL.Cache.Models.Base
|
||||
{
|
||||
/// <summary>
|
||||
/// Базовый интерфейс для КЭШ моделей
|
||||
/// </summary>
|
||||
public interface IBaseCache<T>
|
||||
{
|
||||
public T Data { get; set; }
|
||||
|
||||
public DateTimeOffset Timestamp { get; set; }
|
||||
|
||||
public string Source { get; set; }
|
||||
}
|
||||
}
|
||||
27
PARR.DAL/Cache/Models/MatchingStatus.cs
Normal file
27
PARR.DAL/Cache/Models/MatchingStatus.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using PARR.Constants;
|
||||
using PARR.DAL.Cache.Models.Base;
|
||||
|
||||
namespace PARR.DAL.Cache.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// КЭШ модель, состояние matching`a
|
||||
/// </summary>
|
||||
public class MatchingStatus : IBaseCache<MatchingStatusDto>
|
||||
{
|
||||
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; }
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace PARR.DAL.CacheServices
|
||||
namespace PARR.DAL.Cache.Services.Base
|
||||
{
|
||||
public interface IRedisCacheService
|
||||
{
|
||||
@@ -49,5 +49,13 @@
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
Task DeleteCachedDataAsync(string key);
|
||||
|
||||
/// <summary>
|
||||
/// Сгенерировать уникальный ключ
|
||||
/// </summary>
|
||||
/// <param name="keyParts"></param>
|
||||
/// <param name="isUseHash"></param>
|
||||
/// <returns></returns>
|
||||
string GetKey(string[] keyParts, bool isUseHash = false);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
45
PARR.DAL/Cache/Services/CacheMatchingStatusService.cs
Normal file
45
PARR.DAL/Cache/Services/CacheMatchingStatusService.cs
Normal file
@@ -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<MatchingStatus?> GetDataByJobGroupIdAsync(Guid jobGroupId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<MatchingStatus?> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
19
PARR.DAL/Cache/Services/ICacheMatchingStatusService.cs
Normal file
19
PARR.DAL/Cache/Services/ICacheMatchingStatusService.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using PARR.DAL.Cache.Models;
|
||||
|
||||
namespace PARR.DAL.Cache.Services
|
||||
{
|
||||
public interface ICacheMatchingStatusService
|
||||
{
|
||||
Task<MatchingStatus?> GetDataByJobIdAsync(Guid jobId);
|
||||
|
||||
Task<MatchingStatus?> GetDataByJobGroupIdAsync(Guid jobGroupId);
|
||||
|
||||
Task SetCahcheByJobIdAsync(Guid jobId, MatchingStatusDto data);
|
||||
|
||||
Task SetCahcheByJobGroupIdAsync(Guid jobGroupId, MatchingStatusDto data);
|
||||
|
||||
Task DeleteCacheByJobIdAsync(Guid jobId);
|
||||
|
||||
Task DeleteCacheByJobGroupIdAsync(Guid jobGroupId);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<IRedisCacheService, RedisCacheService>();
|
||||
|
||||
services.AddTransient<ICacheMatchingStatusService, CacheMatchingStatusService>();
|
||||
|
||||
#endregion
|
||||
|
||||
#region InfluxDb
|
||||
@@ -139,6 +142,8 @@ namespace PARR.DAL
|
||||
services.AddTransient<IShortcodesService, ShortcodesService>();
|
||||
services.AddTransient<IUnitFilterService, UnitFilterService>();
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user