forked from itsecd/cloud-development
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoftwareProjectService.cs
More file actions
90 lines (78 loc) · 3.02 KB
/
SoftwareProjectService.cs
File metadata and controls
90 lines (78 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.Text.Json;
using Microsoft.Extensions.Caching.Distributed;
using ProjectGenerator.Domain.Models;
namespace ProjectGenerator.Api.Services;
/// <summary>
/// Сервис программных проектов с кэшированием
/// </summary>
/// <param name="generator">Генератор программных проектов</param>
/// <param name="cache">Распределённый кэш</param>
/// <param name="configuration">Конфигурация приложения</param>
/// <param name="logger">Логгер</param>
public class SoftwareProjectService(
ISoftwareProjectGenerator generator,
IDistributedCache cache,
IConfiguration configuration,
ILogger<SoftwareProjectService> logger) : ISoftwareProjectService
{
private const string CacheKeyPrefix = "software-project";
private readonly TimeSpan _cacheTtl = TimeSpan.FromMinutes(configuration.GetValue("CacheTtlMinutes", 15));
/// <inheritdoc />
public async Task<SoftwareProject> GetOrGenerate(int id)
{
var cacheKey = $"{CacheKeyPrefix}:{id}";
var cached = await GetFromCache(cacheKey);
if (cached is not null)
{
return cached;
}
logger.LogInformation("Cache miss for id {Id}, generating new data", id);
var project = generator.Generate(id);
await SetToCache(cacheKey, project);
return project;
}
/// <summary>
/// Получение программного проекта из кэша
/// </summary>
/// <param name="cacheKey">Ключ кэша</param>
/// <returns>Программный проект или null</returns>
private async Task<SoftwareProject?> GetFromCache(string cacheKey)
{
try
{
var cached = await cache.GetStringAsync(cacheKey);
if (cached is null)
{
return null;
}
logger.LogInformation("Cache hit for key {CacheKey}", cacheKey);
return JsonSerializer.Deserialize<SoftwareProject>(cached);
}
catch (Exception ex)
{
logger.LogWarning(ex, "Failed to read from cache for key {CacheKey}", cacheKey);
return null;
}
}
/// <summary>
/// Сохранение программного проекта в кэш
/// </summary>
/// <param name="cacheKey">Ключ кэша</param>
/// <param name="project">Программный проект</param>
private async Task SetToCache(string cacheKey, SoftwareProject project)
{
try
{
var json = JsonSerializer.Serialize(project);
await cache.SetStringAsync(cacheKey, json, new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = _cacheTtl
});
logger.LogInformation("Cached data for key {CacheKey}", cacheKey);
}
catch (Exception ex)
{
logger.LogWarning(ex, "Failed to write to cache for key {CacheKey}", cacheKey);
}
}
}