From 7070c15efe4370acb5bed8a78f34e4c51e1d11f4 Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Tue, 20 Aug 2024 11:56:26 +0800 Subject: [PATCH] Replace AnswerAddedToPostCommand with lock --- .../App/AnswerAddedToPostCommand.cs | 19 ------------------- MyApp.ServiceInterface/QuestionServices.cs | 8 +++++++- 2 files changed, 7 insertions(+), 20 deletions(-) delete mode 100644 MyApp.ServiceInterface/App/AnswerAddedToPostCommand.cs diff --git a/MyApp.ServiceInterface/App/AnswerAddedToPostCommand.cs b/MyApp.ServiceInterface/App/AnswerAddedToPostCommand.cs deleted file mode 100644 index e368294..0000000 --- a/MyApp.ServiceInterface/App/AnswerAddedToPostCommand.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Data; -using ServiceStack; -using ServiceStack.OrmLite; -using MyApp.Data; -using MyApp.ServiceModel; - -namespace MyApp.ServiceInterface.App; - -[Tag(Tags.Answers)] -[Worker(Databases.App)] -public class AnswerAddedToPostCommand(IDbConnection db) : AsyncCommand -{ - protected override async Task RunAsync(AnswerAddedToPost request, CancellationToken token) - { - await db.UpdateAddAsync(() => new Post { - AnswerCount = 1, - }, x => x.Id == request.Id, token:token); - } -} \ No newline at end of file diff --git a/MyApp.ServiceInterface/QuestionServices.cs b/MyApp.ServiceInterface/QuestionServices.cs index 1330ba3..e49e684 100644 --- a/MyApp.ServiceInterface/QuestionServices.cs +++ b/MyApp.ServiceInterface/QuestionServices.cs @@ -189,7 +189,13 @@ public async Task Any(AnswerQuestion request) }; jobs.RunCommand(answer); - jobs.RunCommand(new AnswerAddedToPost { Id = postId }); + + lock (Locks.AppDb) + { + Db.UpdateAdd(() => new Post { + AnswerCount = 1, + }, x => x.Id == postId); + } rendererCache.DeleteCachedQuestionPostHtml(postId);