From 7ac072de0cb4308b903926c591e42d4810f02d67 Mon Sep 17 00:00:00 2001 From: Salih Date: Fri, 22 Mar 2024 00:58:31 +0300 Subject: [PATCH 1/4] Improve clear cache --- .../Docs/Admin/Documents/DocumentAdminAppService.cs | 6 ++---- .../Volo/Docs/Documents/DocumentWithoutDetails.cs | 2 ++ .../Volo/Docs/Documents/IDocumentRepository.cs | 2 ++ .../Volo/Docs/Documents/EFCoreDocumentRepository.cs | 5 +++++ .../Volo/Docs/Documents/MongoDocumentRepository.cs | 10 ++++++++++ 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/modules/docs/src/Volo.Docs.Admin.Application/Volo/Docs/Admin/Documents/DocumentAdminAppService.cs b/modules/docs/src/Volo.Docs.Admin.Application/Volo/Docs/Admin/Documents/DocumentAdminAppService.cs index 9356ba9c38..b93b4f6698 100644 --- a/modules/docs/src/Volo.Docs.Admin.Application/Volo/Docs/Admin/Documents/DocumentAdminAppService.cs +++ b/modules/docs/src/Volo.Docs.Admin.Application/Volo/Docs/Admin/Documents/DocumentAdminAppService.cs @@ -57,7 +57,7 @@ namespace Volo.Docs.Admin.Documents var versionCacheKey = CacheKeyGenerator.GenerateProjectVersionsCacheKey(project); await _versionCache.RemoveAsync(versionCacheKey, true); - var documents = await _documentRepository.GetListByProjectId(project.Id); + var documents = await _documentRepository.GetListWithoutDetailsByProjectId(project.Id); var documentUpdateInfoCacheKeys = documents.Select(document => CacheKeyGenerator.GenerateDocumentUpdateInfoCacheKey( @@ -69,10 +69,8 @@ namespace Volo.Docs.Admin.Documents ); await _documentUpdateCache.RemoveManyAsync(documentUpdateInfoCacheKeys); - - documents.ForEach(document => document.LastCachedTime = DateTime.MinValue); - await _documentRepository.UpdateManyAsync(documents); + await _documentRepository.ClearCachesAsync(project.Id); } public virtual async Task PullAllAsync(PullAllDocumentInput input) diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/DocumentWithoutDetails.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/DocumentWithoutDetails.cs index 8adade5be2..2aaf54aee4 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/DocumentWithoutDetails.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/DocumentWithoutDetails.cs @@ -5,6 +5,8 @@ namespace Volo.Docs.Documents public class DocumentWithoutDetails { public Guid Id { get; set; } + + public string Name { get; set; } public virtual string Version { get; set; } diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentRepository.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentRepository.cs index b1580f379a..7b985b19d8 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentRepository.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentRepository.cs @@ -12,6 +12,8 @@ namespace Volo.Docs.Documents Task> GetUniqueListDocumentInfoAsync(CancellationToken cancellationToken = default); Task> GetListByProjectId(Guid projectId, CancellationToken cancellationToken = default); + + Task ClearCachesAsync(Guid projectId, CancellationToken cancellationToken = default); Task FindAsync(Guid projectId, string name, diff --git a/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/Documents/EFCoreDocumentRepository.cs b/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/Documents/EFCoreDocumentRepository.cs index 700203e7dd..9fe9ecceb0 100644 --- a/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/Documents/EFCoreDocumentRepository.cs +++ b/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/Documents/EFCoreDocumentRepository.cs @@ -55,6 +55,11 @@ namespace Volo.Docs.Documents return await (await GetDbSetAsync()).Where(d => d.ProjectId == projectId).ToListAsync(GetCancellationToken(cancellationToken)); } + public async Task ClearCachesAsync(Guid projectId, CancellationToken cancellationToken = default) + { + await (await GetDbSetAsync()).Where(d => d.ProjectId == projectId).ExecuteUpdateAsync(x => x.SetProperty(d => d.LastCachedTime, DateTime.MinValue), GetCancellationToken(cancellationToken)); + } + public virtual async Task> GetListAsync(Guid? projectId, string version, string name, CancellationToken cancellationToken = default) { return await (await GetDbSetAsync()) diff --git a/modules/docs/src/Volo.Docs.MongoDB/Volo/Docs/Documents/MongoDocumentRepository.cs b/modules/docs/src/Volo.Docs.MongoDB/Volo/Docs/Documents/MongoDocumentRepository.cs index 1cd93545a5..26811a3567 100644 --- a/modules/docs/src/Volo.Docs.MongoDB/Volo/Docs/Documents/MongoDocumentRepository.cs +++ b/modules/docs/src/Volo.Docs.MongoDB/Volo/Docs/Documents/MongoDocumentRepository.cs @@ -53,6 +53,16 @@ namespace Volo.Docs.Documents return await (await GetMongoQueryableAsync(cancellationToken)).Where(d => d.ProjectId == projectId).ToListAsync(GetCancellationToken(cancellationToken)); } + public async Task ClearCachesAsync(Guid projectId, CancellationToken cancellationToken = default) + { + var collection = await GetCollectionAsync(cancellationToken); + await collection.UpdateManyAsync( + Builders.Filter.Eq(x => x.ProjectId, projectId), + Builders.Update.Set(x => x.LastCachedTime, DateTime.MinValue), + cancellationToken: GetCancellationToken(cancellationToken) + ); + } + public virtual async Task FindAsync(Guid projectId, string name, string languageCode, string version, bool includeDetails = true, CancellationToken cancellationToken = default) From 1912165e71cafeeba4f075ca5eea7ef0216aff32 Mon Sep 17 00:00:00 2001 From: Salih Date: Fri, 22 Mar 2024 01:23:25 +0300 Subject: [PATCH 2/4] Add name --- .../Volo/Docs/Documents/EFCoreDocumentRepository.cs | 1 + .../Volo/Docs/Documents/MongoDocumentRepository.cs | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/Documents/EFCoreDocumentRepository.cs b/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/Documents/EFCoreDocumentRepository.cs index 9fe9ecceb0..83d1683368 100644 --- a/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/Documents/EFCoreDocumentRepository.cs +++ b/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/Documents/EFCoreDocumentRepository.cs @@ -28,6 +28,7 @@ namespace Volo.Docs.Documents Version = x.Version, LanguageCode = x.LanguageCode, Format = x.Format, + Name = x.Name }) .ToListAsync(GetCancellationToken(cancellationToken)); } diff --git a/modules/docs/src/Volo.Docs.MongoDB/Volo/Docs/Documents/MongoDocumentRepository.cs b/modules/docs/src/Volo.Docs.MongoDB/Volo/Docs/Documents/MongoDocumentRepository.cs index 26811a3567..b1a3288ac4 100644 --- a/modules/docs/src/Volo.Docs.MongoDB/Volo/Docs/Documents/MongoDocumentRepository.cs +++ b/modules/docs/src/Volo.Docs.MongoDB/Volo/Docs/Documents/MongoDocumentRepository.cs @@ -29,7 +29,8 @@ namespace Volo.Docs.Documents Id = x.Id, Version = x.Version, LanguageCode = x.LanguageCode, - Format = x.Format + Format = x.Format, + Name = x.Name }) .ToListAsync(GetCancellationToken(cancellationToken)); } From 874dd5649d0c6ec2836572651018a0aae14e14ba Mon Sep 17 00:00:00 2001 From: Salih Date: Fri, 22 Mar 2024 01:32:41 +0300 Subject: [PATCH 3/4] Add test --- .../Volo/Docs/DocumentRepository_Tests.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/modules/docs/test/Volo.Docs.TestBase/Volo/Docs/DocumentRepository_Tests.cs b/modules/docs/test/Volo.Docs.TestBase/Volo/Docs/DocumentRepository_Tests.cs index ed0769ccc1..a1cdaa6269 100644 --- a/modules/docs/test/Volo.Docs.TestBase/Volo/Docs/DocumentRepository_Tests.cs +++ b/modules/docs/test/Volo.Docs.TestBase/Volo/Docs/DocumentRepository_Tests.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Shouldly; using Volo.Abp.Modularity; using Volo.Docs.Documents; @@ -34,5 +35,13 @@ namespace Volo.Docs (await DocumentRepository.GetListAsync()).ShouldBeEmpty(); } + + [Fact] + public async Task ClearCachesAsync() + { + await DocumentRepository.ClearCachesAsync(DocsTestData.ProjectId); + var documentsAfterClear = await DocumentRepository.GetListByProjectId(DocsTestData.ProjectId); + documentsAfterClear.ForEach(d => d.LastCachedTime.ShouldBe(DateTime.MinValue)); + } } } From ecd55789d821089be1916ed938052e8b0cdcf3f3 Mon Sep 17 00:00:00 2001 From: Salih Date: Fri, 22 Mar 2024 14:54:41 +0300 Subject: [PATCH 4/4] Update method signature --- .../Volo/Docs/Admin/Documents/DocumentAdminAppService.cs | 2 +- .../Volo/Docs/Documents/IDocumentRepository.cs | 3 ++- .../Volo/Docs/Documents/EFCoreDocumentRepository.cs | 5 +++-- .../Volo/Docs/Documents/MongoDocumentRepository.cs | 5 +++-- .../Volo.Docs.TestBase/Volo/Docs/DocumentRepository_Tests.cs | 4 ++-- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/modules/docs/src/Volo.Docs.Admin.Application/Volo/Docs/Admin/Documents/DocumentAdminAppService.cs b/modules/docs/src/Volo.Docs.Admin.Application/Volo/Docs/Admin/Documents/DocumentAdminAppService.cs index b93b4f6698..8e70be19e4 100644 --- a/modules/docs/src/Volo.Docs.Admin.Application/Volo/Docs/Admin/Documents/DocumentAdminAppService.cs +++ b/modules/docs/src/Volo.Docs.Admin.Application/Volo/Docs/Admin/Documents/DocumentAdminAppService.cs @@ -70,7 +70,7 @@ namespace Volo.Docs.Admin.Documents await _documentUpdateCache.RemoveManyAsync(documentUpdateInfoCacheKeys); - await _documentRepository.ClearCachesAsync(project.Id); + await _documentRepository.UpdateProjectLastCachedTimeAsync(project.Id, DateTime.MinValue); } public virtual async Task PullAllAsync(PullAllDocumentInput input) diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentRepository.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentRepository.cs index 7b985b19d8..7dc8f13147 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentRepository.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentRepository.cs @@ -13,7 +13,8 @@ namespace Volo.Docs.Documents Task> GetListByProjectId(Guid projectId, CancellationToken cancellationToken = default); - Task ClearCachesAsync(Guid projectId, CancellationToken cancellationToken = default); + Task UpdateProjectLastCachedTimeAsync(Guid projectId, DateTime cachedTime, + CancellationToken cancellationToken = default); Task FindAsync(Guid projectId, string name, diff --git a/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/Documents/EFCoreDocumentRepository.cs b/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/Documents/EFCoreDocumentRepository.cs index 83d1683368..1120e36d01 100644 --- a/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/Documents/EFCoreDocumentRepository.cs +++ b/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/Documents/EFCoreDocumentRepository.cs @@ -56,9 +56,10 @@ namespace Volo.Docs.Documents return await (await GetDbSetAsync()).Where(d => d.ProjectId == projectId).ToListAsync(GetCancellationToken(cancellationToken)); } - public async Task ClearCachesAsync(Guid projectId, CancellationToken cancellationToken = default) + public async Task UpdateProjectLastCachedTimeAsync(Guid projectId, DateTime cachedTime, + CancellationToken cancellationToken = default) { - await (await GetDbSetAsync()).Where(d => d.ProjectId == projectId).ExecuteUpdateAsync(x => x.SetProperty(d => d.LastCachedTime, DateTime.MinValue), GetCancellationToken(cancellationToken)); + await (await GetDbSetAsync()).Where(d => d.ProjectId == projectId).ExecuteUpdateAsync(x => x.SetProperty(d => d.LastCachedTime, cachedTime), GetCancellationToken(cancellationToken)); } public virtual async Task> GetListAsync(Guid? projectId, string version, string name, CancellationToken cancellationToken = default) diff --git a/modules/docs/src/Volo.Docs.MongoDB/Volo/Docs/Documents/MongoDocumentRepository.cs b/modules/docs/src/Volo.Docs.MongoDB/Volo/Docs/Documents/MongoDocumentRepository.cs index b1a3288ac4..9a3750deb0 100644 --- a/modules/docs/src/Volo.Docs.MongoDB/Volo/Docs/Documents/MongoDocumentRepository.cs +++ b/modules/docs/src/Volo.Docs.MongoDB/Volo/Docs/Documents/MongoDocumentRepository.cs @@ -54,12 +54,13 @@ namespace Volo.Docs.Documents return await (await GetMongoQueryableAsync(cancellationToken)).Where(d => d.ProjectId == projectId).ToListAsync(GetCancellationToken(cancellationToken)); } - public async Task ClearCachesAsync(Guid projectId, CancellationToken cancellationToken = default) + public async Task UpdateProjectLastCachedTimeAsync(Guid projectId, DateTime cachedTime, + CancellationToken cancellationToken = default) { var collection = await GetCollectionAsync(cancellationToken); await collection.UpdateManyAsync( Builders.Filter.Eq(x => x.ProjectId, projectId), - Builders.Update.Set(x => x.LastCachedTime, DateTime.MinValue), + Builders.Update.Set(x => x.LastCachedTime, cachedTime), cancellationToken: GetCancellationToken(cancellationToken) ); } diff --git a/modules/docs/test/Volo.Docs.TestBase/Volo/Docs/DocumentRepository_Tests.cs b/modules/docs/test/Volo.Docs.TestBase/Volo/Docs/DocumentRepository_Tests.cs index a1cdaa6269..742f80fe17 100644 --- a/modules/docs/test/Volo.Docs.TestBase/Volo/Docs/DocumentRepository_Tests.cs +++ b/modules/docs/test/Volo.Docs.TestBase/Volo/Docs/DocumentRepository_Tests.cs @@ -37,9 +37,9 @@ namespace Volo.Docs } [Fact] - public async Task ClearCachesAsync() + public async Task UpdateProjectLastCachedTimeAsync() { - await DocumentRepository.ClearCachesAsync(DocsTestData.ProjectId); + await DocumentRepository.UpdateProjectLastCachedTimeAsync(DocsTestData.ProjectId, DateTime.MinValue); var documentsAfterClear = await DocumentRepository.GetListByProjectId(DocsTestData.ProjectId); documentsAfterClear.ForEach(d => d.LastCachedTime.ShouldBe(DateTime.MinValue)); }