/// <summary>
/// Redis 帮助类文件
/// </summary>
public class RedisHelper : IDisposable {
/// <summary>
/// 针对Log4net的实例
/// </summary>
private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// The seconds time out.
/// 默认缓存过期时间单位秒
/// </summary>
private int secondsTimeOut = 24 * 60 * 60;
/// <summary>
/// 给某个键对应的数据设置过期时间
/// </summary>
/// <param name="key">键</param>
/// <param name="seconds">过期时间</param>
public void Expire(string key, int seconds) {
try {
this.redis.Expire(key, seconds);
} catch(Exception ex) {
var message = string.Format("设置过期时间出错");
Logger.Error(message, ex);
}
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose() {
if(this.redis != null) {
this.redis.Dispose();
this.redis = null;
}
GC.Collect();
}
private IEnumerable<string> SplitString(string strSource, string split) {
return strSource.Split(split.ToArray());
}
/// <summary>
/// 设置单个实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">缓存建</param>
/// <param name="t">缓存值</param>
/// <param name="timeout">过期时间,单位秒,-1:不过期,0:默认过期时间:一天</param>
public bool Set<T>(string key, T t, int timeout = -1) {
try {
if(timeout >= 0) {
if(timeout > 0) {
this.secondsTimeOut = timeout;
}
var dtTimeOut = DateTime.Now.AddSeconds(this.secondsTimeOut);
return this.redis.Set(key, t, dtTimeOut);
}
return this.redis.Set(key, t);
} catch(Exception ex) {
string message = string.Format("设置Redis缓存出错");
Logger.Error(message, ex);
}
}
/// <summary>
/// 获取单个实体
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="key">键值</param>
public T Get<T>(string key) {
try {
return this.redis.Get<T>(key);
} catch(Exception ex) {
string message = string.Format("获取Redis缓存出错");
Logger.Error(message, ex);
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="key">键值</param>
public bool Remove(string key) {
try {
return this.redis.Remove(key);
} catch(Exception ex) {
string message = string.Format("删除Redis缓存出错");
Logger.Error(message, ex);
}
}
/// <summary>
/// 删除所有
/// </summary>
public void RemoveAll() {
var keyList = this.redis.GetAllKeys();
this.redis.RemoveAll(keyList);
}
/// <summary>
/// 获取Redis的所有key
/// </summary>
public List<string> ListKey() {
return this.redis.GetAllKeys();
}
/// <summary>
/// 添加一个对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="key">键</param>
/// <param name="t"></param>
/// <param name="timeout">过期时间(单位为秒) -1:不过期,0:默认过期时间 一天</param>
public bool Add<T>(string key, T t, int timeout = -1) {
try {
if(timeout >= 0) {
if(timeout > 0) {
this.secondsTimeOut = timeout;
}
this.redis.Expire(key, this.secondsTimeOut);
}
return this.redis.Add(key, t);
} catch(Exception ex) {
string message = string.Format("添加Redis缓存出错");
Logger.Error(message, ex);
}
}
/// <summary>
/// 根据IEnumerable数据添加链表
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="key">键</param>
/// <param name="values">值</param>
/// <param name="timeout">过期时间 -1:不过期,0:默认过期时间:一天</param>
public void AddList<T>(string key, IEnumerable<T> values, int timeout = -1) {
try {
IRedisTypedClient<T> iredisClient = this.redis.As<T>();
IRedisList<T> redisList = iredisClient.Lists[key];
redisList.AddRange(values);
if(timeout > 0) {
if(timeout > 0) {
this.secondsTimeOut = timeout;
}
this.redis.Expire(key, this.secondsTimeOut);
}
iredisClient.Save();
} catch(Exception ex) {
string message = string.Format("添加链表出错");
Logger.Error(message, ex);
}
}
/// <summary>
/// 添加单个实体到链表中
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="key">键</param>
/// <param name="Item"></param>
/// <param name="timeout">过期时间 -1:不过期,0:默认过期时间:一天</param>
public void AddEntityToList<T>(string key, T Item, int timeout = -1) {
try {
IRedisTypedClient<T> iredisClient = redis.As<T>();
IRedisList<T> redisList = iredisClient.Lists[key];
redisList.Add(Item);
iredisClient.Save();
if(timeout >= 0) {
if(timeout > 0) {
this.secondsTimeOut = timeout;
}
this.redis.Expire(key, this.secondsTimeOut);
}
} catch(Exception ex) {
string message = string.Format("添加单个的实体到链表中出错");
Logger.Error(message, ex);
}
}
/// <summary>
/// 获取链表
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="key">键</param>
public IEnumerable<T> GetList<T>(string key) {
try {
IRedisTypedClient<T> iredisClient = redis.As<T>();
return iredisClient.Lists[key];
} catch(Exception ex) {
string message = string.Format("获取链表出错");
Logger.Error(message, ex);
}
}
/// <summary>
/// 在链表中删除单个实体
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="key">键</param>
public void RemoveEntityFromList<T>(string key, T t) {
try {
IRedisTypedClient<T> iredisClient = this.redis.As<T>();
IRedisList<T> redisList = iredisClient.Lists[key];
redisList.RemoveValue(t);
iredisClient.Save();
} catch(Exception ex) {
string message = string.Format("删除链表中的单个实体出错");
Logger.Error(message, ex);
}
}
/// <summary>
/// 根据key移除整个链表
/// </summary>
/// <param name="key">键</param>
public void RemoveAllList<T>(string key) {
try {
IRedisTypedClient<T> iredisClient = this.redis.As<T>();
IRedisList<T> redisList = iredisClient.Lists[key];
redisList.RemoveAll();
iredisClient.Save();
} catch(Exception ex) {
string message = string.Format("删除链表集合");
Logger.Error(message, ex);
}
}
}
本文出自GitHub开源网站