C# 通过ServiceStack 操作Redis——List类型的使用及示例

2023-10-21 18:40:32 浏览数 (2)

Redis list的实现为一个双向链表,即可以支持反向查找和遍历,更方便操作,不过带来了部分额外的内存开销,

代码语言:javascript复制
/// <summary>
    ///  Redis list的实现为一个双向链表,即可以支持反向查找和遍历,更方便操作,不过带来了部分额外的内存开销,
    ///  Redis内部的很多实现,包括发送缓冲队列等也都是用的这个数据结构。  
    /// </summary>
    public class RedisListService : RedisBase
    {
        #region 赋值
        /// <summary>
        /// 从左侧向list中添加值
        /// </summary>
        public void LPush(string key, string value)
        {
            base.iClient.PushItemToList(key, value);
        }
        /// <summary>
        /// 从左侧向list中添加值,并设置过期时间
        /// </summary>
        public void LPush(string key, string value, DateTime dt)
        {

            base.iClient.PushItemToList(key, value);
            base.iClient.ExpireEntryAt(key, dt);
        }
        /// <summary>
        /// 从左侧向list中添加值,设置过期时间
        /// </summary>
        public void LPush(string key, string value, TimeSpan sp)
        {
            base.iClient.PushItemToList(key, value);
            base.iClient.ExpireEntryIn(key, sp);
        }
        /// <summary>
        /// 从右侧向list中添加值
        /// </summary>
        public void RPush(string key, string value)
        {
            base.iClient.PrependItemToList(key, value);
        }
        /// <summary>
        /// 从右侧向list中添加值,并设置过期时间
        /// </summary>    
        public void RPush(string key, string value, DateTime dt)
        {
            base.iClient.PrependItemToList(key, value);
            base.iClient.ExpireEntryAt(key, dt);
        }
        /// <summary>
        /// 从右侧向list中添加值,并设置过期时间
        /// </summary>        
        public void RPush(string key, string value, TimeSpan sp)
        {
            base.iClient.PrependItemToList(key, value);
            base.iClient.ExpireEntryIn(key, sp);
        }
        /// <summary>
        /// 添加key/value
        /// </summary>     
        public void Add(string key, string value)
        {
            base.iClient.AddItemToList(key, value);
        }
        /// <summary>
        /// 添加key/value ,并设置过期时间
        /// </summary>  
        public void Add(string key, string value, DateTime dt)
        {
            base.iClient.AddItemToList(key, value);
            base.iClient.ExpireEntryAt(key, dt);
        }
        /// <summary>
        /// 添加key/value。并添加过期时间
        /// </summary>  
        public void Add(string key, string value, TimeSpan sp)
        {
            base.iClient.AddItemToList(key, value);
            base.iClient.ExpireEntryIn(key, sp);
        }
        /// <summary>
        /// 为key添加多个值
        /// </summary>  
        public void Add(string key, List<string> values)
        {
            base.iClient.AddRangeToList(key, values);
        }
        /// <summary>
        /// 为key添加多个值,并设置过期时间
        /// </summary>  
        public void Add(string key, List<string> values, DateTime dt)
        {
            base.iClient.AddRangeToList(key, values);
            base.iClient.ExpireEntryAt(key, dt);
        }
        /// <summary>
        /// 为key添加多个值,并设置过期时间
        /// </summary>  
        public void Add(string key, List<string> values, TimeSpan sp)
        {
            base.iClient.AddRangeToList(key, values);
            base.iClient.ExpireEntryIn(key, sp);
        }
        #endregion

        #region 获取值
        /// <summary>
        /// 获取list中key包含的数据数量
        /// </summary>  
        public long Count(string key)
        {
            return base.iClient.GetListCount(key);
        }
        /// <summary>
        /// 获取key包含的所有数据集合
        /// </summary>  
        public List<string> Get(string key)
        {
            return base.iClient.GetAllItemsFromList(key);
        }
        /// <summary>
        /// 获取key中下标为star到end的值集合 
        /// </summary>  
        public List<string> Get(string key, int star, int end)
        {
            return base.iClient.GetRangeFromList(key, star, end);
        }
        #endregion

        #region 阻塞命令
        /// <summary>
        ///  阻塞命令:从list为key的尾部移除一个值,并返回移除的值,阻塞时间为sp
        /// </summary>  
        public string BlockingPopItemFromList(string key, TimeSpan? sp)
        {
            return base.iClient.BlockingPopItemFromList(key, sp);
        }
        /// <summary>
        ///  阻塞命令:从多个list中尾部移除一个值,并返回移除的值&key,阻塞时间为sp
        /// </summary>  
        public ItemRef BlockingPopItemFromLists(string[] keys, TimeSpan? sp)
        {
            return base.iClient.BlockingPopItemFromLists(keys, sp);
        }


        /// <summary>
        ///  阻塞命令:从list中keys的尾部移除一个值,并返回移除的值,阻塞时间为sp
        /// </summary>  
        public string BlockingDequeueItemFromList(string key, TimeSpan? sp)
        {
            return base.iClient.BlockingDequeueItemFromList(key, sp);
        }

        /// <summary>
        /// 阻塞命令:从多个list中尾部移除一个值,并返回移除的值&key,阻塞时间为sp
        /// </summary>  
        public ItemRef BlockingDequeueItemFromLists(string[] keys, TimeSpan? sp)
        {
            return base.iClient.BlockingDequeueItemFromLists(keys, sp);
        }

        /// <summary>
        /// 阻塞命令:从list中一个fromkey的尾部移除一个值,添加到另外一个tokey的头部,并返回移除的值,阻塞时间为sp
        /// </summary>  
        public string BlockingPopAndPushItemBetweenLists(string fromkey, string tokey, TimeSpan? sp)
        {
            return base.iClient.BlockingPopAndPushItemBetweenLists(fromkey, tokey, sp);
        }
        #endregion

        #region 删除
        /// <summary>
        /// 从尾部移除数据,返回移除的数据
        /// </summary>  
        public string PopItemFromList(string key)
        {
            var sa = base.iClient.CreateSubscription();
            return base.iClient.PopItemFromList(key);
        }
        /// <summary>
        /// 从尾部移除数据,返回移除的数据
        /// </summary>  
        public string DequeueItemFromList(string key)
        {
            return base.iClient.DequeueItemFromList(key);
        }

        /// <summary>
        /// 移除list中,key/value,与参数相同的值,并返回移除的数量
        /// </summary>  
        public long RemoveItemFromList(string key, string value)
        {
            return base.iClient.RemoveItemFromList(key, value);
        }
        /// <summary>
        /// 从list的尾部移除一个数据,返回移除的数据
        /// </summary>  
        public string RemoveEndFromList(string key)
        {
            return base.iClient.RemoveEndFromList(key);
        }
        /// <summary>
        /// 从list的头部移除一个数据,返回移除的值
        /// </summary>  
        public string RemoveStartFromList(string key)
        {
            return base.iClient.RemoveStartFromList(key);
        }
        #endregion

        #region 其它
        /// <summary>
        /// 从一个list的尾部移除一个数据,添加到另外一个list的头部,并返回移动的值
        /// </summary>  
        public string PopAndPushItemBetweenLists(string fromKey, string toKey)
        {
            return base.iClient.PopAndPushItemBetweenLists(fromKey, toKey);
        }


        public void TrimList(string key, int start, int end)
        {
            base.iClient.TrimList(key, start, end);
        }
        #endregion

        #region 发布订阅
        public void Publish(string channel, string message)
        {
            base.iClient.PublishMessage(channel, message);
        }

        public void Subscribe(string channel, Action<string, string, IRedisSubscription> actionOnMessage)
        {
            var subscription = base.iClient.CreateSubscription();
            subscription.OnSubscribe = c =>
            {
                Console.WriteLine($"订阅频道{c}");
                Console.WriteLine();
            };
            //取消订阅
            subscription.OnUnSubscribe = c =>
            {
                Console.WriteLine($"取消订阅 {c}");
                Console.WriteLine();
            };
            subscription.OnMessage  = (c, s) =>
            {
                actionOnMessage(c, s, subscription);
            };
            Console.WriteLine($"开始启动监听 {channel}");
            subscription.SubscribeToChannels(channel); //blocking
        }

        public void UnSubscribeFromChannels(string channel)
        {
            var subscription = base.iClient.CreateSubscription();
            subscription.UnSubscribeFromChannels(channel);
        }
        #endregion
    }

