zoukankan      html  css  js  c++  java
  • [C#] ServiceStack.Redis如何批量的pop数据?

    要安全的批量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的OK
    	var a2 = ReadLine(cln);		//忽略LRANGE的QUEUED
    	var a3 = ReadLine(cln);		//忽略LTRIM的QUEUED
    	var a4 = ReadLine(cln);		//忽略*2
    	var b = cln.ReceiveMessages();
    	foreach (var item in b)
    	{
    		string ss = Encoding.UTF8.GetString(item);
    		ret.Add(ss);
    	}
    	ReadLine(cln);		//忽略EXEC的OK
    
    	return ret;
    }
    




  • 相关阅读:
    NOJ 1116 哈罗哈的大披萨 【淡蓝】 状态压缩DP
    优先队列原理与实现【转】
    testC-I
    济南NOIP冬令营 选拔(select)
    P4747 D’s problem(d)
    P4746 C’s problem(c)
    P4745 B’s problem(b)
    P4744 A’s problem(a)
    [bzoj] 1004: [HNOI2008]Cards
    NOIP2013 表达式求值
  • 原文地址:https://www.cnblogs.com/hehe520/p/6330322.html
Copyright © 2011-2022 走看看