[C#] ServiceStack.Redis如何批量的pop資料?

來源:互聯網
上載者:User

標籤:

要安全的批量pop資料,有兩個辦法:

1、用事務(不用事務的話可能導致重複讀。ServiceStack的pipeline是沒有內建事務的。)

2、執行lua指令碼


我這裡提供用事務的實現方法:

public static string ReadLine(RedisNativeClient cln){MethodInfo mi = cln.GetType().GetMethod("ReadLine", BindingFlags.NonPublic | BindingFlags.Instance);string ret = (string)mi.Invoke(cln, new object[] { });return ret;}public static List<string> BatchDequeue(RedisNativeClient cln, string listID, int max_count){List<string> ret = new List<string>();var uListId = Encoding.UTF8.GetBytes(listID);var pipeline = cln.CreatePipelineCommand();pipeline.WriteCommand(Commands.Multi);pipeline.WriteCommand(Commands.LRange, uListId, Encoding.UTF8.GetBytes("0"), Encoding.UTF8.GetBytes((max_count - 1).ToString()));pipeline.WriteCommand(Commands.LTrim, uListId, Encoding.UTF8.GetBytes(max_count.ToString()), Encoding.UTF8.GetBytes("-1"));pipeline.WriteCommand(Commands.Exec);pipeline.Flush();var a1 = ReadLine(cln);//忽略Multi的OKvar a2 = ReadLine(cln);//忽略LRANGE的QUEUEDvar a3 = ReadLine(cln);//忽略LTRIM的QUEUEDvar a4 = ReadLine(cln);//忽略*2var b = cln.ReceiveMessages();foreach (var item in b){string ss = Encoding.UTF8.GetString(item);ret.Add(ss);}ReadLine(cln);//忽略EXEC的OKreturn ret;}




[C#] ServiceStack.Redis如何批量的pop資料?

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.