首页 \ 问答 \ 使用jedis管道获取值(Getting values with jedis pipeline)

使用jedis管道获取值(Getting values with jedis pipeline)

我有一个id列表,我想用它来使用java客户端jedis从Redis服务器检索哈希。 正如文档中所提到的,Jedis提供了一种通过声明Response对象然后同步管道来获取值来使用管道的方法:

Pipeline p = jedis.pipelined();
p.set("fool", "bar"); 
p.zadd("foo", 1, "barowitch");  p.zadd("foo", 0, "barinsky"); p.zadd("foo", 0, "barikoviev");
Response<String> pipeString = p.get("fool");
Response<Set<String>> sose = p.zrange("foo", 0, -1);
p.sync(); 

但是,我的列表具有可变长度,每隔几分钟就会不断变化。 因此,我无法预测需要声明的Response对象的数量。 有没有办法解决这个问题,例如:

Pipeline p = jedis.pipelined();
Response<List<List<Map<String,String>>> records;
for (int id: ids)
    records.add(p.hgetAll(id))
p.sync();

I have a list of ids that I want to use to retrieve hashes from a Redis server using the java client jedis. As mentioned in the documentation, Jedis provides a way to use the pipeline by declaring Response objects and then sync the pipeline to get values:

Pipeline p = jedis.pipelined();
p.set("fool", "bar"); 
p.zadd("foo", 1, "barowitch");  p.zadd("foo", 0, "barinsky"); p.zadd("foo", 0, "barikoviev");
Response<String> pipeString = p.get("fool");
Response<Set<String>> sose = p.zrange("foo", 0, -1);
p.sync(); 

However, my list has a variable length that keeps changing every few minutes. Thus, I am not able to predict the number of Response objects that I need to declare. Is there a way to get around that, something like:

Pipeline p = jedis.pipelined();
Response<List<List<Map<String,String>>> records;
for (int id: ids)
    records.add(p.hgetAll(id))
p.sync();

原文:https://stackoverflow.com/questions/30538085
更新时间:2023-06-23 08:06

最满意答案

在第9章第4节“编写新的控制结构”中,Odersky说(所有的拼写错误都是我的):

让客户端代码看起来更像一个内置控制结构的一种方法是使用大括号而不是括号来包围参数列表。 在Scala中只传入一个参数的任何方法调用中,您可以选择使用大括号来包围参数而不是括号。

在第3章“第7步”中,他还谈到了apply

当您将一个或多个值的括号应用于变量时,Scala会将代码转换为对该变量名为apply的方法的调用。

一起应用这两个规则会导致

Thingy { ... }

被重写入

Thingy( ... )

然后进入

Thingy.apply(...)

它隐含的参数变得更有趣。 例如,对于类型类,通常定义一个如下的apply

trait MyTypeClass[A]
object MyTypeClass {
  def apply[A](implicit inst: MyTypeClass[A]) = inst
}

以便值表达式MyTypeClass[Int]解析为MyTypeClass.apply[Int](someImplicitlyInjectedInstance) ,因此是MyTypeClass[Int]类型的值。 因此, MyTypeClass[Int]值和MyTypeClass[Int]类型看起来完全一样。


事实上,没有明确告诉特质也可以有伴侣对象:有很多事情没有被明确地告知。 例如,没有明确告诉你可以在Scala中写下类型lambda表达式,但事实证明你确实可以。


In Chapter 9, section 4 "Writing new control structures", Odersky says (all typos are mine):

One way in which you can make the client code look a bit more like a built-in control structure is to use curly braces instead of parentheses to surround the argument list. In any method invocation in Scala in which you're passing in exactly one argument, you can opt to use curly braces to surround the argument instead of parentheses.

In Chapter 3, "Step 7" he also talks about apply:

When you apply parentheses surrounding one or more values to a variable, Scala will transform the code into an invocation of a method named apply on that variable.

Applying these two rules together results in

Thingy { ... }

being rewritten into

Thingy( ... )

and then into

Thingy.apply(...)

It gets even funnier with implicit parameters. For example, for typeclasses it is common to define an apply that looks as follows:

trait MyTypeClass[A]
object MyTypeClass {
  def apply[A](implicit inst: MyTypeClass[A]) = inst
}

so that the value-expression MyTypeClass[Int] desugars into MyTypeClass.apply[Int](someImplicitlyInjectedInstance), and thus is a value of type MyTypeClass[Int]. So, both the value MyTypeClass[Int] and the type MyTypeClass[Int] look exactly the same.


On the fact that it is not told explicitly that traits can also have companion objects: there are a lot of things that aren't told explicitly. For example, it was not told explicitly that you can write down type-lambdas in Scala, but it turned out that you actually can.

