2018-06-27 更新了redis新的 方法类 https://www.cnblogs.com/jhy55/p/9232623.html
更新内容:添加连接池,高并发线程错误
实用redis已经有一段时间了,今天刚好有空记录一下所用到的方法,欢迎指正
首先我封装了一些字段信息
#region 字段
/// <summary>
/// Redis服务器地址
/// </summary>
private static string _host;
/// <summary>
/// 端口
/// </summary>
private static int _port;
/// <summary>
/// 密码
/// </summary>
private static string _password;
/// <summary>
/// 过期时间
/// </summary>
private static DateTime _timeout;
#endregion
#region 属性
/// <summary>
/// Redis服务器地址
/// </summary>
public static string Host
{
get { return _host = ConfigurationManager.AppSettings["RedisIp"]; }
}
/// <summary>
/// 端口
/// </summary>
public static int Port
{
get { return _port = Int32.Parse(ConfigurationManager.AppSettings["RedisPort"]); }
}
/// <summary>
/// 密码
/// </summary>
public static string Password
{
get { return _password = ConfigurationManager.AppSettings["PassWord"]; }
}
public static int TimeOutDay = Int32.Parse(ConfigurationManager.AppSettings["DayOut"]);
/// <summary>
/// 过期时间
/// </summary>
public static DateTime Timeout
{
get { return _timeout = DateTime.Now.AddDays(TimeOutDay); }
}
#endregion
private static object _locker = new object();
private static RedisClient redis
{
get
{
RedisClient redis = new RedisClient();
if (!IsLock)
redis = new RedisClient(Host, Port, Password, ChangeDb);
redis.ChangeDb(ChangeDb);
return redis;
}
}
/// <summary>
/// 获得Redis对象
/// </summary>
public static RedisClient Redis
{
get { return redis; }
}
/// <summary>
/// 设置缓存
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key"></param>
/// <param name="t"></param>
/// <param name="timeOut">过期时间</param>
/// <returns></returns>
public static bool Set<T>(string key, T t)
{
if (IsLock) return false;
lock (_locker)
{
return redis.Set(key, t, Timeout);
}
}
/// <summary>
/// 设置缓存
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key"></param>
/// <param name="t"></param>
/// <param name="timeOut">过期时间</param>
/// <returns></returns>
public static bool Set<T>(string key, T t, DateTime time)
{
if (IsLock) return false;
lock (_locker)
{
return redis.Set(key, t, time);
}
}
/// <summary>
/// 获取缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static T Get<T>(string key)
{
if (IsLock) return default(T);
lock (_locker)
{
if (redis.Exists(key) > 0)
return redis.Get<T>(key);
else
return default(T);
}
}
/// <summary>
/// 判断是否存在某个key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool IsExist(string key)
{
if (IsLock) return false;
byte[] buffer = redis.Get(key);
if (buffer.Length > 0)
return true;
else
return false;
}
/// <summary>
/// 获取Value的byte的长度
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static int GetValueLength(string key)
{
if (IsLock) return 0;
return redis.Get(key).Length;
}
/// <summary>
/// 移除指定的Key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool Remove(string key)
{
if (IsLock) return false;
lock (_locker)
{
return redis.Remove(key);
}
}
/// <summary>
/// 累加(专用的哦)
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string Append(string sKey, string s)
{
byte[] t = Encoding.Default.GetBytes(s);
t = Encoding.Default.GetBytes(Convert.ToBase64String(t) + "^");
return redis.Append(sKey, t).ToString();
}
/// <summary>
/// 从Redis中读取记录列表
/// </summary>
/// <typeparam name="T">数据结构类型</typeparam>
/// <param name="sKey">关键字</param>
/// <returns></returns>
public static List<T> ReaderEnqueueList<T>(string sKey)
{
var OrderStepLogList = Soholife.Common.RedisUtil.Redis.As<T>();
var fromList = OrderStepLogList.Lists[sKey];
List<T> oList = OrderStepLogList.GetAllItemsFromList(fromList);
if (oList != null)
{
OrderStepLogList.RemoveAllFromList(fromList);
}
return oList;
}
/// <summary>
/// 加入具有数据结构的消息队列中
/// </summary>
/// <typeparam name="T">数据结构类型</typeparam>
/// <param name="t">数据对象</param>
/// <param name="sKey">关键字</param>
public static void AddEnqueue<T>(T t, string sKey)
{
var OrderStepLogList = Soholife.Common.RedisUtil.Redis.As<T>();
//加入具有数据结构的消息队列中
OrderStepLogList.EnqueueItemOnList(OrderStepLogList.Lists[sKey], t);
}