|
5 | 5 | using FluentValidation;
|
6 | 6 | using Foundatio.Repositories;
|
7 | 7 | using Foundatio.Repositories.Models;
|
| 8 | +using Foundatio.Repositories.Options; |
8 | 9 | using Nest;
|
9 | 10 |
|
10 | 11 | namespace Exceptionless.Core.Repositories;
|
@@ -36,13 +37,13 @@ private void OnDocumentsChanging(object sender, DocumentsChangeEventArgs<Project
|
36 | 37 | if (configCacheValue.HasValue)
|
37 | 38 | return configCacheValue.Value;
|
38 | 39 |
|
39 |
| - var project = await FindOneAsync(q => q.Id(projectId).Include(p => p.Configuration, p => p.OrganizationId)); |
40 |
| - if (project?.Document is null) |
| 40 | + var project = await GetByIdAsync(projectId, o => o.ReadCache().Include(p => p.Configuration, p => p.OrganizationId)); |
| 41 | + if (project is null) |
41 | 42 | return null;
|
42 | 43 |
|
43 |
| - await Cache.AddAsync(cacheKey, project.Document); |
44 |
| - |
45 |
| - return project.Document; |
| 44 | + // NOTE: We might read from cache, but we want to save a limited subset of data. |
| 45 | + await Cache.AddAsync(cacheKey, ToCachedProjectConfig(project)); |
| 46 | + return project; |
46 | 47 | }
|
47 | 48 |
|
48 | 49 | public Task<CountResult> GetCountByOrganizationIdAsync(string organizationId)
|
@@ -88,17 +89,42 @@ public async Task IncrementNextSummaryEndOfDayTicksAsync(IReadOnlyCollection<Pro
|
88 | 89 | await InvalidateCacheAsync(projects);
|
89 | 90 | }
|
90 | 91 |
|
| 92 | + protected override async Task AddDocumentsToCacheAsync(ICollection<FindHit<Project>> findHits, ICommandOptions options, bool isDirtyRead) |
| 93 | + { |
| 94 | + await base.AddDocumentsToCacheAsync(findHits, options, isDirtyRead); |
| 95 | + |
| 96 | + var cacheEntries = new Dictionary<string, Project>(); |
| 97 | + foreach (var project in findHits.Select(hit => hit.Document).Where(d => !String.IsNullOrEmpty(d?.Id))) |
| 98 | + cacheEntries.Add(ConfigCacheKey(project.Id), ToCachedProjectConfig(project)); |
| 99 | + |
| 100 | + // NOTE: We call SetAllAsync instead of AddDocumentsToCacheWithKeyAsync due to our repo method gets the value directly from cache. |
| 101 | + if (cacheEntries.Count > 0) |
| 102 | + await Cache.SetAllAsync(cacheEntries, options.GetExpiresIn()); |
| 103 | + } |
| 104 | + |
91 | 105 | protected override async Task InvalidateCacheAsync(IReadOnlyCollection<ModifiedDocument<Project>> documents, ChangeType? changeType = null)
|
92 | 106 | {
|
93 |
| - var organizations = documents.Select(d => d.Value.OrganizationId).Distinct().Where(id => !String.IsNullOrEmpty(id)); |
94 |
| - await Cache.RemoveAllAsync(organizations.Select(id => $"count:{OrganizationCacheKey(id)}")); |
| 107 | + var originalAndModifiedDocuments = documents.UnionOriginalAndModified(); |
| 108 | + |
| 109 | + // Invalidate GetCountByOrganizationIdAsync |
| 110 | + var organizationIds = originalAndModifiedDocuments.Select(d => d.OrganizationId).Distinct().Where(id => !String.IsNullOrEmpty(id)); |
| 111 | + var countByOrganizationKeysToRemove = organizationIds.Select(id => $"count:{OrganizationCacheKey(id)}"); |
| 112 | + await Cache.RemoveAllAsync(countByOrganizationKeysToRemove); |
95 | 113 |
|
96 |
| - var configCacheKeys = documents.Select(d => ConfigCacheKey(d.Value.Id)); |
97 |
| - await Cache.RemoveAllAsync(configCacheKeys); |
| 114 | + var configKeysToRemove = originalAndModifiedDocuments.Select(d => ConfigCacheKey(d.Id)).Distinct(); |
| 115 | + await Cache.RemoveAllAsync(configKeysToRemove); |
98 | 116 |
|
99 | 117 | await base.InvalidateCacheAsync(documents, changeType);
|
100 | 118 | }
|
101 | 119 |
|
| 120 | + private static Project ToCachedProjectConfig(Project project) |
| 121 | + { |
| 122 | + return new Project |
| 123 | + { |
| 124 | + Id = project.Id, OrganizationId = project.OrganizationId, Configuration = project.Configuration |
| 125 | + }; |
| 126 | + } |
| 127 | + |
102 | 128 | private static string ConfigCacheKey(string projectId) => String.Concat("config:", projectId);
|
103 | 129 | private static string OrganizationCacheKey(string organizationId) => String.Concat("Organization:", organizationId);
|
104 | 130 | }
|
0 commit comments