| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.Extensions.Caching.Memory; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | |
| | | 5 | | namespace Services.CacheServices.CommercialCategoryLookup; |
| | | 6 | | |
| | | 7 | | public sealed class CommercialCategoryLookupService : ICommercialCategoryLookupService |
| | | 8 | | { |
| | | 9 | | private const string CacheKey = "dict_commercial_categories_distinct_v1"; |
| | 1 | 10 | | private static readonly TimeSpan CacheTtl = TimeSpan.FromHours(24); |
| | 1 | 11 | | private static readonly SemaphoreSlim LoadGate = new(1, 1); |
| | | 12 | | |
| | | 13 | | private readonly ApplicationDbContext _context; |
| | | 14 | | private readonly IMemoryCache _memoryCache; |
| | | 15 | | |
| | 2 | 16 | | public CommercialCategoryLookupService(ApplicationDbContext context, IMemoryCache memoryCache) |
| | | 17 | | { |
| | 2 | 18 | | _context = context; |
| | 2 | 19 | | _memoryCache = memoryCache; |
| | 2 | 20 | | } |
| | | 21 | | |
| | | 22 | | public async Task<IReadOnlyList<string>> SearchAsync( |
| | | 23 | | string? query, |
| | | 24 | | int limit, |
| | | 25 | | CancellationToken cancellationToken = default) |
| | | 26 | | { |
| | 3 | 27 | | if (limit < 1) |
| | | 28 | | { |
| | 0 | 29 | | limit = 1; |
| | | 30 | | } |
| | | 31 | | |
| | 3 | 32 | | if (limit > 200) |
| | | 33 | | { |
| | 0 | 34 | | limit = 200; |
| | | 35 | | } |
| | | 36 | | |
| | 3 | 37 | | var all = await GetAllDistinctAsync(cancellationToken); |
| | 3 | 38 | | if (all.Count == 0) |
| | | 39 | | { |
| | 0 | 40 | | return Array.Empty<string>(); |
| | | 41 | | } |
| | | 42 | | |
| | 3 | 43 | | var normalizedQuery = (query ?? string.Empty).Trim(); |
| | 3 | 44 | | if (normalizedQuery.Length == 0) |
| | | 45 | | { |
| | 2 | 46 | | return all.Take(limit).ToList(); |
| | | 47 | | } |
| | | 48 | | |
| | 1 | 49 | | return all |
| | 3 | 50 | | .Where(c => c.Contains(normalizedQuery, StringComparison.OrdinalIgnoreCase)) |
| | 1 | 51 | | .Take(limit) |
| | 1 | 52 | | .ToList(); |
| | 3 | 53 | | } |
| | | 54 | | |
| | | 55 | | private async Task<IReadOnlyList<string>> GetAllDistinctAsync(CancellationToken cancellationToken) |
| | | 56 | | { |
| | 3 | 57 | | if (_memoryCache.TryGetValue(CacheKey, out IReadOnlyList<string>? cached) && cached is not null) |
| | | 58 | | { |
| | 1 | 59 | | return cached; |
| | | 60 | | } |
| | | 61 | | |
| | 2 | 62 | | await LoadGate.WaitAsync(cancellationToken); |
| | | 63 | | try |
| | | 64 | | { |
| | 2 | 65 | | if (_memoryCache.TryGetValue(CacheKey, out cached) && cached is not null) |
| | | 66 | | { |
| | 0 | 67 | | return cached; |
| | | 68 | | } |
| | | 69 | | |
| | 2 | 70 | | var categories = await _context.Products |
| | 2 | 71 | | .AsNoTracking() |
| | 2 | 72 | | .Where(p => p.CommercialCategory != null && p.CommercialCategory != "") |
| | 2 | 73 | | .Select(p => p.CommercialCategory!) |
| | 2 | 74 | | .Distinct() |
| | 2 | 75 | | .OrderBy(c => c) |
| | 2 | 76 | | .ToListAsync(cancellationToken); |
| | | 77 | | |
| | 2 | 78 | | IReadOnlyList<string> snapshot = categories; |
| | 2 | 79 | | _memoryCache.Set(CacheKey, snapshot, CacheTtl); |
| | 2 | 80 | | return snapshot; |
| | | 81 | | } |
| | | 82 | | finally |
| | | 83 | | { |
| | 2 | 84 | | LoadGate.Release(); |
| | | 85 | | } |
| | 3 | 86 | | } |
| | | 87 | | } |