< Summary

Information
Class: Services.BitrixStockServices.BitrixStockSyncReportStore
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/BitrixStockServices/BitrixStockSyncReportStore.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 22
Coverable lines: 22
Total lines: 57
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
Set(...)0%2040%
Get(...)0%7280%
CacheKey(...)100%210%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/BitrixStockServices/BitrixStockSyncReportStore.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2using Microsoft.Extensions.Caching.Memory;
 3
 4namespace Services.BitrixStockServices;
 5
 6/// <summary>Кэш отчётов синхронизации (singleton — доступен и HTTP, и Hangfire в том же процессе).</summary>
 7public sealed class BitrixStockSyncReportStore
 8{
 09    private readonly ConcurrentDictionary<string, (BitrixStockSyncReportDto Report, DateTime ExpiresUtc)> _reports = new
 10    private readonly IMemoryCache _memoryCache;
 11
 012    public BitrixStockSyncReportStore(IMemoryCache memoryCache)
 13    {
 014        _memoryCache = memoryCache;
 015    }
 16
 17    public void Set(string reportId, BitrixStockSyncReportDto report, TimeSpan ttl)
 18    {
 019        if (string.IsNullOrWhiteSpace(reportId) || report == null)
 20        {
 021            return;
 22        }
 23
 024        var id = reportId.Trim();
 025        var expires = DateTime.UtcNow.Add(ttl);
 026        _reports[id] = (report, expires);
 027        _memoryCache.Set(CacheKey(id), report, ttl);
 028    }
 29
 30    public BitrixStockSyncReportDto? Get(string reportId)
 31    {
 032        if (string.IsNullOrWhiteSpace(reportId))
 33        {
 034            return null;
 35        }
 36
 037        var id = reportId.Trim();
 038        if (_memoryCache.TryGetValue(CacheKey(id), out BitrixStockSyncReportDto? cached))
 39        {
 040            return cached;
 41        }
 42
 043        if (_reports.TryGetValue(id, out var entry))
 44        {
 045            if (entry.ExpiresUtc > DateTime.UtcNow)
 46            {
 047                return entry.Report;
 48            }
 49
 050            _reports.TryRemove(id, out _);
 51        }
 52
 053        return null;
 54    }
 55
 056    private static string CacheKey(string reportId) => $"bitrix-sync-report:{reportId}";
 57}