fix: 修复统计数量偶发问题
This commit is contained in:
@@ -23,6 +23,7 @@ public class AccessLogMiddleware : IMiddleware, ITransientDependency
|
||||
{
|
||||
_accessLogNumber = 0;
|
||||
}
|
||||
|
||||
internal static int GetAccessLogNumber()
|
||||
{
|
||||
return _accessLogNumber;
|
||||
@@ -71,21 +72,18 @@ public class AccessLogResetEventHandler : ILocalEventHandler<AccessLogResetArgs>
|
||||
if (EnableRedisCache)
|
||||
{
|
||||
//分布式锁
|
||||
if (await RedisClient.SetNxAsync("AccessLogLock",true,TimeSpan.FromSeconds(5)))
|
||||
if (await RedisClient.SetNxAsync("AccessLogLock", true, TimeSpan.FromSeconds(5)))
|
||||
{
|
||||
//自增长数
|
||||
var incrNumber= AccessLogMiddleware.GetAccessLogNumber();
|
||||
var incrNumber = AccessLogMiddleware.GetAccessLogNumber();
|
||||
//立即重置,开始计算,方式丢失
|
||||
AccessLogMiddleware.ResetAccessLogNumber();
|
||||
if (incrNumber>0)
|
||||
if (incrNumber > 0)
|
||||
{
|
||||
await RedisClient.IncrByAsync(
|
||||
$"{CacheKeyPrefix}{AccessLogCacheConst.Key}:{DateTime.Now.Date:yyyyMMdd}", incrNumber);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,8 +53,6 @@ public class AccessLogStoreJob : HangfireBackgroundWorkerBase
|
||||
// Trigger = TriggerBuilder.Create().WithIdentity(nameof(AccessLogStoreJob))
|
||||
// .WithCronSchedule("0 * * * * ?")
|
||||
// .Build();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user