|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | | -using System.Threading.Tasks; |
5 | | -using System.Timers; |
6 | | -using Microsoft.GS.DPS.Storage.Document; |
7 | | -using Timers =System.Timers; |
8 | | - |
9 | | -namespace Microsoft.GS.DPS.API.UserInterface |
10 | | -{ |
11 | | - public class DataCacheManager |
12 | | - { |
13 | | - private readonly DocumentRepository _documentRepository; |
14 | | - private Dictionary<string, List<string>> _keywordCache; |
15 | | - private readonly Timers.Timer _cacheTimer; |
16 | | - private readonly object _cacheLock = new object(); |
17 | | - |
18 | | - public DataCacheManager(DocumentRepository documentRepository) |
19 | | - { |
20 | | - _documentRepository = documentRepository; |
21 | | - _keywordCache = new Dictionary<string, List<string>>(); |
22 | | - _cacheTimer = new Timers.Timer(5 * 60 * 1000); // 5 minutes |
23 | | - _cacheTimer.Elapsed += async (sender, e) => await RefreshCacheAsync(); |
24 | | - _cacheTimer.Start(); |
25 | | - } |
26 | | - |
27 | | - public async Task<Dictionary<string, List<string>>> GetConsolidatedKeywordsAsync() |
28 | | - { |
29 | | - if (_keywordCache.Count == 0) |
30 | | - { |
31 | | - await RefreshCacheAsync(); |
32 | | - } |
33 | | - |
34 | | - lock (_cacheLock) |
35 | | - { |
36 | | - return new Dictionary<string, List<string>>(_keywordCache); |
37 | | - } |
38 | | - } |
39 | | - |
40 | | - public async Task RefreshCacheAsync() |
41 | | - { |
42 | | - var consolidatedKeywords = new Dictionary<string, List<string>>(); |
43 | | - var documents = await _documentRepository.GetAllDocuments(); |
44 | | - |
45 | | - foreach (var document in documents) |
46 | | - { |
47 | | - if (document.Keywords != null) |
48 | | - { |
49 | | - foreach (var keywordDict in document.Keywords) |
50 | | - { |
51 | | - if (!consolidatedKeywords.ContainsKey(keywordDict.Key)) |
52 | | - { |
53 | | - consolidatedKeywords[keywordDict.Key] = new List<string>(); |
54 | | - } |
55 | | - |
56 | | - var values = keywordDict.Value.Split(',').Select(v => v.Trim()).ToArray(); |
57 | | - |
58 | | - foreach (var value in values) |
59 | | - { |
60 | | - if (!consolidatedKeywords[keywordDict.Key].Contains(value)) |
61 | | - { |
62 | | - consolidatedKeywords[keywordDict.Key].Add(value); |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - consolidatedKeywords[keywordDict.Key] = consolidatedKeywords[keywordDict.Key].OrderBy(v => v).ToList(); |
67 | | - } |
68 | | - } |
69 | | - } |
70 | | - |
71 | | - consolidatedKeywords = consolidatedKeywords.OrderBy(k => k.Key).ToDictionary(k => k.Key, v => v.Value); |
72 | | - |
73 | | - lock (_cacheLock) |
74 | | - { |
75 | | - _keywordCache = consolidatedKeywords; |
76 | | - } |
77 | | - } |
78 | | - |
79 | | - public void ManualRefresh() |
80 | | - { |
81 | | - _cacheTimer.Stop(); |
82 | | - _cacheTimer.Start(); |
83 | | - Task.Run(async () => await RefreshCacheAsync()); |
84 | | - } |
85 | | - } |
86 | | -} |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using System.Timers; |
| 6 | +using Microsoft.GS.DPS.Storage.Document; |
| 7 | +using Timers =System.Timers; |
| 8 | + |
| 9 | +namespace Microsoft.GS.DPS.API.UserInterface |
| 10 | +{ |
| 11 | + public class DataCacheManager |
| 12 | + { |
| 13 | + private readonly DocumentRepository _documentRepository; |
| 14 | + private Dictionary<string, List<string>> _keywordCache; |
| 15 | + private readonly Timers.Timer _cacheTimer; |
| 16 | + private readonly object _cacheLock = new object(); |
| 17 | + |
| 18 | + public DataCacheManager(DocumentRepository documentRepository) |
| 19 | + { |
| 20 | + _documentRepository = documentRepository; |
| 21 | + _keywordCache = new Dictionary<string, List<string>>(); |
| 22 | + _cacheTimer = new Timers.Timer(5 * 60 * 1000); // 5 minutes |
| 23 | + _cacheTimer.Elapsed += async (sender, e) => await RefreshCacheAsync(); |
| 24 | + _cacheTimer.Start(); |
| 25 | + } |
| 26 | + |
| 27 | + public async Task<Dictionary<string, List<string>>> GetConsolidatedKeywordsAsync() |
| 28 | + { |
| 29 | + if (_keywordCache.Count == 0) |
| 30 | + { |
| 31 | + await RefreshCacheAsync(); |
| 32 | + } |
| 33 | + |
| 34 | + lock (_cacheLock) |
| 35 | + { |
| 36 | + return new Dictionary<string, List<string>>(_keywordCache); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public async Task RefreshCacheAsync() |
| 41 | + { |
| 42 | + var consolidatedKeywords = new Dictionary<string, List<string>>(); |
| 43 | + var documents = await _documentRepository.GetAllDocuments(); |
| 44 | + |
| 45 | + foreach (var document in documents.Where(d => d.Keywords != null)) |
| 46 | + { |
| 47 | + foreach (var keywordDict in document.Keywords) |
| 48 | + { |
| 49 | + if (!consolidatedKeywords.ContainsKey(keywordDict.Key)) |
| 50 | + { |
| 51 | + consolidatedKeywords[keywordDict.Key] = new List<string>(); |
| 52 | + } |
| 53 | + |
| 54 | + var values = keywordDict.Value.Split(',').Select(v => v.Trim()).ToArray(); |
| 55 | + |
| 56 | + foreach (var value in values) |
| 57 | + { |
| 58 | + if (!consolidatedKeywords[keywordDict.Key].Contains(value)) |
| 59 | + { |
| 60 | + consolidatedKeywords[keywordDict.Key].Add(value); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + consolidatedKeywords[keywordDict.Key] = consolidatedKeywords[keywordDict.Key].OrderBy(v => v).ToList(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + consolidatedKeywords = consolidatedKeywords.OrderBy(k => k.Key).ToDictionary(k => k.Key, v => v.Value); |
| 69 | + |
| 70 | + lock (_cacheLock) |
| 71 | + { |
| 72 | + _keywordCache = consolidatedKeywords; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public void ManualRefresh() |
| 77 | + { |
| 78 | + _cacheTimer.Stop(); |
| 79 | + _cacheTimer.Start(); |
| 80 | + Task.Run(async () => await RefreshCacheAsync()); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments