C#操作Redis类
标签:add sys ring try 堆栈 client param core push
public class RedisHelper
{
static RedisClient client;
static RedisHelper()
{
client = new RedisClient("127.0.0.1", 6379);
}
///
/// 清空数据库缓存
///
public static void FlushRedis()
{
client.FlushAll();
}
///
/// 获取redis所有key
///
///
public static Liststring> GetAllKeys()
{
try
{
return client.GetAllKeys();
}
catch (System.Exception)
{
throw;
}
}
///
/// 判断key是否存在
///
///
///
public static bool KeyExists(string key)
{
try
{
return client.Exists(key) == 1 ? true : false;
}
catch (System.Exception)
{
throw;
}
}
///
/// 删除key
///
///
public static void KeyDel(string key)
{
try
{
client.Del(key);
}
catch (System.Exception)
{
throw;
}
}
///
/// 获取key类型
///
///
///
public static string KeyType(string key)
{
try
{
return client.Type(key);
}
catch (System.Exception)
{
throw;
}
}
#region 哈希集合
///
/// 添加值到哈希集合
///
///
///
///
public static void AddHash(string ID,string key,string value)
{
try
{
client.SetEntryInHash(ID, key, value);
}
catch (System.Exception)
{
throw;
}
}
///
/// 获取哈希集合的key
///
///
///
public static Liststring> GetHashKeys(string ID)
{
try
{
return client.GetHashKeys(ID);
}
catch (System.Exception)
{
throw;
}
}
///
/// 获取哈希集合的value
///
///
///
public static Liststring> GetHashValues(string ID)
{
try
{
return client.GetHashValues(ID);
}
catch (System.Exception)
{
throw;
}
}
///
/// 获取哈希集合指定ID、key的value
///
///
///
///
public static string GetHashValue(string ID,string key)
{
try
{
return client.GetValueFromHash(ID, key);
}
catch (System.Exception)
{
throw;
}
}
#endregion
#region 无序集合
///
/// 添加值到数据集合
///
///
///
public static void AddSet(string key,string value)
{
try
{
client.AddItemToSet(key, value);
}
catch
{
}
}
///
/// 获取数据集合
///
///
///
public static HashSetstring> GetSet(string key)
{
try
{
return client.GetAllItemsFromSet(key);
}
catch
{
string[] strMsg = new string[0];
HashSetstring> hSetMsg = new HashSetstring>(strMsg);
return hSetMsg;
}
}
///
/// 获取数据集合
///
///
///
///
///
public static HashSetstring> GetSet(string type,string key1,string key2)
{
try
{
HashSetstring> hSet = new HashSetstring>();
switch (type.ToLower())
{
case "union"://并集
hSet = client.GetUnionFromSets(new string[] { key1, key2 });
break;
case "intersect"://交集
hSet = client.GetIntersectFromSets(new string[] { key1, key2 });
break;
case "except"://差集,存在于key1,不存在于key2
hSet = client.GetDifferencesFromSet(key1, new string[] { key2 });
break;
default:
break;
}
return hSet;
}
catch
{
string[] strMsg = new string[0];
HashSetstring> hSetMsg = new HashSetstring>(strMsg);
return hSetMsg;
}
}
#endregion
#region 有序集合
///
/// 添加值到有序集合
///
///
///
public static void AddZset(string key,string value,double score)
{
try
{
client.AddItemToSortedSet(key, value, score);
}
catch (System.Exception)
{
throw;
}
}
///
/// 获取有序集合
///
///
///
public static Liststring> GetZset(string key)
{
try
{
return client.GetAllItemsFromSortedSet(key);
}
catch (System.Exception)
{
throw;
}
}
///
/// 按score排序获取前n位的有序集合
///
///
///
///
public static Liststring> GetZset(string key,int i)
{
try
{
var value = client.GetRangeWithScoresFromSortedSet(key, 0, i);
Liststring> llist = new Liststring>();
foreach (var item in value)
{
llist.Add(item.Key);
}
return llist;
}
catch (System.Exception)
{
throw;
}
}
#endregion
#region 字符串
///
/// 添加值到字符串
///
///
///
public static void AddString(string key,string value)
{
try
{
client.Setstring>(key, value);
}
catch (System.Exception)
{
throw;
}
}
///
/// 获取字符串
///
///
///
public static string GetString(string key)
{
try
{
return client.Getstring>(key);
}
catch (System.Exception)
{
throw;
}
}
#endregion
#region 列表堆栈
///
/// 添加值入堆:先进先出
///
///
///
public static void AddListQueue(string key,string value)
{
try
{
client.EnqueueItemOnList(key, value);
}
catch (System.Exception)
{
throw;
}
}
///
/// 添加值入栈:先进后出
///
///
///
public static void AddListStack(string key,string value)
{
try
{
client.PushItemToList(key, value);
}
catch (System.Exception)
{
throw;
}
}
///
/// 获取列表
///
///
///
public static Liststring> GetList(string key)
{
Liststring> lList = new Liststring>();
try
{
var p = client.GetListCount(key);
for (int i = 0; i )
{
lList.Add(client.PopItemFromList(key));
}
return lList;
}
catch (System.Exception)
{
throw;
}
}
#endregion
}
C#操作Redis类
标签:add sys ring try 堆栈 client param core push
原文地址:https://www.cnblogs.com/blossomwei/p/14262175.html
文章来自:
搜素材网的
编程语言模块,转载请注明文章出处。
文章标题:
C#操作Redis类
文章链接:http://soscw.com/index.php/essay/58672.html
评论