相关问答

更多
  • Scala中的这个问题当然并非没有讽刺意味,但即使在Java空间中,也可能存在有关java.lang.Object ,它的实例,它的类和它的类的实例的命名问题。 对于Scala,我提出以下术语: 调用实例化对象“实例” 调用object的“对象” 最后,我会说OO的概念包括对象(在模块和组合中)与对象(从实例的意义上)一样多。 无论你如何决定,一致性都是关键,特别是对于教学。 This issue in Scala is certainly not without irony, but even in th ...
  • 当您调用方法时,您可以将参数传递给它或在括号内,或者如果只有一个参数则不传递参数。 这里fx {10}传递一个返回类型为Int {10}的块而没有括号。 它相当于fx({10}) 。 这里List(1, 3, 5, 7) filter { _ > 5}你还传递了返回类型Int => Boolean块{ _ > 5}而没有括号。 在上述两种情况中,最后一个块语句是合法表达式 这里List.apply{"A", "B"}你没有括号传递一个块{"A", "B"} ,它相当于List.apply({"A", "B" ...
  • 在Scala API文档中 ,您会看到一个小图标 在左侧的列表中。 如果你点击它,你可以找到所提到的类或特征的伴侣对象的文档。 标准库中的大多数集合类和许多其他类都具有带有apply方法的伴随对象。 对于案例类,Scala编译器会自动使用apply方法(以及其他方法)创建伴随对象。 In the Scala API documentation, you see a little icon in the list on the left side. If you click on that, you go t ...
  • case类伴随对象实现FunctionN的原因在于,case类生成类和工厂方法,而不是伴随对象。 当我们将提取器添加到Scala时,将工厂方法转换为具有应用和未应用方法的完整协同对象变得更有意义。 但是,由于工厂方法确实符合FunctionN,所以配套对象也需要符合。 [编辑]这就是说,让伴侣对象显示为自己的名字,而不是“功能” The reason why case class companion objects implement FunctionN is that before, case class ...
  • 你的代码有几个问题。 首先,正如你已经说过的那样,类型将被擦除,第二个object s( object FooCompanionObject[f <: Foo] )不接受类型参数,第三个object s不能扩展( object Bar extends FooCompanionObject )。 要做你想做的事情,你必须为你的伴侣对象创建一个抽象基类,它需要一个类型参数,如果你愿意,它可以被限制为一个特定的类型,并且必须在ClassTag上进行上下文绑定。 然后,您可以通过调用runtimeClass上的Cl ...
  • 在第9章第4节“编写新的控制结构”中,Odersky说(所有的拼写错误都是我的): 让客户端代码看起来更像一个内置控制结构的一种方法是使用大括号而不是括号来包围参数列表。 在Scala中只传入一个参数的任何方法调用中,您可以选择使用大括号来包围参数而不是括号。 在第3章“第7步”中,他还谈到了apply : 当您将一个或多个值的括号应用于变量时,Scala会将代码转换为对该变量名为apply的方法的调用。 一起应用这两个规则会导致 Thingy { ... } 被重写入 Thingy( ... ) 然后 ...
  • 适用于我(见下文)。 你有没有在同一个文件中定义它们? Welcome to Scala version 2.9.0.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24). Type in expressions to have them evaluated. Type :help for more information. scala> :paste // Entering paste mode (ctrl-D to finish) sea ...
  • 在这些情况下, Set和Map分别只是res1和res2的声明类型。 要获取实际类型,请尝试以下方法: Set(1, 2, 3).getClass.getCanonicalName Map(1 -> "a", 2 -> "b").getClass.getCanonicalName 现在您将看到它们不仅仅是traits实例, scala.collection.immutable.Set和scala.collection.immutable.Map ; 它们是实现这些特征的具体类的实例。 如果您想了解有关如何 ...
  • 对我来说这看起来像个错误。 可能与内联函数(例如apply )和伴随对象有关。 我建议搜索JetBrains Bug&Issue Tracker ,如果你找不到类似的东西,就会产生一个新问题。 与此同时,我看到了一些替代方案: 使用this (不理想): fun fromB(b: B): A { return A().apply { this.value1 = b.value3 this.value2 = b.value4 } } 将value1和value ...
  • 第二个错误只是因为REPL的运行方式。 在REPL中,必须使用:paste模式一起定义协同服务器; 但是,在包对象中,这不是问题。 所以,另一种方法 - 写val IntSeq = Seq - 实际上会起作用。 The second error is just because of the way REPL operates. In REPL, the companions must be defined together using the :paste mode; however, in the pac ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。