fix: 修复统计数量偶发问题
This commit is contained in:
@@ -23,12 +23,13 @@ public class AccessLogMiddleware : IMiddleware, ITransientDependency
|
|||||||
{
|
{
|
||||||
_accessLogNumber = 0;
|
_accessLogNumber = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static int GetAccessLogNumber()
|
internal static int GetAccessLogNumber()
|
||||||
{
|
{
|
||||||
return _accessLogNumber;
|
return _accessLogNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
|
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
|
||||||
{
|
{
|
||||||
await next(context);
|
await next(context);
|
||||||
@@ -64,28 +65,25 @@ public class AccessLogResetEventHandler : ILocalEventHandler<AccessLogResetArgs>
|
|||||||
return redisEnabled.IsNullOrEmpty() || bool.Parse(redisEnabled);
|
return redisEnabled.IsNullOrEmpty() || bool.Parse(redisEnabled);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//该事件由job定时10秒触发
|
//该事件由job定时10秒触发
|
||||||
public async Task HandleEventAsync(AccessLogResetArgs eventData)
|
public async Task HandleEventAsync(AccessLogResetArgs eventData)
|
||||||
{
|
{
|
||||||
if (EnableRedisCache)
|
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();
|
AccessLogMiddleware.ResetAccessLogNumber();
|
||||||
if (incrNumber>0)
|
if (incrNumber > 0)
|
||||||
{
|
{
|
||||||
await RedisClient.IncrByAsync(
|
await RedisClient.IncrByAsync(
|
||||||
$"{CacheKeyPrefix}{AccessLogCacheConst.Key}:{DateTime.Now.Date:yyyyMMdd}", incrNumber);
|
$"{CacheKeyPrefix}{AccessLogCacheConst.Key}:{DateTime.Now.Date:yyyyMMdd}", incrNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -43,7 +43,7 @@ public class AccessLogStoreJob : HangfireBackgroundWorkerBase
|
|||||||
{
|
{
|
||||||
_repository = repository;
|
_repository = repository;
|
||||||
|
|
||||||
|
|
||||||
RecurringJobId = "访问日志写入数据库";
|
RecurringJobId = "访问日志写入数据库";
|
||||||
//每小时执行一次
|
//每小时执行一次
|
||||||
CronExpression = "0 0 * * * ?";
|
CronExpression = "0 0 * * * ?";
|
||||||
@@ -53,10 +53,8 @@ public class AccessLogStoreJob : HangfireBackgroundWorkerBase
|
|||||||
// Trigger = TriggerBuilder.Create().WithIdentity(nameof(AccessLogStoreJob))
|
// Trigger = TriggerBuilder.Create().WithIdentity(nameof(AccessLogStoreJob))
|
||||||
// .WithCronSchedule("0 * * * * ?")
|
// .WithCronSchedule("0 * * * * ?")
|
||||||
// .Build();
|
// .Build();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public override async Task DoWorkAsync(CancellationToken cancellationToken = new CancellationToken())
|
public override async Task DoWorkAsync(CancellationToken cancellationToken = new CancellationToken())
|
||||||
{
|
{
|
||||||
@@ -64,7 +62,8 @@ public class AccessLogStoreJob : HangfireBackgroundWorkerBase
|
|||||||
{
|
{
|
||||||
//当天的访问量
|
//当天的访问量
|
||||||
var number =
|
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)
|
var entity = await _repository._DbQueryable.Where(x => x.AccessLogType == AccessLogTypeEnum.Request)
|
||||||
@@ -74,16 +73,18 @@ public class AccessLogStoreJob : HangfireBackgroundWorkerBase
|
|||||||
|
|
||||||
if (entity is not null)
|
if (entity is not null)
|
||||||
{
|
{
|
||||||
entity.Number = number+1;
|
entity.Number = number;
|
||||||
await _repository.UpdateAsync(entity);
|
await _repository.UpdateAsync(entity);
|
||||||
}
|
}
|
||||||
else
|
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