|
@@ -11,19 +11,17 @@ namespace MySystem
|
|
|
public class RedisDbconn
|
|
|
{
|
|
|
public readonly static RedisDbconn Instance = new RedisDbconn();
|
|
|
- public static CSRedis.CSRedisClient csredis;
|
|
|
private RedisDbconn()
|
|
|
{
|
|
|
- if (csredis == null)
|
|
|
- {
|
|
|
- csredis = new CSRedis.CSRedisClient(ConfigurationManager.AppSettings["RedisConnStr"].ToString());
|
|
|
- }
|
|
|
+ var csredis = new CSRedis.CSRedisClient(ConfigurationManager.AppSettings["RedisConnStr"].ToString());
|
|
|
+ //初始化 RedisHelper
|
|
|
+ RedisHelper.Initialization(csredis);
|
|
|
}
|
|
|
|
|
|
#region 设置单个字段
|
|
|
public bool Set(string key, object value, int sec = -1)
|
|
|
{
|
|
|
- return csredis.Set(key, value, sec);
|
|
|
+ return RedisHelper.Set(key, value, sec);
|
|
|
// return false;
|
|
|
}
|
|
|
#endregion
|
|
@@ -31,7 +29,7 @@ namespace MySystem
|
|
|
#region 整数累加
|
|
|
public long AddInt(string key, long value = 1)
|
|
|
{
|
|
|
- return csredis.IncrBy(key, value);
|
|
|
+ return RedisHelper.IncrBy(key, value);
|
|
|
// return 0;
|
|
|
}
|
|
|
#endregion
|
|
@@ -39,7 +37,7 @@ namespace MySystem
|
|
|
#region 数字累加
|
|
|
public decimal AddNumber(string key, decimal value = 1)
|
|
|
{
|
|
|
- return csredis.IncrByFloat(key, value);
|
|
|
+ return RedisHelper.IncrByFloat(key, value);
|
|
|
// return 0;
|
|
|
}
|
|
|
#endregion
|
|
@@ -47,14 +45,14 @@ namespace MySystem
|
|
|
#region 获取单个字段
|
|
|
public T Get<T>(string key)
|
|
|
{
|
|
|
- return csredis.Get<T>(key);
|
|
|
+ return RedisHelper.Get<T>(key);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 设置散列字段
|
|
|
public bool HSet(string key, string field, object value)
|
|
|
{
|
|
|
- return csredis.HSet(key, field, value);
|
|
|
+ return RedisHelper.HSet(key, field, value);
|
|
|
// return false;
|
|
|
}
|
|
|
#endregion
|
|
@@ -62,7 +60,7 @@ namespace MySystem
|
|
|
#region 散列整数累加
|
|
|
public long HAddInt(string key, string field, long value = 1)
|
|
|
{
|
|
|
- return csredis.HIncrBy(key, field, value);
|
|
|
+ return RedisHelper.HIncrBy(key, field, value);
|
|
|
// return 0;
|
|
|
}
|
|
|
#endregion
|
|
@@ -70,7 +68,7 @@ namespace MySystem
|
|
|
#region 散列数字累加
|
|
|
public decimal HAddNumber(string key, string field, decimal value = 1)
|
|
|
{
|
|
|
- return csredis.HIncrByFloat(key, field, value);
|
|
|
+ return RedisHelper.HIncrByFloat(key, field, value);
|
|
|
// return 0;
|
|
|
}
|
|
|
#endregion
|
|
@@ -78,48 +76,48 @@ namespace MySystem
|
|
|
#region 获取散列元素
|
|
|
public T HGet<T>(string key, string field)
|
|
|
{
|
|
|
- return csredis.HGet<T>(key, field);
|
|
|
+ return RedisHelper.HGet<T>(key, field);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 获取散列所有元素
|
|
|
public Dictionary<string, T> HGetAll<T>(string key)
|
|
|
{
|
|
|
- return csredis.HGetAll<T>(key);
|
|
|
+ return RedisHelper.HGetAll<T>(key);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 添加列表对象
|
|
|
public long AddList(string key, object value)
|
|
|
{
|
|
|
- return csredis.LPush(key, value);
|
|
|
+ return RedisHelper.LPush(key, value);
|
|
|
// return 0;
|
|
|
}
|
|
|
public long AddList(string key, object[] value)
|
|
|
{
|
|
|
- return csredis.LPush(key, value);
|
|
|
+ return RedisHelper.LPush(key, value);
|
|
|
// return 0;
|
|
|
}
|
|
|
public long AddRightList(string key, object value)
|
|
|
{
|
|
|
- return csredis.RPush(key, value);
|
|
|
+ return RedisHelper.RPush(key, value);
|
|
|
// return 0;
|
|
|
}
|
|
|
public T RPop<T>(string key)
|
|
|
{
|
|
|
- return csredis.RPop<T>(key);
|
|
|
+ return RedisHelper.RPop<T>(key);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 添加集合对象
|
|
|
public long SAdd(string key, object value)
|
|
|
{
|
|
|
- return csredis.SAdd(key, value);
|
|
|
+ return RedisHelper.SAdd(key, value);
|
|
|
// return 0;
|
|
|
}
|
|
|
public long SAdd(string key, object[] value)
|
|
|
{
|
|
|
- return csredis.SAdd(key, value);
|
|
|
+ return RedisHelper.SAdd(key, value);
|
|
|
// return 0;
|
|
|
}
|
|
|
#endregion
|
|
@@ -127,22 +125,22 @@ namespace MySystem
|
|
|
#region 获取集合对象
|
|
|
public T[] SGetList<T>(string key)
|
|
|
{
|
|
|
- return csredis.SMembers<T>(key);
|
|
|
+ return RedisHelper.SMembers<T>(key);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 判断元素是否存在
|
|
|
public bool SIsMember(string key, object value)
|
|
|
{
|
|
|
- return csredis.SIsMember(key, value);
|
|
|
+ return RedisHelper.SIsMember(key, value);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 修改列表对象
|
|
|
public bool SetList(string key, int index, object value)
|
|
|
{
|
|
|
- long itemindex = csredis.LLen(key) - index - 1;
|
|
|
- return csredis.LSet(key, itemindex, value);
|
|
|
+ long itemindex = RedisHelper.LLen(key) - index - 1;
|
|
|
+ return RedisHelper.LSet(key, itemindex, value);
|
|
|
// return false;
|
|
|
}
|
|
|
#endregion
|
|
@@ -152,7 +150,7 @@ namespace MySystem
|
|
|
{
|
|
|
int start = (pageNum - 1) * pageSize;
|
|
|
int end = start + pageSize - 1;
|
|
|
- T[] list = csredis.LRange<T>(key, start, end);
|
|
|
+ T[] list = RedisHelper.LRange<T>(key, start, end);
|
|
|
return list.ToList();
|
|
|
}
|
|
|
#endregion
|
|
@@ -160,7 +158,7 @@ namespace MySystem
|
|
|
#region 添加排序列表对象
|
|
|
public long AddSort(string key, object value, decimal score)
|
|
|
{
|
|
|
- return csredis.ZAdd(key, (score, value));
|
|
|
+ return RedisHelper.ZAdd(key, (score, value));
|
|
|
// return 0;
|
|
|
}
|
|
|
#endregion
|
|
@@ -170,7 +168,7 @@ namespace MySystem
|
|
|
{
|
|
|
int start = (pageNum - 1) * pageSize;
|
|
|
int end = start + pageSize;
|
|
|
- string[] list = csredis.ZRangeByScore(key, start, end);
|
|
|
+ string[] list = RedisHelper.ZRangeByScore(key, start, end);
|
|
|
List<T> lists = new List<T>();
|
|
|
foreach (string record in list)
|
|
|
{
|
|
@@ -182,7 +180,7 @@ namespace MySystem
|
|
|
{
|
|
|
int start = (pageNum - 1) * pageSize;
|
|
|
int end = start + pageSize;
|
|
|
- string[] list = csredis.ZRevRangeByScore(key, start, end);
|
|
|
+ string[] list = RedisHelper.ZRevRangeByScore(key, start, end);
|
|
|
List<T> lists = new List<T>();
|
|
|
foreach (string record in list)
|
|
|
{
|
|
@@ -194,7 +192,7 @@ namespace MySystem
|
|
|
|
|
|
public bool Remove(string key, long start, long end)
|
|
|
{
|
|
|
- return csredis.LTrim(key, start, end);
|
|
|
+ return RedisHelper.LTrim(key, start, end);
|
|
|
}
|
|
|
|
|
|
public bool RemoveTop(string key, long count)
|
|
@@ -204,34 +202,34 @@ namespace MySystem
|
|
|
|
|
|
public long Count(string key)
|
|
|
{
|
|
|
- return csredis.LLen(key);
|
|
|
+ return RedisHelper.LLen(key);
|
|
|
}
|
|
|
|
|
|
public void Clear(string pattern)
|
|
|
{
|
|
|
- string[] keys = csredis.Keys(pattern);
|
|
|
- csredis.Del(keys);
|
|
|
+ string[] keys = RedisHelper.Keys(pattern);
|
|
|
+ RedisHelper.Del(keys);
|
|
|
}
|
|
|
|
|
|
public string[] GetKeys(string pattern)
|
|
|
{
|
|
|
- string[] keys = csredis.Keys(pattern);
|
|
|
+ string[] keys = RedisHelper.Keys(pattern);
|
|
|
return keys;
|
|
|
}
|
|
|
|
|
|
public bool Exists(string key)
|
|
|
{
|
|
|
- return csredis.Exists(key);
|
|
|
+ return RedisHelper.Exists(key);
|
|
|
}
|
|
|
|
|
|
public bool HExists(string key, string field)
|
|
|
{
|
|
|
- return csredis.HExists(key, field);
|
|
|
+ return RedisHelper.HExists(key, field);
|
|
|
}
|
|
|
|
|
|
public void SetExpire(string key, int expire)
|
|
|
{
|
|
|
- csredis.Expire(key, expire); //秒为单位
|
|
|
+ RedisHelper.Expire(key, expire); //秒为单位
|
|
|
}
|
|
|
|
|
|
|
|
@@ -261,7 +259,7 @@ namespace MySystem
|
|
|
while (true)
|
|
|
{
|
|
|
//设置key Redis2.6.12以上版本,可以用set获取锁。set可以实现setnx和expire,这个是原子操作
|
|
|
- if (csredis.Set(lockKey, requestId, lockTime, RedisExistence.Nx))
|
|
|
+ if (RedisHelper.Set(lockKey, requestId, lockTime, RedisExistence.Nx))
|
|
|
{
|
|
|
//设置成功后开启子线程为key续命
|
|
|
CreateThredXm();
|
|
@@ -285,12 +283,12 @@ namespace MySystem
|
|
|
return;
|
|
|
}
|
|
|
//查询key还有多少秒释放
|
|
|
- var Seconds = csredis.PTtl(lockKey) / 1000;
|
|
|
+ var Seconds = RedisHelper.PTtl(lockKey) / 1000;
|
|
|
//key还剩1/3秒时重设过期时间
|
|
|
if (Seconds < (lockTime / 3))
|
|
|
{
|
|
|
//小于5秒则自动 重设过期时间
|
|
|
- csredis.Expire(lockKey, lockTime);
|
|
|
+ RedisHelper.Expire(lockKey, lockTime);
|
|
|
}
|
|
|
}
|
|
|
}, tokenSource.Token);
|
|
@@ -305,7 +303,7 @@ namespace MySystem
|
|
|
string script = "if redis.call('get', KEYS[1]) == ARGV[1] then " +
|
|
|
"return redis.call('del', KEYS[1]) " +
|
|
|
"else return 0 end";
|
|
|
- csredis.Eval(script, lockKey, requestId);
|
|
|
+ RedisHelper.Eval(script, lockKey, requestId);
|
|
|
//取消续命线程
|
|
|
tokenSource.Cancel();
|
|
|
}
|