mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2025-12-06 17:13:26 +08:00
【优化】移除EasyCaching.Core,EasyCaching.CSRedis,EasyCaching.InMemory等组件,直接使用原生CSRedis组件,替换SqlSugar二级缓存组件。
This commit is contained in:
@@ -14,65 +14,11 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using CoreCms.Net.Caching.Manual;
|
||||
using CoreCms.Net.Configuration;
|
||||
using CoreCms.Net.Utility.Extensions;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace CoreCms.Net.Caching.Redis
|
||||
{
|
||||
public class RedisCacheManager : IManualCacheManager
|
||||
{
|
||||
private readonly string _redisConnenctionString;
|
||||
|
||||
public volatile ConnectionMultiplexer RedisConnection;
|
||||
|
||||
private readonly object _redisConnectionLock = new object();
|
||||
|
||||
public RedisCacheManager()
|
||||
{
|
||||
string redisConfiguration = AppSettingsConstVars.RedisConfigConnectionString;//获取连接字符串
|
||||
|
||||
if (string.IsNullOrWhiteSpace(redisConfiguration))
|
||||
{
|
||||
throw new ArgumentException("redis config is empty", nameof(redisConfiguration));
|
||||
}
|
||||
_redisConnenctionString = redisConfiguration;
|
||||
|
||||
RedisConnection = GetRedisConnection();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 核心代码,获取连接实例
|
||||
/// 通过双if 夹lock的方式,实现单例模式
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private ConnectionMultiplexer GetRedisConnection()
|
||||
{
|
||||
//如果已经连接实例,直接返回
|
||||
if (RedisConnection != null && RedisConnection.IsConnected)
|
||||
{
|
||||
return RedisConnection;
|
||||
}
|
||||
//加锁,防止异步编程中,出现单例无效的问题
|
||||
lock (_redisConnectionLock)
|
||||
{
|
||||
if (RedisConnection != null)
|
||||
{
|
||||
//释放redis连接
|
||||
RedisConnection.Dispose();
|
||||
}
|
||||
try
|
||||
{
|
||||
RedisConnection = ConnectionMultiplexer.Connect(_redisConnenctionString);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw new Exception("Redis服务未启用,请开启该服务,并且请注意端口号,Redis默认使用6379端口号。");
|
||||
}
|
||||
}
|
||||
return RedisConnection;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 判断key是否存在
|
||||
@@ -81,12 +27,10 @@ namespace CoreCms.Net.Caching.Redis
|
||||
/// <returns></returns>
|
||||
public bool Exists(string key)
|
||||
{
|
||||
return RedisConnection.GetDatabase().KeyExists(key);
|
||||
return RedisHelper.Exists(key);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
@@ -98,15 +42,7 @@ namespace CoreCms.Net.Caching.Redis
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
//序列化,将object值生成RedisValue
|
||||
if (expiresIn > 0)
|
||||
{
|
||||
return RedisConnection.GetDatabase().StringSet(key, SerializeExtensions.Serialize(value), TimeSpan.FromMinutes(expiresIn));
|
||||
}
|
||||
else
|
||||
{
|
||||
return RedisConnection.GetDatabase().StringSet(key, SerializeExtensions.Serialize(value));
|
||||
}
|
||||
return expiresIn > 0 ? RedisHelper.Set(key, value, TimeSpan.FromMinutes(expiresIn)) : RedisHelper.Set(key, value);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -118,7 +54,7 @@ namespace CoreCms.Net.Caching.Redis
|
||||
/// <returns></returns>
|
||||
public void Remove(string key)
|
||||
{
|
||||
RedisConnection.GetDatabase().KeyDelete(key);
|
||||
RedisHelper.DelAsync(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -127,11 +63,9 @@ namespace CoreCms.Net.Caching.Redis
|
||||
/// <returns></returns>
|
||||
public void RemoveAll(IEnumerable<string> keys)
|
||||
{
|
||||
foreach (var key in keys)
|
||||
{
|
||||
RedisConnection.GetDatabase().KeyDelete(key);
|
||||
}
|
||||
|
||||
var enumerable = keys as string[] ?? keys.ToArray();
|
||||
string[] keyStrings = enumerable.ToArray();
|
||||
RedisHelper.DelAsync(keyStrings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -141,19 +75,12 @@ namespace CoreCms.Net.Caching.Redis
|
||||
/// <returns></returns>
|
||||
public T Get<T>(string key)
|
||||
{
|
||||
var value = RedisConnection.GetDatabase().StringGet(key);
|
||||
if (value.HasValue)
|
||||
{
|
||||
//需要用的反序列化,将Redis存储的Byte[],进行反序列化
|
||||
return SerializeExtensions.Deserialize<T>(value);
|
||||
}
|
||||
|
||||
return default;
|
||||
return RedisHelper.Get<T>(key);
|
||||
}
|
||||
|
||||
public object Get(string key)
|
||||
{
|
||||
return RedisConnection.GetDatabase().StringGet(key);
|
||||
return RedisHelper.Get<object>(key);
|
||||
}
|
||||
|
||||
public IDictionary<string, object> GetAll(IEnumerable<string> keys)
|
||||
@@ -162,48 +89,27 @@ namespace CoreCms.Net.Caching.Redis
|
||||
throw new ArgumentNullException(nameof(keys));
|
||||
var dict = new Dictionary<string, object>();
|
||||
|
||||
keys.ToList().ForEach(item => dict.Add(item, RedisConnection.GetDatabase().StringGet(item)));
|
||||
keys.ToList().ForEach(item => dict.Add(item, RedisHelper.Get(item)));
|
||||
return dict;
|
||||
|
||||
}
|
||||
|
||||
public void RemoveCacheAll()
|
||||
{
|
||||
foreach (var endPoint in GetRedisConnection().GetEndPoints())
|
||||
{
|
||||
var server = GetRedisConnection().GetServer(endPoint);
|
||||
foreach (var key in server.Keys())
|
||||
{
|
||||
RedisConnection.GetDatabase().KeyDelete(key);
|
||||
}
|
||||
}
|
||||
//查找所有分区节点中符合给定模式(pattern)的 key
|
||||
var cacheKeys = RedisHelper.Keys("*");
|
||||
RedisHelper.Del(cacheKeys);
|
||||
}
|
||||
|
||||
public void RemoveCacheRegex(string pattern)
|
||||
{
|
||||
var script = "return redis.call('keys',@pattern)";
|
||||
var prepared = LuaScript.Prepare(script);
|
||||
var redisResult = RedisConnection.GetDatabase().ScriptEvaluate(prepared, new { pattern });
|
||||
if (!redisResult.IsNull)
|
||||
{
|
||||
RedisConnection.GetDatabase().KeyDelete((RedisKey[])redisResult); //删除一组key
|
||||
}
|
||||
var cacheKeys = RedisHelper.Keys(pattern);
|
||||
RedisHelper.Del(cacheKeys);
|
||||
}
|
||||
|
||||
public IList<string> SearchCacheRegex(string pattern)
|
||||
{
|
||||
var list = new List<String>();
|
||||
var script = "return redis.call('keys',@pattern)";
|
||||
var prepared = LuaScript.Prepare(script);
|
||||
var redisResult = RedisConnection.GetDatabase().ScriptEvaluate(prepared, new { pattern });
|
||||
if (!redisResult.IsNull)
|
||||
{
|
||||
foreach (var key in (RedisKey[])redisResult)
|
||||
{
|
||||
list.Add(key);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
return RedisHelper.Keys(pattern);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user