我在这里将注解添加上去,有不正确之处,欢迎指正、

代码语言:javascript复制
                //尾部追加数据
                client.PushItemToList(listId, value);
                //设置过期时间
                client.ExpireEntryAt(key, expireAt);
                //设置滑动过期时间
                client.ExpireEntryIn(key, expireIn);
                client.PrependItemToList(listId, value);
                client.AddItemToList(listId, value);
                client.AddRangeToList(listId, values);

                //获取listId包含的数据数量
                client.GetListCount(listId);
                //获取listId包含的所有数据集合
                client.GetAllItemsFromList(listId);
                //获取listId中下标为startingFrom到endingAt的值集合 
                client.GetRangeFromList(listId, startingFrom, endingAt);


                //  阻塞命令:从listId的头部移除一个值,并返回移除的值,阻塞时间为sp
                client.BlockingPopItemFromList(listId, timeOut);
                //  阻塞命令:从多个listId的尾部移除一个值,并返回移除的值,阻塞时间为sp
                client.BlockingPopItemFromLists(listIds, timeOut);
                // 阻塞命令:从listId的尾部移除一个值,并返回移除的值,阻塞时间为sp
                client.BlockingDequeueItemFromList(listId, timeOut);
                // 阻塞命令:从多个listId的尾部移除一个值,并返回移除的值,阻塞时间为sp
                client.BlockingDequeueItemFromLists(listIds, timeOut);
                //阻塞命令:从一个fromListId的头部移除一个值,添加到另外一个toListId的头部,并返回移除的值,阻塞时间为sp
                client.BlockingPopAndPushItemBetweenLists(fromListId, toListId, timeOut);

                //从尾部移除数据,返回移除的数据
                client.PopItemFromList(listId);
                // 从尾部移除数据,返回移除的数据
                client.DequeueItemFromList(listId);
                //移除listId中,与value相同的值的数据,并返回移除的数量
                client.RemoveItemFromList(listId, value);
                // 从list的尾部移除一个数据,返回移除的数据
                client.RemoveEndFromList(listId);
                //从list的头部移除一个数据,返回移除的数据
                client.RemoveStartFromList(listId);
                //从一个list的尾部移除一个数据,添加到另外一个list的头部,并返回移动的值
                client.PopAndPushItemBetweenLists(fromListId, toListId);

