网站首页 网站源码
namespace Dpz.Core.Service.RepositoryServiceImpl;
public class BlockService(IRepository<Block> repository, IFusionCache fusionCache, IMapper mapper)
: IBlockService
{
public async Task SaveBlockAsync(Block block)
{
var entity = await repository
.SearchFor(x =>
x.RequestMethod == block.RequestMethod && x.RequestPath == block.RequestPath
)
.FirstOrDefaultAsync();
if (entity != null)
{
var updates = new List<UpdateDefinition<Block>>
{
Builders<Block>.Update.Inc(x => x.AccessCount, 1),
};
var pushIpAddresses = (block.IpAddresses ?? []).Except(entity.IpAddresses).ToList();
if (pushIpAddresses.Count > 0)
{
updates.Add(Builders<Block>.Update.PushEach(x => x.IpAddresses, pushIpAddresses));
}
var pushUserAgents = (block.UserAgents ?? []).Except(entity.UserAgents).ToList();
if (pushUserAgents.Count > 0)
{
updates.Add(Builders<Block>.Update.PushEach(x => x.UserAgents, pushUserAgents));
}
await repository.UpdateAsync(
x => x.Id == entity.Id,
Builders<Block>.Update.Combine(updates)
);
return;
}
if (block.AccessCount <= 0)
{
block.AccessCount = 1;
}
await repository.InsertAsync(block);
}
public async Task IncrementCountAsync(string method, string requestPath)
{
var block = await repository
.SearchFor(x => x.RequestMethod == method && x.RequestPath == requestPath)
.FirstOrDefaultAsync();
if (block == null)
{
await SaveBlockAsync(new Block { RequestMethod = method, RequestPath = requestPath });
return;
}
var update = Builders<Block>.Update.Inc(x => x.AccessCount, 1);
await repository.UpdateAsync(x => x.Id == block.Id, update);
}
public async Task PushIpAddressAsync(
string method,
string requestPath,
params string[] ipAddresses
)
{
if (string.IsNullOrEmpty(method))
{
throw new ArgumentNullException(nameof(method));
}
if (string.IsNullOrEmpty(requestPath))
{
throw new ArgumentNullException(nameof(requestPath));
}
var block = await repository
.SearchFor(x => x.RequestMethod == method && x.RequestPath == requestPath)
.FirstOrDefaultAsync();
if (block == null)
{
await SaveBlockAsync(
new Block
{
RequestMethod = method,
RequestPath = requestPath,
IpAddresses = ipAddresses,
}
);
return;
}
var pushIpAddresses = ipAddresses.Except(block.IpAddresses).ToList();
if (pushIpAddresses.Count == 0)
{
return;
}
var update = Builders<Block>
.Update.PushEach(x => x.IpAddresses, pushIpAddresses)
.Inc(x => x.AccessCount, 1);
await repository.UpdateAsync(x => x.Id == block.Id, update);
}
public async Task PushUserAgentAsync(
string method,
string requestPath,
params string[] userAgents
)
{
if (string.IsNullOrEmpty(method))
{
throw new ArgumentNullException(nameof(method));
}
if (string.IsNullOrEmpty(requestPath))
{
throw new ArgumentNullException(nameof(requestPath));
}
var block = await repository
.SearchFor(x => x.RequestMethod == method && x.RequestPath == requestPath)
.FirstOrDefaultAsync();
if (block == null)
{
await SaveBlockAsync(
new Block
{
RequestMethod = method,
RequestPath = requestPath,
UserAgents = userAgents,
}
);
return;
}
var pushUserAgents = userAgents.Except(block.UserAgents).ToList();
if (pushUserAgents.Count == 0)
{
return;
}
var update = Builders<Block>
.Update.PushEach(x => x.UserAgents, pushUserAgents)
.Inc(x => x.AccessCount, 1);
await repository.UpdateAsync(x => x.Id == block.Id, update);
}
public async Task<List<VmBlock>> GetBlocksAsync(int? minAccess = null)
{
var key = $"Block:List:{(minAccess is null or <= 0 ? "All" : minAccess)}";
var cache = await fusionCache.TryGetAsync<List<VmBlock>>(key);
if (cache.HasValue)
{
return cache.Value;
}
var minAccessValue = minAccess ?? 0;
var list = await repository.SearchFor(x => x.AccessCount >= minAccessValue).ToListAsync();
var source = mapper.Map<List<VmBlock>>(list);
await fusionCache.SetAsync(key, source, TimeSpan.FromHours(3));
return source;
}
}
上述代码定义了一个名为 BlockService
的类,它实现了 IBlockService
接口,主要用于处理与 Block
实体相关的操作。这个类的功能主要集中在以下几个方面:
保存区块 (SaveBlockAsync
):
Block
对象并尝试将其保存到数据库中。Block
实体。Block
实体。增加访问计数 (IncrementCountAsync
):
Block
实体。SaveBlockAsync
方法创建一个新的 Block
实体。推送 IP 地址 (PushIpAddressAsync
):
Block
实体,如果未找到,则创建一个新的 Block
实体并保存。推送用户代理 (PushUserAgentAsync
):
PushIpAddressAsync
类似,但它处理的是用户代理(User Agents)。Block
实体,添加新的用户代理,并增加访问计数。获取区块列表 (GetBlocksAsync
):
Block
实体列表。VmBlock
视图模型列表中,最后将结果存入缓存以供后续使用。BlockService
类的主要功能是管理与 Block
实体相关的操作,包括保存、更新、增加访问计数、推送 IP 地址和用户代理,以及获取区块列表。它利用了异步编程模型来提高性能,并使用缓存来减少数据库访问次数。