List 主要用于队列和栈,先进先出,后进先出等。
using (RedisClient client = new RedisClient("127.0.0.1", 6379)) { //删除当前数据库中的所有Key 默认删除的是db0 client.FlushDb(); //删除所有数据库中的key client.FlushAll(); string key = "keyiwang"; client.AddItemToList(key, "001"); client.AddItemToList(key, "002"); client.AddItemToList("key1", "AAA"); client.AddItemToList("key1", "BBB"); client.AddItemToList("key1", "CCC"); //client.PushItemToList(key, "003"); //插入到最后面 //client.PrependItemToList(key, "004"); //插入到最前面 //var list = client.GetRangeFromList(key, 0, 2);//根据下标来取值,比如用于分页 //var str = client.RemoveEndFromList(key); //从尾部移除,并返回移除的数据 ,后进先出。在实际队列处理后,需要进行适当的进程阻塞。 //Console.WriteLine(str); //var str = client.RemoveStartFromList(key); //从尾部移除,并返回移除的数据 ,先进先出 //Console.WriteLine(str); Console.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff")); var items= client.BlockingDequeueItemFromList("key1",null); Console.WriteLine(items); var items1 = client.BlockingDequeueItemFromList("key1",null); Console.WriteLine(items1); items1 = client.BlockingDequeueItemFromList("key1",null); Console.WriteLine(items1); //阻塞命令:从list中keys的尾部移除一个值,并返回移除的值,阻塞时间为sp,当这个队列里面的数据为空了,才会进行阻塞当前线程。阻塞的目的是等待新数据产生,避免一直循环空操作(类似长轮训,当该listid又产生新数据时,它立马返回)。 items1 = client.BlockingDequeueItemFromList("key1", TimeSpan.FromSeconds(60)); Console.WriteLine(items1); Console.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff")); //详细可查看 博客:https://www.cnblogs.com/knowledgesea/p/5032101.html }
Net操作Redis数据类型List
public class DoRedisList:DoRedisBase { #region 赋值 /// <summary> /// 从左侧向list中添加值 /// </summary> public void LPush(string key,string value) { RedisBase.Core.PushItemToList(key,value); } /// <summary> /// 从左侧向list中添加值,并设置过期时间 /// </summary> public void LPush(string key, string value,DateTime dt) { RedisBase.Core.PushItemToList(key, value); RedisBase.Core.ExpireEntryAt(key,dt); } /// <summary> /// 从左侧向list中添加值,设置过期时间 /// </summary> public void LPush(string key, string value, TimeSpan sp) { RedisBase.Core.PushItemToList(key, value); RedisBase.Core.ExpireEntryIn(key, sp); } /// <summary> /// 从左侧向list中添加值 /// </summary> public void RPush(string key, string value) { RedisBase.Core.PrependItemToList(key,value); } /// <summary> /// 从右侧向list中添加值,并设置过期时间 /// </summary> public void RPush(string key, string value, DateTime dt) { RedisBase.Core.PrependItemToList(key, value); RedisBase.Core.ExpireEntryAt(key, dt); } /// <summary> /// 从右侧向list中添加值,并设置过期时间 /// </summary> public void RPush(string key, string value, TimeSpan sp) { RedisBase.Core.PrependItemToList(key, value); RedisBase.Core.ExpireEntryIn(key, sp); } /// <summary> /// 添加key/value /// </summary> public void Add(string key, string value) { RedisBase.Core.AddItemToList(key,value); } /// <summary> /// 添加key/value ,并设置过期时间 /// </summary> public void Add(string key, string value,DateTime dt) { RedisBase.Core.AddItemToList(key, value); RedisBase.Core.ExpireEntryAt(key,dt); } /// <summary> /// 添加key/value。并添加过期时间 /// </summary> public void Add(string key, string value,TimeSpan sp) { RedisBase.Core.AddItemToList(key, value); RedisBase.Core.ExpireEntryIn(key,sp); } /// <summary> /// 为key添加多个值 /// </summary> public void Add(string key, List<string> values) { RedisBase.Core.AddRangeToList(key,values); } /// <summary> /// 为key添加多个值,并设置过期时间 /// </summary> public void Add(string key, List<string> values,DateTime dt) { RedisBase.Core.AddRangeToList(key, values); RedisBase.Core.ExpireEntryAt(key,dt); } /// <summary> /// 为key添加多个值,并设置过期时间 /// </summary> public void Add(string key, List<string> values,TimeSpan sp) { RedisBase.Core.AddRangeToList(key, values); RedisBase.Core.ExpireEntryIn(key,sp); } #endregion #region 获取值 /// <summary> /// 获取list中key包含的数据数量 /// </summary> public long Count(string key) { return RedisBase.Core.GetListCount(key); } /// <summary> /// 获取key包含的所有数据集合 /// </summary> public List<string> Get(string key) { return RedisBase.Core.GetAllItemsFromList(key); } /// <summary> /// 获取key中下标为star到end的值集合 /// </summary> public List<string> Get(string key,int star,int end) { return RedisBase.Core.GetRangeFromList(key,star,end); } #endregion #region 阻塞命令,阻塞类似长连接,当该listid被移除完时,进行长连接等待,有新数据就立刻移除并返回。 /// <summary> /// 阻塞命令:从list中keys的尾部移除一个值,并返回移除的值,阻塞时间为sp /// </summary> public string BlockingPopItemFromList(string key,TimeSpan? sp) { return RedisBase.Core.BlockingDequeueItemFromList(key,sp); } /// <summary> /// 阻塞命令:从list中keys的尾部移除一个值,并返回移除的值,阻塞时间为sp,只有数组中所有key的数据被移除完之后,才会进行阻塞。移除顺序按数组的先后顺序,需要在循环里面处理 /// </summary> public ItemRef BlockingPopItemFromLists(string[] keys, TimeSpan? sp) { return RedisBase.Core.BlockingPopItemFromLists(keys, sp); } /// <summary> /// 阻塞命令:从list中keys的尾部移除一个值,并返回移除的值,阻塞时间为sp /// </summary> public string BlockingDequeueItemFromList(string key, TimeSpan? sp) { return RedisBase.Core.BlockingDequeueItemFromList(key, sp); } /// <summary> /// 阻塞命令:从list中keys的尾部移除一个值,并返回移除的值,阻塞时间为sp /// </summary> public ItemRef BlockingDequeueItemFromLists(string[] keys, TimeSpan? sp) { return RedisBase.Core.BlockingDequeueItemFromLists(keys, sp); } /// <summary> /// 阻塞命令:从list中key的头部移除一个值,并返回移除的值,阻塞时间为sp /// </summary> public string BlockingRemoveStartFromList(string keys, TimeSpan? sp) { return RedisBase.Core.BlockingRemoveStartFromList(keys, sp); } /// <summary> /// 阻塞命令:从list中key的头部移除一个值,并返回移除的值,阻塞时间为sp /// </summary> public ItemRef BlockingRemoveStartFromLists(string[] keys, TimeSpan? sp) { return RedisBase.Core.BlockingRemoveStartFromLists(keys, sp); } /// <summary> /// 阻塞命令:从list中一个fromkey的尾部移除一个值,添加到另外一个tokey的头部,并返回移除的值,阻塞时间为sp /// </summary> public string BlockingPopAndPushItemBetweenLists(string fromkey, string tokey, TimeSpan? sp) { return RedisBase.Core.BlockingPopAndPushItemBetweenLists(fromkey, tokey, sp); } #endregion #region 删除 /// <summary> /// 从尾部移除数据,返回移除的数据 /// </summary> public string PopItemFromList(string key) { return RedisBase.Core.PopItemFromList(key); } /// <summary> /// 移除list中,key/value,与参数相同的值,并返回移除的数量 /// </summary> public long RemoveItemFromList(string key,string value) { return RedisBase.Core.RemoveItemFromList(key,value); } /// <summary> /// 从list的尾部移除一个数据,返回移除的数据 /// </summary> public string RemoveEndFromList(string key) { return RedisBase.Core.RemoveEndFromList(key); } /// <summary> /// 从list的头部移除一个数据,返回移除的值 /// </summary> public string RemoveStartFromList(string key) { return RedisBase.Core.RemoveStartFromList(key); } #endregion #region 其它 /// <summary> /// 从一个list的尾部移除一个数据,添加到另外一个list的头部,并返回移动的值 /// </summary> public string PopAndPushItemBetweenLists(string fromKey, string toKey) { return RedisBase.Core.PopAndPushItemBetweenLists(fromKey,toKey); } #endregion }