添加

代码语言:javascript复制
                //1. 添加
                //1.1 
                client.PushItemToList("group1", "你好1");
                client.PushItemToList("group1", "你好2");
                client.PushItemToList("group1", "你好3");
                client.PushItemToList("group1", "你好4");
                client.PushItemToList("group1", "你好5");
                //1.2
                client.PrependItemToList("group2", "早上1");
                client.PrependItemToList("group2", "早上2");
                client.PrependItemToList("group2", "早上3");
                client.PrependItemToList("group2", "早上4");
                client.PrependItemToList("group2", "早上5");
                //1.3
                client.AddItemToList("group3", "中午1");
                client.AddItemToList("group3", "中午2");
                client.AddItemToList("group3", "中午3");
                client.AddItemToList("group3", "中午4");
                client.AddItemToList("group3", "中午5");
                //1.4
                List<string> values1 = new List<string>() { "晚上1", "晚上2", "晚上3", "晚上4", "晚上5" };
                client.AddRangeToList("group4",values1);

 插入

代码语言:javascript复制
                //2.插入
                //2.1尾部插入
                client.PushItemToList("group3", "a1");
                client.PushItemToList("group3", "a2");
                client.PushItemToList("group3", "a3");
                //2.2头部插入
                client.PrependItemToList("group3", "b1");
                client.PrependItemToList("group3", "b2");
                client.PrependItemToList("group3", "b3");

 从效果图看,Push方式是往链表的尾部逐步插入数据,Prepend是往链表的头部逐步插入数据

移除

代码语言:javascript复制
                //3.删除
                client.PopItemFromList("group1");
                client.PopItemFromList("group1");
                client.PopItemFromList("group1");
                client.DequeueItemFromList("group3");
                client.DequeueItemFromList("group3");

                client.RemoveEndFromList("group4");
                client.RemoveStartFromList("group4");
                client.RemoveStartFromList("group4");

对比前后的数据变化,可以得出Pop、Dequeue方法效果都是一样,删除链表尾部数据。RemoveEnd 删除链表尾部数据、RemoveStart删除链表头部数据

 订阅发布

代码语言:javascript复制
                //4.发布订阅
                //4.1(服务器)发布消息
                client.PublishMessage(toChanner, message);
                //4.2 (客户端)订阅
               var subscription= client.CreateSubscription();
                //4.2.1(客户端)订阅频道
                subscription.SubscribeToChannels(channels);
                //4.2.2 (客户端)取消频道
                subscription.UnSubscribeFromChannels(channels);

其它的一些api方法,就不做数据效果展示了~

0 人点赞