fix: 修复统计数量偶发问题

This commit is contained in:
ccnetcore
2025-08-09 12:20:28 +08:00
parent 4681d468ce
commit f3c67cf598
2 changed files with 17 additions and 18 deletions

View File

@@ -43,7 +43,7 @@ public class AccessLogStoreJob : HangfireBackgroundWorkerBase
{
_repository = repository;
RecurringJobId = "访问日志写入数据库";
//每小时执行一次
CronExpression = "0 0 * * * ?";
@@ -53,10 +53,8 @@ public class AccessLogStoreJob : HangfireBackgroundWorkerBase
// Trigger = TriggerBuilder.Create().WithIdentity(nameof(AccessLogStoreJob))
// .WithCronSchedule("0 * * * * ?")
// .Build();
}
public override async Task DoWorkAsync(CancellationToken cancellationToken = new CancellationToken())
{
@@ -64,7 +62,8 @@ public class AccessLogStoreJob : HangfireBackgroundWorkerBase
{
//当天的访问量
var number =
await RedisClient.GetAsync<long>($"{CacheKeyPrefix}{AccessLogCacheConst.Key}:{DateTime.Now.Date:yyyyMMdd}");
await RedisClient.GetAsync<long>(
$"{CacheKeyPrefix}{AccessLogCacheConst.Key}:{DateTime.Now.Date:yyyyMMdd}");
var entity = await _repository._DbQueryable.Where(x => x.AccessLogType == AccessLogTypeEnum.Request)
@@ -74,16 +73,18 @@ public class AccessLogStoreJob : HangfireBackgroundWorkerBase
if (entity is not null)
{
entity.Number = number+1;
entity.Number = number;
await _repository.UpdateAsync(entity);
}
else
{
await _repository.InsertAsync((new AccessLogAggregateRoot() { Number = number,AccessLogType = AccessLogTypeEnum.Request}));
await _repository.InsertAsync((new AccessLogAggregateRoot()
{ Number = number, AccessLogType = AccessLogTypeEnum.Request }));
}
//删除前天的缓存
await RedisClient.DelAsync($"{CacheKeyPrefix}{AccessLogCacheConst.Key}:{DateTime.Now.Date.AddDays(-1):yyyyMMdd}");
//删除前天的缓存
await RedisClient.DelAsync(
$"{CacheKeyPrefix}{AccessLogCacheConst.Key}:{DateTime.Now.Date.AddDays(-3):yyyyMMdd}");
}
}
}