集成BloomFilter.CSRedis.NetCore,并结合进SqlSugar二级缓存中,缓解缓存穿透攻击
This commit is contained in:
@@ -1,24 +1,39 @@
|
||||
using ZR.Infrastructure.Cache;
|
||||
using BloomFilter;
|
||||
using ZR.Infrastructure.Cache;
|
||||
|
||||
namespace ZR.ServiceCore.SqlSugar
|
||||
{
|
||||
public class SqlSugarCache : ICacheService
|
||||
{
|
||||
private readonly IBloomFilter _bloomFilter;
|
||||
|
||||
public SqlSugarCache(IBloomFilter bloomFilter)
|
||||
{
|
||||
_bloomFilter = bloomFilter;
|
||||
}
|
||||
|
||||
public void Add<V>(string key, V value)
|
||||
{
|
||||
RedisServer.Cache.Set(key, value, 3600 + RedisHelper.RandomExpired(5, 30));
|
||||
// CacheHelper.SetCache(key, value);
|
||||
_bloomFilter.Add(key);
|
||||
}
|
||||
|
||||
public void Add<V>(string key, V value, int cacheDurationInSeconds)
|
||||
{
|
||||
RedisServer.Cache.Set(key, value, cacheDurationInSeconds);
|
||||
// CacheHelper.SetCaches(key, value, cacheDurationInSeconds);
|
||||
_bloomFilter.Add(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);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Infrastructure;
|
||||
using BloomFilter;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Model;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -35,7 +36,8 @@ namespace ZR.ServiceCore.SqlSugar
|
||||
});
|
||||
}
|
||||
SugarIocServices.AddSqlSugar(iocList);
|
||||
ICacheService cache = new SqlSugarCache();
|
||||
var provider = services.BuildServiceProvider();
|
||||
ICacheService cache = new SqlSugarCache(provider.GetService<IBloomFilter>());
|
||||
SugarIocServices.ConfigurationSugar(db =>
|
||||
{
|
||||
var u = App.User;
|
||||
|
||||
Reference in New Issue
Block a user