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

254 lines
7.5 KiB

using System;
using System.Configuration;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using StackExchange.Redis;
namespace GPSBusiness.Redis
{
/// <summary>
/// Redis 操作类
/// </summary>
public class RedisHelper : IDisposable
{
/// <summary>
/// 连接字符串
/// </summary>
private static readonly string ConnectionString = ConfigurationManager.AppSettings["RedisConnectionString"];
private static readonly int RedisAnalysisDb = int.Parse(ConfigurationManager.AppSettings["RedisAnalysisDb"]);
/// <summary>
/// 锁
/// </summary>
private readonly object _lock = new object();
/// <summary>
/// 连接对象
/// </summary>
private volatile IConnectionMultiplexer _connection;
/// <summary>
/// 数据库
/// </summary>
private readonly IDatabase _db;
public RedisHelper()
{
_connection =
ConnectionMultiplexer.Connect(string.Format(
"{0},allowAdmin=true,syncTimeout=60000,connectTimeout=30000", ConnectionString));
_db = GetDatabase(RedisAnalysisDb);
}
public RedisHelper(string connectStr,int dataDb)
{
_connection =
ConnectionMultiplexer.Connect(string.Format(
"{0},allowAdmin=true,syncTimeout=60000,connectTimeout=60000", connectStr));
_db = GetDatabase(dataDb);
}
/// <summary>
/// 获取连接
/// </summary>
/// <returns></returns>
protected IConnectionMultiplexer GetConnection()
{
if (_connection != null && _connection.IsConnected)
{
return _connection;
}
lock (_lock)
{
if (_connection != null && _connection.IsConnected)
{
return _connection;
}
if (_connection != null)
{
_connection.Dispose();
}
_connection = ConnectionMultiplexer.Connect(ConnectionString);
}
return _connection;
}
/// <summary>
/// 获取数据库
/// </summary>
/// <param name="db"></param>
/// <returns></returns>
public IDatabase GetDatabase(int? db = null)
{
return GetConnection().GetDatabase(db ?? -1);
}
/// <summary>
/// 设置
/// </summary>
/// <param name="key">键</param>
/// <param name="data">值</param>
/// <param name="cacheTime">时间-分钟</param>
public virtual void SetOfBytes(string key, object data, int cacheTime)
{
if (data == null)
{
return;
}
var entryBytes = SerializeToBytes(data);
TimeSpan? expiresIn = null;
if(cacheTime != -1)
expiresIn = TimeSpan.FromMinutes(cacheTime);
_db.StringSet(key, entryBytes, expiresIn);
}
/// <summary>
/// 设置
/// </summary>
/// <param name="key">键</param>
/// <param name="data">值</param>
/// <param name="cacheTime">时间-分钟</param>
public virtual void SetOfJson(string key, object data, int cacheTime)
{
if (data == null) return;
var serializedValue = SerializeToJson(data);
TimeSpan? expiresIn = null;
if(cacheTime != -1)
expiresIn = TimeSpan.FromMinutes(cacheTime);
_db.StringSet(key, serializedValue, expiresIn);
}
/// <summary>
/// 根据键获取值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public virtual T GetFromBytes<T>(string key)
{
var rValue = _db.StringGet(key);
if (!rValue.HasValue)
{
return default(T);
}
var result = DeserializeBytes<T>(rValue);
return result;
}
/// <summary>
/// 根据键获取值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public virtual T GetFromJson<T>(string key)
{
var rValue = _db.StringGet(key);
if (!rValue.HasValue)
{
return default(T);
}
var result = DeserializeJson<T>(rValue);
return result;
}
/// <summary>
/// 根据键获取值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public virtual async Task<T> GetFromJsonAsync<T>(string key)
{
var rValue =await _db.StringGetAsync(key);
if (!rValue.HasValue)
{
return default(T);
}
var result = DeserializeJson<T>(rValue);
return result;
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="data"></param>
/// <returns>byte[]</returns>
private byte[] SerializeToBytes(object data)
{
var json = JsonConvert.SerializeObject(data);
return Encoding.UTF8.GetBytes(json);
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="data"></param>
/// <returns>byte[]</returns>
private string SerializeToJson(object data)
{
return JsonConvert.SerializeObject(data);
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="serializedObject"></param>
/// <returns></returns>
protected virtual T DeserializeBytes<T>(byte[] serializedObject)
{
if (serializedObject == null)
{
return default(T);
}
var json = Encoding.UTF8.GetString(serializedObject);
return JsonConvert.DeserializeObject<T>(json);
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="serializedObject"></param>
/// <returns></returns>
protected virtual T DeserializeJson<T>(string serializedObject)
{
if (serializedObject == null)
{
return default(T);
}
return JsonConvert.DeserializeObject<T>(serializedObject);
}
/// <summary>
/// 判断是否已经设置
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public virtual bool IsSet(string key)
{
return _db.KeyExists(key);
}
/// <summary>
/// 根据key删除
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public virtual bool Remove(string key)
{
return _db.KeyDelete(key);
}
public void Dispose()
{
throw new NotImplementedException();
}
/// <summary>
/// 自增
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public virtual long StringIncrement(string key)
{
return _db.StringIncrement(key);
}
}
}