feat(dal): CacheService - структура + CacheMatchingStatusService

This commit is contained in:
Mikhail Trubnikov
2026-01-15 15:01:56 +10:00
parent d89347787e
commit b5c25f3110
12 changed files with 150 additions and 9 deletions

View File

@@ -0,0 +1,61 @@
namespace PARR.DAL.Cache.Services.Base
{
public interface IRedisCacheService
{
/// <summary>
/// Получить кэшированные данные
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
T? GetCachedData<T>(string key);
/// <summary>
/// Получить кэшированные данные асинхронно
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
Task<T?> GetCachedDataAsync<T>(string key);
/// <summary>
/// Добавить в кэш данные
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="data"></param>
/// <param name="cacheDuration"></param>
void SetCachedData<T>(string key, T data, TimeSpan cacheDuration);
/// <summary>
/// Добавить в кэш данные асинхронно
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="data"></param>
/// <param name="cacheDuration"></param>
/// <returns></returns>
Task SetCachedDataAsync<T>(string key, T data, TimeSpan cacheDuration);
/// <summary>
/// Удалить кэшированные данные
/// </summary>
/// <param name="key"></param>
void DeleteCachedData(string key);
/// <summary>
/// Удалить кэшированные данные асинхронно
/// </summary>
/// <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);
}
}

View File

@@ -0,0 +1,96 @@
using Microsoft.Extensions.Caching.Distributed;
using PARR.DAL.Migrations;
using System.Text.Json;
namespace PARR.DAL.Cache.Services.Base
{
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);
}
public async Task DeleteCachedDataAsync(string key)
{
await cache.RemoveAsync(key);
}
public void DeleteCachedData(string key)
{
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;
}
}
}

View 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();
}
}
}

View 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);
}
}