Skip to content

Commit

Permalink
add mutex (#634)
Browse files Browse the repository at this point in the history
# Description

This PR includes the following proposed change(s):

- add mutex for appending more items in cache
  • Loading branch information
peggy-quartech authored Dec 15, 2023
1 parent e2e1179 commit f16d060
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Spd.Manager.Licence/PersonalLicenceAppContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public record WorkerLicenceCreateResponse

public record LicenceAppDocumentsCache
{
public List<LicAppFileInfo> LicAppFileInfos { get; set; } = new List<LicAppFileInfo>();
public List<LicAppFileInfo> Items { get; set; } = new List<LicAppFileInfo>();
}
public record LicAppFileInfo
{
Expand Down
4 changes: 2 additions & 2 deletions src/Spd.Manager.Licence/PersonalLicenceAppManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AutoMapper;
using AutoMapper;
using MediatR;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -166,7 +166,7 @@ public async Task<WorkerLicenceAppUpsertResponse> Handle(AnonymousWorkerLicenceA
SaveLicenceApplicationCmd saveCmd = _mapper.Map<SaveLicenceApplicationCmd>(request);
var response = await _licenceAppRepository.SaveLicenceApplicationAsync(saveCmd, ct);

foreach (LicAppFileInfo licAppFile in appDocCache.LicAppFileInfos)
foreach (LicAppFileInfo licAppFile in appDocCache.Items)
{
DocumentTypeEnum? docType1 = GetDocumentType1Enum(licAppFile.LicenceDocumentTypeCode);
DocumentTypeEnum? docType2 = GetDocumentType2Enum(licAppFile.LicenceDocumentTypeCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class WorkerLicensingController : SpdControllerBase
private readonly IMapper _mapper;
private readonly IRecaptchaVerificationService _recaptchaVerificationService;
private readonly IDistributedCache _cache;
private static Mutex mutex = new(false);

public WorkerLicensingController(ILogger<WorkerLicensingController> logger,
IPrincipal currentUser,
Expand Down Expand Up @@ -286,8 +287,7 @@ public async Task<Guid> UploadLicenceAppFilesAnonymous([FromForm][Required] Lice

CreateDocumentInCacheCommand command = new CreateDocumentInCacheCommand(fileUploadRequest);
var newFileInfos = await _mediator.Send(command);
existingFileInfo.LicAppFileInfos.AddRange(newFileInfos);
await _cache.Set($"{keyCode}", existingFileInfo, TimeSpan.FromMinutes(30));
await UpdateCache(_cache,keyCode,newFileInfos);
return keyCode;
}

Expand All @@ -314,7 +314,32 @@ public async Task<WorkerLicenceAppUpsertResponse> SubmitSecurityWorkerLicenceApp

AnonymousWorkerLicenceAppSubmitCommand command = new AnonymousWorkerLicenceAppSubmitCommand(jsonRequest, keyCode);
return await _mediator.Send(command);
}

private static async Task UpdateCache(IDistributedCache cache, Guid keyCode, IEnumerable<LicAppFileInfo> newFileInfos)
{
try
{
mutex.WaitOne();
}
catch (AbandonedMutexException)
{
return;
}
try
{
LicenceAppDocumentsCache existingFileInfo = await cache.Get<LicenceAppDocumentsCache?>(keyCode.ToString());
if (existingFileInfo == null)
{
throw new ApiException(HttpStatusCode.BadRequest, "invalid key code.");
}
existingFileInfo.Items.AddRange(newFileInfos);
await cache.Set($"{keyCode}", existingFileInfo, TimeSpan.FromMinutes(30));
}
finally
{
mutex.ReleaseMutex();
}
}
#endregion
}
Expand Down

0 comments on commit f16d060

Please sign in to comment.