Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

StackExchange.Redis 之 Set集合 类型示例

疯狂的Mr.Zhang 2020-02-16 23:17:00 阅读数:18 评论数:0 点赞数:0 收藏数:0

话不多说直接上代码:

 // set添加单个元素 
 stopwatch.Start();
var isok = RedisCacheHelper.Instance.SetAdd("setkey", "");
stopwatch.Stop();
Console.WriteLine("set添加单个元素消耗时间:" + stopwatch.ElapsedMilliseconds.ToString());
// set添加多个元素 sting类型集合 
List<string> strlist = new List<string>() { "", "", "A", "B", "", "" };
stopwatch.Start();
var getlong = RedisCacheHelper.Instance.SetAdd("setkeylist", strlist);
stopwatch.Stop();
Console.WriteLine("set添加多个元素消耗时间:" + stopwatch.ElapsedMilliseconds.ToString());
Console.WriteLine("返回集合长度" + getlong);
//从数据库获取10条数据
 stopwatch.Start();
var getlist = TestRedis.GetOrderFormAll();
stopwatch.Stop();
Console.WriteLine("从数据库获取10条数据消耗时间:" + stopwatch.ElapsedMilliseconds.ToString());
// set添加多个对象集合 序列化 
 stopwatch.Start();
var getvalue = RedisCacheHelper.Instance.SetAddList("setkeyobjlist", getlist);
stopwatch.Stop();
Console.WriteLine("set添加多个对象集合消耗时间:" + stopwatch.ElapsedMilliseconds.ToString());
Console.WriteLine("返回集合长度" + getvalue);

 

 

 // 获取set集合的长度 
var getleng = RedisCacheHelper.Instance.SetLength("setkeylist");
Console.WriteLine("获取 setkeylist 的长度:" + getleng);
var getobjleng = RedisCacheHelper.Instance.SetLength("setkeyobjlist");
Console.WriteLine("获取 setkeyobjlist 的长度:" + getobjleng);
// 检查元素是否属于Set集合 
var getisA = RedisCacheHelper.Instance.ExistsInSet("setkeylist", "A");
var getisC = RedisCacheHelper.Instance.ExistsInSet("setkeylist", "C");
Console.WriteLine("检查 A 是否属于setkeylist集合:" + getisA);
Console.WriteLine("检查 C 是否属于setkeylist集合:" + getisC);
// 根据key获取所有Set元素 
 stopwatch.Start();
var getlist = RedisCacheHelper.Instance.GetMembers("setkeylist");
stopwatch.Stop();
Console.WriteLine("获取所有Set元素消耗时间:" + stopwatch.ElapsedMilliseconds.ToString());
foreach (var item in getlist)
{
Console.WriteLine(item);
}
// 根据key获取所有对象元素 反序列化
 stopwatch.Start();
var getobjlist = RedisCacheHelper.Instance.GetAllMembers<OrderForm>("setkeyobjlist");
stopwatch.Stop();
Console.WriteLine("获取所有对象集合消耗时间:" + stopwatch.ElapsedMilliseconds.ToString());
foreach (var item in getobjlist)
{
Console.WriteLine("打印Area: " + item.Area);
}

 //求 交集、差集、并集
string[] arry2 = { "setkeylist", "setkeylist2" };
var d1 = RedisCacheHelper.Instance.GetCombines(SetOperation.Union, arry2.ToList()); //并集
var d2 = RedisCacheHelper.Instance.GetCombines(SetOperation.Intersect, arry2.ToList()); //交集
var d3 = RedisCacheHelper.Instance.GetCombines(SetOperation.Difference, arry2.ToList()); //差集
foreach (var item in d1)
{
Console.WriteLine("setkeylist和setkeylist2的并集有:" + item);
}
foreach (var item in d2)
{
Console.WriteLine("setkeylist和setkeylist2的交集有:" + item);
}
foreach (var item in d3)
{
Console.WriteLine("setkeylist和setkeylist2的差集有:" + item);
}

 // 根据key随机获取Set中的1个元素 循环获取会得到重复内容,
for (int i = ; i < ; i++)
{
stopwatch.Start();
var getone = RedisCacheHelper.Instance.GetRandomMember("setkeyobjlist");
stopwatch.Stop();
//Console.WriteLine("根据key随机获取Set中的1个元素消耗时间:" + stopwatch.ElapsedMilliseconds.ToString());
Console.WriteLine("打印: " + getone);
}
// 根据key随机获取Set中的n个元素 一次性获取,无重复值
 stopwatch.Start();
var getobjlist = RedisCacheHelper.Instance.GetRandomMembers("setkeyobjlist", );
stopwatch.Stop();
Console.WriteLine("根据key随机获取Set中的n个元素消耗时间:" + stopwatch.ElapsedMilliseconds.ToString());
foreach (var item in getobjlist)
{
Console.WriteLine("打印: " + item);
}

 

最后附上Helper帮助类

 /// <summary>
