This commit is contained in:
yxw
2026-01-04 18:53:19 +08:00
parent 948aa3d5b2
commit 41a939176e
11 changed files with 178 additions and 26 deletions

View File

@@ -0,0 +1,54 @@
using Atomx.Data;
using Atomx.Data.CacheServices;
using Hangfire;
using Microsoft.Extensions.Logging;
namespace Atomx.Core.Jos
{
public class ResetCacheJob
{
readonly ILogger<ResetCacheJob> _logger;
readonly DataContext _dbContext;
readonly ICacheService _cacheService;
public ResetCacheJob(ILogger<ResetCacheJob> logger, DataContext dataContext, ICacheService cacheService)
{
_logger = logger;
_dbContext = dataContext;
_cacheService = cacheService;
}
/// <summary>
/// 如果任务失败,重试 3 次超过后删除任务60 秒内不允许并发执行
/// </summary>
[AutomaticRetry(Attempts = 3, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
[DisableConcurrentExecution(60)]
public async Task ResetStateProvinceAndAreaTree(long countryId)
{
try
{
await _cacheService.GetAreaTree(countryId, true);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error resetting cache for countryId: {CountryId}", countryId);
}
}
/// <summary>
/// 如果任务失败,重试 3 次超过后删除任务60 秒内不允许并发执行
/// </summary>
[AutomaticRetry(Attempts = 3, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
[DisableConcurrentExecution(60)]
public async Task ResetCountry()
{
try
{
await _cacheService.GetCountries(true);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error resetting country cache");
}
}
}
}

View File

@@ -1,5 +1,7 @@
using Hangfire;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Text.RegularExpressions;
namespace Atomx.Core.Jos
{
@@ -24,6 +26,13 @@ namespace Atomx.Core.Jos
string SendSMSVerificationCode(string phoneNumber, string code, TimeSpan validDuration);
/// <summary>
/// 更新调整区域树缓存数据,会更新州省缓存
/// </summary>
/// <param name="countryId"></param>
/// <returns></returns>
string ResetStateProvinceAndAreaTree(long countryId);
}
public partial class BackgroundJobService : IBackgroundJobService
@@ -68,5 +77,16 @@ namespace Atomx.Core.Jos
return string.Empty;
}
/// <summary>
/// 更新调整区域树缓存数据,会更新州省缓存
/// </summary>
/// <param name="countryId"></param>
/// <returns></returns>
public string ResetStateProvinceAndAreaTree(long countryId)
{
var jobId = _backgroundJobClient.Enqueue<ResetCacheJob>(job => job.ResetStateProvinceAndAreaTree(countryId));
return jobId;
}
}
}