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