3)Set,可以去重的、无序的集合。可以取交集、并集。zset(sorted set),有序的、去重的集合,排序不是根据value排序,而是根据score排序。
using (RedisClient client = new RedisClient("127.0.0.1", 6379, "12345", 10))
{ //删除当前数据库中的所有Key 默认删除的是db0
client.FlushDb();
//删除所有数据库中的key
//client.FlushAll();
#region Set 不重复集合
string key = "world_set";
//投票
#region 添加键值 //就是自动去重,再带去重的功能
{
client.FlushDb();
var litaibai = new UserInfo() { Id = 1, Name = "李太白" };
client.AddItemToSet(key, JsonConvert.SerializeObject(litaibai));
client.AddItemToSet(key, JsonConvert.SerializeObject(litaibai));
client.AddItemToSet(key, JsonConvert.SerializeObject(litaibai));
client.AddItemToSet(key, JsonConvert.SerializeObject(litaibai));
Console.WriteLine("***完成了");
}
#endregion
#region 随机获取key集合中的一个值,获取当前setid中的所有值
{
client.FlushDb();
//批量的去操作set 集合 去重
Console.WriteLine("set 开始了");
client.AddRangeToSet(key, new List<string>() { "001", "001", "002", "003", "003", "004" });
//当前setid中的值数量
Console.WriteLine(client.GetSetCount(key));
//随机获取key集合中的一个值 如果有需要取随机数也可以用
Console.WriteLine(client.GetRandomItemFromSet(key));
//获取当前setid中的所有值
var lists = client.GetAllItemsFromSet(key);
Console.WriteLine("展示所有的值");
foreach (var item in lists)
{
Console.WriteLine(item);
}
}
#endregion
#region 随机删除key集合中的一个值
{
client.FlushDb();
client.AddRangeToSet(key, new List<string>() { "001", "001", "002" });
//随机删除key集合中的一个值 返回当前删掉的这个值
Console.WriteLine("随机删除的值" + client.PopItemFromSet(key));
var lists = client.GetAllItemsFromSet(key);
Console.WriteLine("展示删除之后所有的值");
foreach (var item in lists)
{
Console.WriteLine(item);
}
}
#endregion
#region 根据小key 删除 一个key 对象多个value
{
client.FlushDb();
client.AddRangeToSet(key, new List<string>() { "001", "001", "002" });
client.RemoveItemFromSet(key, "001");
var lists = client.GetAllItemsFromSet(key);
Console.WriteLine("展示删除之后所有的值");
foreach (var item in lists)
{
Console.WriteLine(item);
}
}
#endregion
#region 从fromkey集合中移除值为value的值,并把value添加到tokey集合中
{
client.FlushDb();
client.AddRangeToSet("fromkey", new List<string>() { "003", "001", "002", "004" });
client.AddRangeToSet("tokey", new List<string>() { "001", "002" });
//从fromkey 中把元素004 剪切到tokey 集合中去
client.MoveBetweenSets("fromkey", "tokey", "004");
Console.WriteLine("fromkey data ~~~~~~");
foreach (var item in client.GetAllItemsFromSet("fromkey"))
{
Console.WriteLine(item);
}
Console.WriteLine("tokey data ~~~~~~");
foreach (var item in client.GetAllItemsFromSet("tokey"))
{
Console.WriteLine(item);
}
}
#endregion
#region 并集 把两个集合合并起来,然后去重
{
client.FlushDb();
client.AddRangeToSet("keyone", new List<string>() { "001", "002", "003", "004" });
client.AddRangeToSet("keytwo", new List<string>() { "001", "002", "005" });
var unionlist = client.GetUnionFromSets("keyone", "keytwo");
Console.WriteLine("返回并集结果");
foreach (var item in unionlist)
{
Console.WriteLine(item);
}
//把 keyone 和keytwo 并集结果存放到newkey 集合中
client.StoreUnionFromSets("newkey", "keyone", "keytwo");
Console.WriteLine("返回并集结果的新集合数据");
foreach (var item in client.GetAllItemsFromSet("newkey"))
{
Console.WriteLine(item);
}
}
#endregion
#region 交集 获取两个集合中共同存在的元素
{
client.FlushDb();
client.AddRangeToSet("keyone", new List<string>() { "001", "002", "003", "004" });
client.AddRangeToSet("keytwo", new List<string>() { "001", "002", "005" });
var Intersectlist = client.GetIntersectFromSets("keyone", "keytwo");
Console.WriteLine("交集的结果");
foreach (var item in Intersectlist)
{
Console.WriteLine(item);
}
//把 keyone 和keytwo 交集结果存放到newkey 集合中
client.StoreIntersectFromSets("newkey", "keyone", "keytwo");
Console.WriteLine("返回交集结果的新集合数据");
foreach (var item in client.GetAllItemsFromSet("newkey"))
{
Console.WriteLine(item);
}
}
#endregion
#endregion
#region sorted set
{
client.FlushDb();
string zsett_key = "world_zset";
//添加一个kye 如果有相同的值,则会替换(覆盖)进去,不会因为分数保留
client.AddItemToSortedSet(zsett_key, "cc", 33);
client.AddItemToSortedSet(zsett_key, "cc", 44);
client.AddItemToSortedSet(zsett_key, "cc", 22);
Console.WriteLine("ok");
//获取当前value的结果
Console.WriteLine(client.GetItemIndexInSortedSet(zsett_key, "cc"));
//批量操作多个key ,给多个key 赋值1
client.AddRangeToSortedSet(zsett_key, new List<string>() { "a", "b" }, 1);
foreach (var item in client.GetAllItemsFromSortedSet(zsett_key))
{
Console.WriteLine(item);
}
client.AddItemToSortedSet("蜀国", "刘备", 5);
client.AddItemToSortedSet("蜀国", "关羽", 2);
client.AddItemToSortedSet("蜀国", "张飞", 3);
client.AddItemToSortedSet("魏国", "刘备", 5);
client.AddItemToSortedSet("魏国", "关羽", 2);
client.AddItemToSortedSet("蜀国", "张飞", 3);
//获取 key为蜀国的下标0,到2 //key为蜀国中第0到第2个 ********************
IDictionary<String, double> Dic = client.GetRangeWithScoresFromSortedSet("蜀国", 0, 2);
foreach (var r in Dic)
{
Console.WriteLine(r.Key + ":" + r.Value);
}
}
#endregion
}
set的数据结构中,存储的形式有两种类型,intzset和hashtable。zset使用了ziplist数据结构。
4)list,各种操作api的代码例子
using (RedisClient client = new RedisClient("127.0.0.1", 6379, "12345", 10))
{
//删除当前数据库中的所有Key 默认删除的是db0
client.FlushDb();
//删除所有数据库中的key
//client.FlushAll();
string listid = "world_list";
#region 顺序添加
{
client.FlushDb();
var caocao = new UserInfo() { Id = 1, Name = "李太白" };
client.AddItemToList(listid, JsonConvert.SerializeObject(caocao));
var jiaxu = new UserInfo() { Id = 2, Name = "贾诩" };
client.AddItemToList(listid, JsonConvert.SerializeObject(jiaxu));
}
#endregion
#region 从左侧向list中添加值 追加
{
client.FlushDb();
var liubei = new UserInfo() { Id = 1, Name = "刘备" };
client.PushItemToList(listid, JsonConvert.SerializeObject(liubei));
}
#endregion
#region 从左侧向list中添加值,并设置过期时间
{
client.FlushDb();
var caocao = new UserInfo() { Id = 1, Name = "李太白" };
client.AddItemToList(listid, JsonConvert.SerializeObject(caocao));
var liubei = new UserInfo() { Id = 2, Name = "刘备" };
client.PushItemToList(listid, JsonConvert.SerializeObject(liubei));
//只在内存中存储一秒,针对一个大key,对应的数据要么全部存在,要么全部消失
client.ExpireEntryAt(listid, DateTime.Now.AddSeconds(1));
Console.WriteLine(client.GetListCount(listid));
Task.Delay(1 * 1000).Wait();
Console.WriteLine("1秒之后");
Console.WriteLine(client.GetListCount(listid));
//雪崩 问题:瞬间大量的数据消失-》大量的数据不要一下的全部消失
}
#endregion
#region 从右侧向list中添加值,并设置过期时间 插队
{
client.FlushDb();
var caocao = new UserInfo() { Id = 1, Name = "李太白" };
client.AddItemToList(listid, JsonConvert.SerializeObject(caocao));
var jiaxu = new UserInfo() { Id = 2, Name = "贾诩" };
client.AddItemToList(listid, JsonConvert.SerializeObject(jiaxu));
var gaunyu = new UserInfo() { Id = 3, Name = "关羽" };
//向右追加就是插队
client.PrependItemToList(listid, JsonConvert.SerializeObject(gaunyu));
Console.WriteLine("ok");
var caomegndeng = new UserInfo() { Id = 3, Name = "曹孟德" };
client.PrependItemToList(listid, JsonConvert.SerializeObject(caomegndeng));
client.ExpireEntryAt(listid, DateTime.Now.AddSeconds(1));
Console.WriteLine(client.GetListCount(listid));
Task.Delay(1 * 1000).Wait();
Console.WriteLine("1秒之后");
Console.WriteLine(client.GetListCount(listid));
}
#endregion
#region 为key添加多个值
{
client.FlushDb();
client.AddRangeToList(listid, new List<string>() { "001", "002", "003", "004" });
//批量去读取list中的元素
var lists = client.GetAllItemsFromList(listid);
foreach (var item in lists)
{
Console.WriteLine(item);
}
}
#endregion
#region 获取key中下标为star到end的值集合
{
//获取listid中第0到第1个 ********************
client.AddRangeToList(listid, new List<string>() { "001", "002", "003", "004" });
var lists = client.GetRangeFromList(listid, 0, 1);//从下标0到1的值
foreach (var item in lists)
{
Console.WriteLine(item);
}
}
#endregion
#region list 队列和集合操作
{
client.FlushDb();
var caocao = new UserInfo() { Id = 1, Name = "李太白" };
client.AddItemToList(listid, JsonConvert.SerializeObject(caocao));
var jiaxu = new UserInfo() { Id = 2, Name = "贾诩" };
client.AddItemToList(listid, JsonConvert.SerializeObject(jiaxu));
var gaunyu = new UserInfo() { Id = 3, Name = "关羽" };
client.AddItemToList(listid, JsonConvert.SerializeObject(gaunyu));
//移除尾部 并返回移除的数据 先删再给数据
{
Console.WriteLine(client.RemoveEndFromList(listid));
foreach (var item in client.GetAllItemsFromList(listid))
{
Console.WriteLine(JsonConvert.DeserializeObject<UserInfo>(item).Name);
}
}
//移除头部并返回移除的数据
{
Console.WriteLine(client.RemoveStartFromList(listid));
foreach (var item in client.GetAllItemsFromList(listid))
{
Console.WriteLine(JsonConvert.DeserializeObject<UserInfo>(item).Name);
}
}
//从一个list的尾部移除一个数据,添加到另外一个list的头部,并返回移动的值
{
Console.WriteLine(client.PopAndPushItemBetweenLists(listid, "newlist"));
Console.WriteLine("移动之后新队列的元素结果");
Console.WriteLine(client.GetItemFromList("newlist", 0));
}
//根据下标获取想要的集合元素,不做移除操作
{
var userstr = client.GetItemFromList(listid, 0);
Console.WriteLine(JsonConvert.DeserializeObject<UserInfo>(userstr).Name);
}
//修改当前下标的结果
{
client.SetItemInList(listid, 0, "new value");
}
}
#endregion
}
list数据结构:双向链表(一个元素的结尾记录了下一个元素的指针,这个元素的开头记录了上一个元素的指针),适合做增删动作,直接把链子打开插进去就可以了。