public class RedisHelper
{
private static string address;
JsonSerializerSettings jsonConfig = new JsonSerializerSettings() { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore, NullValueHandling = NullValueHandling.Ignore };
private static ConnectionMultiplexer connectionMultiplexer;
IDatabase database;
class CacheObject<T>
{
public int ExpireTime { get; set; }
public bool ForceOutofDate { get; set; }
public T Value { get; set; }
}
static RedisHelper()
{
address = "192.168.17.128:22121";
if (address == null || string.IsNullOrWhiteSpace(address.ToString()))
throw new ApplicationException("连接字符串不存在");
var options = ConfigurationOptions.Parse(address);
options.Proxy = Proxy.Twemproxy;
connectionMultiplexer = ConnectionMultiplexer.Connect(options);
}
public RedisHelper()
{
database = connectionMultiplexer.GetDatabase();
}
public ITransaction BeginTransation()
{
return database.CreateTransaction();
}
public IBatch CreateBatch()
{
return database.CreateBatch();
}
public object Get(string key)
{
return Get<object>(key);
}
public T Get<T>(string key)
{
DateTime begin = DateTime.Now;
var cacheValue = database.StringGet(key);
DateTime endCache = DateTime.Now;
var value = default(T);
if (!cacheValue.IsNull)
{
var cacheObject = JsonConvert.DeserializeObject<CacheObject<T>>(cacheValue, jsonConfig);
if (!cacheObject.ForceOutofDate)
database.KeyExpire(key, new TimeSpan(0, 0, cacheObject.ExpireTime));
value = cacheObject.Value;
}
DateTime endJson = DateTime.Now;
return value;
}
/// <summary>
/// 永不过期
/// </summary>
/// <param name="key"></param>
/// <param name="data"></param>
public void Insert(string key, object data)
{
Insert<object>(key, data);
}
public void Insert(string key, object data, int cacheTime, bool forceOutofDate = true)
{
Insert<object>(key, data, cacheTime, forceOutofDate);
}
public void Insert(string key, object data, DateTime cacheTime, bool forceOutofDate = true)
{
Insert<object>(key, data, cacheTime, forceOutofDate);
}
public void Insert<T>(string key, T data)
{
var jsonData = GetJsonData(data, 0, true);
database.StringSet(key, jsonData);
}
public void Insert<T>(string key, T data, int cacheTime, bool forceOutofDate = true)
{
var timeSpan = TimeSpan.FromSeconds(cacheTime);
var jsonData = GetJsonData(data, cacheTime, forceOutofDate);
database.StringSet(key, jsonData, timeSpan);
}
public void Insert<T>(string key, T data, DateTime cacheTime, bool forceOutofDate = true)
{
var timeSpan = cacheTime - DateTime.Now;
var jsonData = GetJsonData(data, timeSpan.Seconds, forceOutofDate);
database.StringSet(key, jsonData, timeSpan);
}
/// <summary>
/// 删除
/// </summary>
/// <param name="key"></param>
public void Remove(string key)
{
database.KeyDelete(key);
}
/// <summary>
/// 判断key是否存在
/// </summary>
public bool Exists(string key)
{
return database.KeyExists(key);
}
private string GetJsonData<T>(T data, int cacheTime, bool forceOutOfDate)
{
var cacheObject = new CacheObject<T>() { Value = data, ExpireTime = cacheTime, ForceOutofDate = forceOutOfDate };
return JsonConvert.SerializeObject(cacheObject, jsonConfig);//序列化对象
}
}