集成BloomFilter.CSRedis.NetCore,并结合进SqlSugar二级缓存中,缓解缓存穿透攻击
This commit is contained in:
parent
e7fe909fd0
commit
e6dae7d1e2
@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="AspectCore.Abstractions" Version="2.4.0" />
|
<PackageReference Include="AspectCore.Abstractions" Version="2.4.0" />
|
||||||
|
<PackageReference Include="BloomFilter.CSRedis.NetCore" Version="2.1.1" />
|
||||||
<PackageReference Include="Caching.CSRedis" Version="3.8.670" />
|
<PackageReference Include="Caching.CSRedis" Version="3.8.670" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
|
|||||||
@ -3,6 +3,8 @@ using Infrastructure.Converter;
|
|||||||
using Microsoft.AspNetCore.DataProtection;
|
using Microsoft.AspNetCore.DataProtection;
|
||||||
using NLog.Web;
|
using NLog.Web;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using BloomFilter;
|
||||||
|
using BloomFilter.CSRedis.Configurations;
|
||||||
using Microsoft.Extensions.Caching.Distributed;
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
using Microsoft.Extensions.Caching.Redis;
|
using Microsoft.Extensions.Caching.Redis;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
@ -64,8 +66,22 @@ if (openRedis == "1")
|
|||||||
{
|
{
|
||||||
RedisServer.Initalize();
|
RedisServer.Initalize();
|
||||||
builder.Services.AddSingleton<IDistributedCache>(new CSRedisCache(RedisHelper.Instance));
|
builder.Services.AddSingleton<IDistributedCache>(new CSRedisCache(RedisHelper.Instance));
|
||||||
|
builder.Services.AddBloomFilter(setupAction =>
|
||||||
|
{
|
||||||
|
setupAction.UseCSRedis(new FilterCSRedisOptions
|
||||||
|
{
|
||||||
|
Name = "Redis1",
|
||||||
|
RedisKey = "BloomFilter",
|
||||||
|
ConnectionStrings = new List<string>
|
||||||
|
{
|
||||||
|
AppSettings.GetConfig("RedisServer:Cache")
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
builder.Services.AddMvc(options =>
|
builder.Services.AddMvc(options =>
|
||||||
{
|
{
|
||||||
options.Filters.Add(typeof(GlobalActionMonitor));//全局注册
|
options.Filters.Add(typeof(GlobalActionMonitor));//全局注册
|
||||||
@ -218,4 +234,8 @@ var json = JsonConvert.SerializeObject(apiResult, new JsonSerializerSettings
|
|||||||
{
|
{
|
||||||
ContractResolver = new JsonPropertyContractResolver(props)
|
ContractResolver = new JsonPropertyContractResolver(props)
|
||||||
});
|
});
|
||||||
|
var provider = builder.Services.BuildServiceProvider();
|
||||||
|
var bf = provider.GetService<IBloomFilter>();
|
||||||
|
bf.Add("Value");
|
||||||
|
Console.WriteLine(bf.Contains("Value"));
|
||||||
app.Run();
|
app.Run();
|
||||||
@ -1,24 +1,39 @@
|
|||||||
using ZR.Infrastructure.Cache;
|
using BloomFilter;
|
||||||
|
using ZR.Infrastructure.Cache;
|
||||||
|
|
||||||
namespace ZR.ServiceCore.SqlSugar
|
namespace ZR.ServiceCore.SqlSugar
|
||||||
{
|
{
|
||||||
public class SqlSugarCache : ICacheService
|
public class SqlSugarCache : ICacheService
|
||||||
{
|
{
|
||||||
|
private readonly IBloomFilter _bloomFilter;
|
||||||
|
|
||||||
|
public SqlSugarCache(IBloomFilter bloomFilter)
|
||||||
|
{
|
||||||
|
_bloomFilter = bloomFilter;
|
||||||
|
}
|
||||||
|
|
||||||
public void Add<V>(string key, V value)
|
public void Add<V>(string key, V value)
|
||||||
{
|
{
|
||||||
RedisServer.Cache.Set(key, value, 3600 + RedisHelper.RandomExpired(5, 30));
|
RedisServer.Cache.Set(key, value, 3600 + RedisHelper.RandomExpired(5, 30));
|
||||||
// CacheHelper.SetCache(key, value);
|
// CacheHelper.SetCache(key, value);
|
||||||
|
_bloomFilter.Add(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Add<V>(string key, V value, int cacheDurationInSeconds)
|
public void Add<V>(string key, V value, int cacheDurationInSeconds)
|
||||||
{
|
{
|
||||||
RedisServer.Cache.Set(key, value, cacheDurationInSeconds);
|
RedisServer.Cache.Set(key, value, cacheDurationInSeconds);
|
||||||
// CacheHelper.SetCaches(key, value, cacheDurationInSeconds);
|
// CacheHelper.SetCaches(key, value, cacheDurationInSeconds);
|
||||||
|
_bloomFilter.Add(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool ContainsKey<V>(string key)
|
public bool ContainsKey<V>(string key)
|
||||||
{
|
{
|
||||||
return RedisServer.Cache.Exists(key);
|
if (_bloomFilter.Contains(key))
|
||||||
|
{
|
||||||
|
return RedisServer.Cache.Exists(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
// return CacheHelper.Exists(key);
|
// return CacheHelper.Exists(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using Infrastructure;
|
using BloomFilter;
|
||||||
|
using Infrastructure;
|
||||||
using Infrastructure.Model;
|
using Infrastructure.Model;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
@ -35,7 +36,8 @@ namespace ZR.ServiceCore.SqlSugar
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
SugarIocServices.AddSqlSugar(iocList);
|
SugarIocServices.AddSqlSugar(iocList);
|
||||||
ICacheService cache = new SqlSugarCache();
|
var provider = services.BuildServiceProvider();
|
||||||
|
ICacheService cache = new SqlSugarCache(provider.GetService<IBloomFilter>());
|
||||||
SugarIocServices.ConfigurationSugar(db =>
|
SugarIocServices.ConfigurationSugar(db =>
|
||||||
{
|
{
|
||||||
var u = App.User;
|
var u = App.User;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user