| | | 1 | | using Microsoft.Extensions.Caching.Memory; |
| | | 2 | | |
| | | 3 | | namespace Services.LabelServices; |
| | | 4 | | |
| | | 5 | | public class LabelExistenceService : ILabelExistenceService |
| | | 6 | | { |
| | | 7 | | private const string PerLabelCacheKeyPrefix = "label_exists_"; |
| | | 8 | | private const string FolderIndexCacheKey = "labels_folder_index_v1"; |
| | | 9 | | |
| | 1 | 10 | | private static readonly TimeSpan PerLabelCacheExpiration = TimeSpan.FromHours(1); |
| | 1 | 11 | | private static readonly TimeSpan FolderIndexCacheExpiration = TimeSpan.FromMinutes(10); |
| | | 12 | | |
| | | 13 | | private readonly ILabelFileStorage _labelFileStorage; |
| | | 14 | | private readonly IMemoryCache _cache; |
| | | 15 | | |
| | 7 | 16 | | public LabelExistenceService(ILabelFileStorage labelFileStorage, IMemoryCache cache) |
| | | 17 | | { |
| | 7 | 18 | | _labelFileStorage = labelFileStorage; |
| | 7 | 19 | | _cache = cache; |
| | 7 | 20 | | } |
| | | 21 | | |
| | | 22 | | public async Task<IReadOnlyDictionary<string, bool>> CheckBatchAsync(IReadOnlyList<string> shipmentNumbers) |
| | | 23 | | { |
| | 6 | 24 | | var results = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase); |
| | 6 | 25 | | if (shipmentNumbers.Count == 0) |
| | | 26 | | { |
| | 1 | 27 | | return results; |
| | | 28 | | } |
| | | 29 | | |
| | 5 | 30 | | var distinctNumbers = shipmentNumbers |
| | 108 | 31 | | .Where(s => !string.IsNullOrWhiteSpace(s)) |
| | 5 | 32 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 5 | 33 | | .Take(100) |
| | 5 | 34 | | .ToList(); |
| | | 35 | | |
| | 5 | 36 | | var toResolve = new List<string>(); |
| | 226 | 37 | | foreach (var shipmentNumber in distinctNumbers) |
| | | 38 | | { |
| | 108 | 39 | | var cacheKey = $"{PerLabelCacheKeyPrefix}{shipmentNumber}"; |
| | 108 | 40 | | if (_cache.TryGetValue(cacheKey, out bool cachedExists)) |
| | | 41 | | { |
| | 2 | 42 | | results[shipmentNumber] = cachedExists; |
| | | 43 | | } |
| | | 44 | | else |
| | | 45 | | { |
| | 106 | 46 | | toResolve.Add(shipmentNumber); |
| | | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | 5 | 50 | | if (toResolve.Count == 0) |
| | | 51 | | { |
| | 1 | 52 | | return results; |
| | | 53 | | } |
| | | 54 | | |
| | 4 | 55 | | var folderIndex = await GetFolderIndexAsync(); |
| | 4 | 56 | | var cacheOptions = new MemoryCacheEntryOptions |
| | 4 | 57 | | { |
| | 4 | 58 | | AbsoluteExpirationRelativeToNow = PerLabelCacheExpiration, |
| | 4 | 59 | | SlidingExpiration = TimeSpan.FromMinutes(30) |
| | 4 | 60 | | }; |
| | | 61 | | |
| | 220 | 62 | | foreach (var shipmentNumber in toResolve) |
| | | 63 | | { |
| | 106 | 64 | | var exists = folderIndex.Contains(shipmentNumber); |
| | 106 | 65 | | results[shipmentNumber] = exists; |
| | 106 | 66 | | _cache.Set($"{PerLabelCacheKeyPrefix}{shipmentNumber}", exists, cacheOptions); |
| | | 67 | | } |
| | | 68 | | |
| | 4 | 69 | | return results; |
| | 6 | 70 | | } |
| | | 71 | | |
| | | 72 | | public void InvalidateShipment(string shipmentNumber) |
| | | 73 | | { |
| | 1 | 74 | | if (string.IsNullOrWhiteSpace(shipmentNumber)) |
| | | 75 | | { |
| | 0 | 76 | | return; |
| | | 77 | | } |
| | | 78 | | |
| | 1 | 79 | | _cache.Remove($"{PerLabelCacheKeyPrefix}{shipmentNumber}"); |
| | 1 | 80 | | _cache.Remove(FolderIndexCacheKey); |
| | 1 | 81 | | } |
| | | 82 | | |
| | | 83 | | public void SetShipmentExists(string shipmentNumber, bool exists) |
| | | 84 | | { |
| | 1 | 85 | | if (string.IsNullOrWhiteSpace(shipmentNumber)) |
| | | 86 | | { |
| | 0 | 87 | | return; |
| | | 88 | | } |
| | | 89 | | |
| | 1 | 90 | | _cache.Set( |
| | 1 | 91 | | $"{PerLabelCacheKeyPrefix}{shipmentNumber}", |
| | 1 | 92 | | exists, |
| | 1 | 93 | | new MemoryCacheEntryOptions |
| | 1 | 94 | | { |
| | 1 | 95 | | AbsoluteExpirationRelativeToNow = PerLabelCacheExpiration, |
| | 1 | 96 | | SlidingExpiration = TimeSpan.FromMinutes(30) |
| | 1 | 97 | | }); |
| | 1 | 98 | | } |
| | | 99 | | |
| | | 100 | | public async Task<int> RefreshFolderIndexAsync(CancellationToken cancellationToken = default) |
| | | 101 | | { |
| | | 102 | | _ = cancellationToken; |
| | 0 | 103 | | _cache.Remove(FolderIndexCacheKey); |
| | 0 | 104 | | var index = await LoadFolderIndexAsync(); |
| | 0 | 105 | | return index.Count; |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | private async Task<HashSet<string>> GetFolderIndexAsync() |
| | | 109 | | { |
| | 4 | 110 | | if (_cache.TryGetValue(FolderIndexCacheKey, out HashSet<string>? cached) && cached is not null) |
| | | 111 | | { |
| | 3 | 112 | | return cached; |
| | | 113 | | } |
| | | 114 | | |
| | 1 | 115 | | return await LoadFolderIndexAsync(); |
| | 4 | 116 | | } |
| | | 117 | | |
| | | 118 | | private async Task<HashSet<string>> LoadFolderIndexAsync() |
| | | 119 | | { |
| | 1 | 120 | | var index = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | | 121 | | try |
| | | 122 | | { |
| | 1 | 123 | | var files = await _labelFileStorage.ListAsync(); |
| | 6 | 124 | | foreach (var file in files) |
| | | 125 | | { |
| | 2 | 126 | | if (file.IsDirectory || !file.Name.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase)) |
| | | 127 | | { |
| | | 128 | | continue; |
| | | 129 | | } |
| | | 130 | | |
| | 1 | 131 | | index.Add(file.Name[..^4]); |
| | | 132 | | } |
| | 1 | 133 | | } |
| | 0 | 134 | | catch (Exception ex) |
| | | 135 | | { |
| | 0 | 136 | | Console.WriteLine($"Ошибка при построении индекса этикеток: {ex.Message}"); |
| | 0 | 137 | | } |
| | | 138 | | |
| | 1 | 139 | | _cache.Set(FolderIndexCacheKey, index, FolderIndexCacheExpiration); |
| | 1 | 140 | | return index; |
| | 1 | 141 | | } |
| | | 142 | | } |