/// set添加单个元素
/// 具有唯一性 比如在线人数/点赞人数/收藏人数等
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="db"></param>
/// <returns></returns>
public bool SetAdd(string key, string value, TimeSpan? span = null, int db = -)
{
try
{
_db = GetDatabase(db);
//设置过期时间
if (span != null)
{
this.KeyExpire(key, span);
}
return _db.SetAdd(key, value);
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// set添加多个元素集合
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="db"></param>
/// <returns></returns>
public long SetAdd(string key, List<string> arryList, int db = -)
{
try
{
_db = GetDatabase(db);
RedisValue[] valueList = arryList.Select(u => (RedisValue)u).ToArray();
return _db.SetAdd(key, valueList);
}
catch (Exception)
{
return ;
}
}
/// <summary>
/// set添加多个对象集合 序列化
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="db"></param>
/// <returns></returns>
public long SetAddList<T>(string key, IEnumerable<T> list, int db = -) where T : class
{
try
{
_db = GetDatabase(db);
List<RedisValue> listRedisValue = new List<RedisValue>();
foreach (var item in list)
{
string json = JsonConvert.SerializeObject(item);
listRedisValue.Add(json);
}
return _db.SetAdd(key, listRedisValue.ToArray());
}
catch (Exception)
{
return ;
}
}
/// <summary>
/// 获取set集合的长度
/// </summary>
/// <param name="key"></param>
/// <param name="db"></param>
/// <returns></returns>
public long SetLength(string key, int db = -)
{
try
{
_db = GetDatabase(db);
return _db.SetLength(key);
}
catch (Exception)
{
return ;
}
}
/// <summary>
/// 检查元素是否属于Set集合
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="db"></param>
/// <returns></returns>
public bool ExistsInSet(string key, string value, int db = -)
{
try
{
_db = GetDatabase(db);
return _db.SetContains(key, value);
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 根据key获取所有Set元素
/// </summary>
/// <param name="redisKey"></param>
/// <param name="db"></param>
/// <returns></returns>
public IEnumerable<string> GetMembers(string redisKey, int db = -)
{
try
{
_db = GetDatabase(db);
var rValue = _db.SetMembers(redisKey);
return rValue.Select(ob => ob.ToString());
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 根据key获取所有Set对象集合 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="redisKey"></param>
/// <param name="db"></param>
/// <returns></returns>
public List<T> GetAllMembers<T>(string redisKey, int db = -) where T : class
{
List<T> result = new List<T>();
try
{
_db = GetDatabase(db);
var arr = _db.SetMembers(redisKey);
foreach (var item in arr)
{
if (!item.IsNullOrEmpty)
{
var t = JsonConvert.DeserializeObject<T>(item);
if (t != null)
{
result.Add(t);
}
}
}
}
catch (Exception)
{
return null;
}
return result;
}
/// <summary>
/// 根据key随机获取Set中的1个元素 不删除该元素
/// 可应用于中奖类的案例
/// </summary>
/// <param name="redisKey"></param>
/// <param name="db"></param>
/// <returns></returns>
public string GetRandomMember(string redisKey, int db = -)
{
try
{
_db = GetDatabase(db);
var rValue = _db.SetRandomMember(redisKey);
return rValue.ToString();
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 根据key随机获取Set中的n个元素 不删除该元素
/// 可应用于中奖类的案例
/// </summary>
/// <param name="redisKey"></param>
/// <param name="count"></param>
/// <param name="db"></param>
/// <returns></returns>
public IEnumerable<string> GetRandomMembers(string redisKey, long count, int db = -)
{
try
{
_db = GetDatabase(db);
var rValue = _db.SetRandomMembers(redisKey, count);
return rValue.Select(ob => ob.ToString());
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 随机删除指定key集合中的一个值,并返回这些值
/// 可应用于抽奖类的案例
/// </summary>
/// <param name="redisKey"></param>
/// <param name="db"></param>
/// <returns></returns>
public string GetRandomRemovePop(string redisKey, int db = -)
{
try
{
_db = GetDatabase(db);
var rValue = _db.SetPop(redisKey);
return rValue.ToString();
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 随机删除指定key集合中的n个值,并返回这些值
/// 可应用于抽奖类的案例
/// </summary>
/// <param name="redisKey"></param>
/// <param name="count"></param>
/// <param name="db"></param>
/// <returns></returns>
public IEnumerable<string> GetRandomRemovePops(string redisKey, long count, int db = -)
{
try
{
_db = GetDatabase(db);
var rValue = _db.SetPop(redisKey, count);
return rValue.Select(ob => ob.ToString());
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 返回指定rediskey集合的交集、差集、并集
/// 只能在同一个库内查询,跨库则不行
/// </summary>
/// <param name="operation"></param>
/// <param name="keyList"></param>
/// <param name="db"></param>
/// <returns></returns>
public IEnumerable<string> GetCombines(SetOperation operation, List<string> keyList, int db = -)
{
try
{
_db = GetDatabase(db);
RedisKey[] valueList = keyList.Select(u => (RedisKey)u).ToArray();
var rValue = _db.SetCombine(operation, valueList);
return rValue.Select(ob => ob.ToString());
}
catch (Exception)
{
throw;
}
}
View Code
版权声明
本文为[每天努力进步一点点]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/peterzhang123/p/12318915.html