mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2025-12-06 16:13:26 +08:00
【新增】redis缓存仓储增加通过Lua脚本查询key的方法,商品增加删除后清理首页缓存的功能。
This commit is contained in:
@@ -62,7 +62,6 @@ namespace CoreCms.Net.Caching.AutoMate.RedisCache
|
||||
/// <returns></returns>
|
||||
public Task<bool> LockTakeAsync(string key, string value, TimeSpan cacheTime);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取值
|
||||
/// </summary>
|
||||
@@ -70,6 +69,11 @@ namespace CoreCms.Net.Caching.AutoMate.RedisCache
|
||||
/// <returns></returns>
|
||||
Task<string> Get(string key);
|
||||
|
||||
/// <summary>
|
||||
/// 获取一个值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
RedisValue StringGet(string key);
|
||||
|
||||
/// <summary>
|
||||
@@ -79,29 +83,88 @@ namespace CoreCms.Net.Caching.AutoMate.RedisCache
|
||||
/// <returns></returns>
|
||||
List<string> SearchKey(string pattern);
|
||||
|
||||
//获取值,并序列化
|
||||
/// <summary>
|
||||
/// 获取值,并序列化
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
Task<TEntity> Get<TEntity>(string key);
|
||||
|
||||
/// <summary>
|
||||
/// 获取一个值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="cacheTime"></param>
|
||||
/// <returns></returns>
|
||||
//保存
|
||||
Task<bool> StringSetAsync(string key, string value, TimeSpan cacheTime);
|
||||
|
||||
/// <summary>
|
||||
/// 获取一个值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="cacheTime"></param>
|
||||
/// <returns></returns>
|
||||
bool StringSet(string key, string value, TimeSpan cacheTime);
|
||||
|
||||
/// <summary>
|
||||
/// 获取一个值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
bool StringSet(string key, string value);
|
||||
|
||||
/// <summary>
|
||||
/// 获取一个值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
bool StringSet(string key, RedisValue value);
|
||||
|
||||
/// <summary>
|
||||
/// 获取一个值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="cacheTime"></param>
|
||||
/// <returns></returns>
|
||||
bool StringSet(string key, RedisValue value, TimeSpan cacheTime);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 设置一个缓存
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="cacheTime"></param>
|
||||
/// <returns></returns>
|
||||
Task SetAsync(string key, object value, TimeSpan cacheTime);
|
||||
|
||||
/// <summary>
|
||||
/// Increment 递增
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
Task<long> StringIncrement(string key, long value=1);
|
||||
|
||||
|
||||
//判断是否存在
|
||||
/// <summary>
|
||||
/// 判断是否存在
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> KeyExistsAsync(string key);
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否存在
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
bool KeyExists(string key);
|
||||
|
||||
/// <summary>
|
||||
@@ -111,11 +174,35 @@ namespace CoreCms.Net.Caching.AutoMate.RedisCache
|
||||
/// <returns></returns>
|
||||
Task<TimeSpan?> KeyTime(string key);
|
||||
|
||||
//移除某一个缓存值
|
||||
/// <summary>
|
||||
/// 删除异步
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
Task KeyDeleteAsync(string key);
|
||||
/// <summary>
|
||||
/// 删除key
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
void KeyDelete(string key);
|
||||
|
||||
//全部清除
|
||||
/// <summary>
|
||||
/// 模糊删除
|
||||
/// </summary>
|
||||
/// <param name="pattern"></param>
|
||||
void LikeRemove(string pattern);
|
||||
|
||||
/// <summary>
|
||||
/// 模糊删除异步
|
||||
/// </summary>
|
||||
/// <param name="pattern"></param>
|
||||
/// <returns></returns>
|
||||
Task LikeRemoveAsync(string pattern);
|
||||
|
||||
/// <summary>
|
||||
/// 全部清除
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task Clear();
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -114,7 +114,6 @@ namespace CoreCms.Net.Caching.AutoMate.RedisCache
|
||||
return await _database.LockReleaseAsync(key, value);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 判断key是否存在
|
||||
/// </summary>
|
||||
@@ -137,7 +136,6 @@ namespace CoreCms.Net.Caching.AutoMate.RedisCache
|
||||
return _database.KeyExists(key);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取键过期时间
|
||||
/// </summary>
|
||||
@@ -149,12 +147,21 @@ namespace CoreCms.Net.Caching.AutoMate.RedisCache
|
||||
return await _database.KeyTimeToLiveAsync(key);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> Get(string key)
|
||||
{
|
||||
return await _database.StringGetAsync(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public RedisValue StringGet(string key)
|
||||
{
|
||||
return _database.StringGet(key);
|
||||
@@ -194,7 +201,48 @@ namespace CoreCms.Net.Caching.AutoMate.RedisCache
|
||||
_database.KeyDelete(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 模糊删除
|
||||
/// </summary>
|
||||
/// <param name="pattern"></param>
|
||||
public void LikeRemove(string pattern)
|
||||
{
|
||||
var redisResult = _database.ScriptEvaluate(LuaScript.Prepare(
|
||||
//Redis的keys模糊查询:
|
||||
" local res = redis.call('KEYS', @keypattern) " +
|
||||
" return res "), new { @keypattern = pattern });
|
||||
|
||||
if (!redisResult.IsNull)
|
||||
{
|
||||
var preSult = (RedisKey[])redisResult;
|
||||
_database.KeyDelete(preSult);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 模糊删除
|
||||
/// </summary>
|
||||
/// <param name="pattern"></param>
|
||||
public async Task LikeRemoveAsync(string pattern)
|
||||
{
|
||||
var redisResult = await _database.ScriptEvaluateAsync(LuaScript.Prepare(
|
||||
//Redis的keys模糊查询:
|
||||
" local res = redis.call('KEYS', @keypattern) " +
|
||||
" return res "), new { @keypattern = pattern });
|
||||
if (!redisResult.IsNull)
|
||||
{
|
||||
var preSult = (RedisKey[])redisResult;
|
||||
await _database.KeyDeleteAsync(preSult);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="cacheTime"></param>
|
||||
/// <returns></returns>
|
||||
public async Task SetAsync(string key, object value, TimeSpan cacheTime)
|
||||
{
|
||||
if (value != null)
|
||||
@@ -204,7 +252,13 @@ namespace CoreCms.Net.Caching.AutoMate.RedisCache
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="cacheTime"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> StringSetAsync(string key, string value, TimeSpan cacheTime)
|
||||
{
|
||||
|
||||
@@ -212,73 +266,75 @@ namespace CoreCms.Net.Caching.AutoMate.RedisCache
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="cacheTime"></param>
|
||||
/// <returns></returns>
|
||||
public bool StringSet(string key, string value, TimeSpan cacheTime)
|
||||
{
|
||||
|
||||
return _database.StringSet(key, value, cacheTime);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public bool StringSet(string key, string value)
|
||||
{
|
||||
|
||||
return _database.StringSet(key, value);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="cacheTime"></param>
|
||||
/// <returns></returns>
|
||||
public bool StringSet(string key, RedisValue value, TimeSpan cacheTime)
|
||||
{
|
||||
|
||||
return _database.StringSet(key, value, cacheTime);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public bool StringSet(string key, RedisValue value)
|
||||
{
|
||||
|
||||
return _database.StringSet(key, value);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Increment 递增
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<long> StringIncrement(string key, long value=1)
|
||||
public async Task<long> StringIncrement(string key, long value = 1)
|
||||
{
|
||||
|
||||
return await _database.StringIncrementAsync(key, value);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取一个值并序列化
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<TEntity> Get<TEntity>(string key)
|
||||
{
|
||||
var value = await _database.StringGetAsync(key);
|
||||
if (value.HasValue)
|
||||
{
|
||||
//需要用的反序列化,将Redis存储的Byte[],进行反序列化
|
||||
return JsonConvert.DeserializeObject<TEntity>(value);
|
||||
return value.HasValue ? JsonConvert.DeserializeObject<TEntity>(value) : default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据key获取RedisValue
|
||||
@@ -470,7 +526,7 @@ namespace CoreCms.Net.Caching.AutoMate.RedisCache
|
||||
/// <returns></returns>
|
||||
public async Task<long?> SortedSetRankAsync(string key, string value)
|
||||
{
|
||||
var result= await _database.SortedSetRankAsync(key,value);
|
||||
var result = await _database.SortedSetRankAsync(key, value);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -506,7 +562,7 @@ namespace CoreCms.Net.Caching.AutoMate.RedisCache
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<string>> SortedSetRangeByRankAsync(string redisKey, int start, int stop)
|
||||
{
|
||||
var result= await _database.SortedSetRangeByRankAsync(redisKey, start, stop);
|
||||
var result = await _database.SortedSetRangeByRankAsync(redisKey, start, stop);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using CoreCms.Net.Caching.AutoMate.RedisCache;
|
||||
using CoreCms.Net.Configuration;
|
||||
using CoreCms.Net.Filter;
|
||||
using CoreCms.Net.IServices;
|
||||
@@ -58,6 +59,7 @@ namespace CoreCms.Net.Web.Admin.Controllers
|
||||
private readonly ICoreCmsUserGradeServices _userGradeServices;
|
||||
private readonly ICoreCmsProductsDistributionServices _productsDistributionServices;
|
||||
private readonly IWebHostEnvironment _webHostEnvironment;
|
||||
private readonly IRedisOperationRepository _redisOperationRepository;
|
||||
|
||||
|
||||
/// <summary>
|
||||
@@ -76,7 +78,7 @@ namespace CoreCms.Net.Web.Admin.Controllers
|
||||
, ICoreCmsGoodsCategoryExtendServices categoryExtendServices
|
||||
, ICoreCmsLabelServices labelServices
|
||||
, ICoreCmsProductsDistributionServices productsDistributionServices
|
||||
, ICoreCmsGoodsTypeSpecServices goodsTypeSpecServices)
|
||||
, ICoreCmsGoodsTypeSpecServices goodsTypeSpecServices, IRedisOperationRepository redisOperationRepository)
|
||||
{
|
||||
_webHostEnvironment = webHostEnvironment;
|
||||
_coreCmsGoodsServices = coreCmsGoodsServices;
|
||||
@@ -92,6 +94,7 @@ namespace CoreCms.Net.Web.Admin.Controllers
|
||||
_labelServices = labelServices;
|
||||
_productsDistributionServices = productsDistributionServices;
|
||||
_goodsTypeSpecServices = goodsTypeSpecServices;
|
||||
_redisOperationRepository = redisOperationRepository;
|
||||
}
|
||||
|
||||
#region 获取列表============================================================
|
||||
@@ -531,6 +534,7 @@ namespace CoreCms.Net.Web.Admin.Controllers
|
||||
{
|
||||
await _productsServices.UpdateAsync(p => new CoreCmsProducts() { isDel = true },
|
||||
p => p.goodsId == model.id);
|
||||
await _redisOperationRepository.LikeRemoveAsync("SqlSugarDataCache.GoodListDTO." + "*");
|
||||
}
|
||||
jm.code = bl ? 0 : 1;
|
||||
jm.msg = bl ? GlobalConstVars.DeleteSuccess : GlobalConstVars.DeleteFailure;
|
||||
|
||||
Reference in New Issue
Block a user