You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.2 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Admin.Core.Common;
using StackExchange.Redis;
using System;
namespace Admin.Core.Extensions
{
public class RedisCacheManager : IRedisCacheManager
{
private readonly string redisConnenctionString;
public volatile ConnectionMultiplexer redisConnection;
private readonly object redisConnectionLock = new object();
public RedisCacheManager()
{
string redisConfiguration = Appsettings.app(new string[] { "AppSettings", "RedisCachingAOP", "ConnectionString" });//获取连接字符串
if (string.IsNullOrWhiteSpace(redisConfiguration))
{
throw new ArgumentException("redis config is empty", nameof(redisConfiguration));
}
this.redisConnenctionString = redisConfiguration;
this.redisConnection = GetRedisConnection();
}
/// <summary>
/// 核心代码,获取连接实例
/// 通过双if 加lock的方式实现单例模式
/// </summary>
/// <returns></returns>
private ConnectionMultiplexer GetRedisConnection()
{
//如果已经连接实例,直接返回
if (this.redisConnection != null && this.redisConnection.IsConnected)
{
return this.redisConnection;
}
//加锁,防止异步编程中,出现单例无效的问题
lock (redisConnectionLock)
{
if (this.redisConnection != null)
{
//释放redis连接
this.redisConnection.Dispose();
}
try
{
var config = new ConfigurationOptions
{
AbortOnConnectFail = false,
AllowAdmin = true,
ConnectTimeout = 15000,//改成15s
SyncTimeout = 5000,
//Password = "Pwd",//Redis数据库密码
EndPoints = { redisConnenctionString }// connectionString 为IP:Port 如”192.168.2.110:6379”
};
this.redisConnection = ConnectionMultiplexer.Connect(config);
}
catch (Exception)
{
throw new Exception("Redis服务未启用请开启该服务并且请注意端口号本项目使用的的6319而且我的是没有设置密码。");
}
}
return this.redisConnection;
}
/// <summary>
/// 清除
/// </summary>
public void Clear()
{
foreach (var endPoint in this.GetRedisConnection().GetEndPoints())
{
var server = this.GetRedisConnection().GetServer(endPoint);
foreach (var key in server.Keys())
{
redisConnection.GetDatabase().KeyDelete(key);
}
}
}
/// <summary>
/// 判断是否存在
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Get(string key)
{
return redisConnection.GetDatabase().KeyExists(key);
}
/// <summary>
/// 查询
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string GetValue(string key)
{
return redisConnection.GetDatabase().StringGet(key);
}
/// <summary>
/// 获取
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public TEntity Get<TEntity>(string key)
{
var value = redisConnection.GetDatabase().StringGet(key);
if (value.HasValue)
{
//需要用的反序列化将Redis存储的Byte[],进行反序列化
return SerializeHelper.Deserialize<TEntity>(value);
}
else
{
return default(TEntity);
}
}
/// <summary>
/// 移除
/// </summary>
/// <param name="key"></param>
public void Remove(string key)
{
redisConnection.GetDatabase().KeyDelete(key);
}
/// <summary>
/// 设置
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="cacheTime"></param>
public void Set(string key, object value, TimeSpan cacheTime)
{
if (value != null)
{
//序列化将object值生成RedisValue
redisConnection.GetDatabase().StringSet(key, SerializeHelper.Serialize(value), cacheTime);
}
}
/// <summary>
/// 增加/修改
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool SetValue(string key, byte[] value)
{
return redisConnection.GetDatabase().StringSet(key, value, TimeSpan.FromSeconds(120));
}
}
}