草庐IT

time - redis:EVAL 和 TIME

coder 2023-07-17 原文

我喜欢 Redis 的 Lua 脚本,但我对 TIME 有很大的疑问。

我将事件存储在 SortedSet 中

分数就是时间,因此在我的应用程序中我可以查看给定时间窗口内的所有事件。

redis.call('zadd', myEventsSet, TIME, EventID);

好的,但这不起作用 - 我无法访问 TIME(服务器时间)。

有什么方法可以从服务器获取时间而不将其作为参数传递给我的 lua 脚本?还是打发时间作为争论的最佳方式?

最佳答案

这是明确禁止的(据我所知)。这背后的原因是你的 lua 函数必须是确定性的并且只依赖于它们的参数。如果此 Lua 调用被复制到具有不同系统时间的从站怎么办?

编辑(Linus G Thiel):这是正确的。来自redis EVAL docs :

Scripts as pure functions

A very important part of scripting is writing scripts that are pure functions. Scripts executed in a Redis instance are replicated on slaves by sending the script -- not the resulting commands.

[...]

In order to enforce this behavior in scripts Redis does the following:

  • Lua does not export commands to access the system time or other external state.
  • Redis will block the script with an error if a script calls a Redis command able to alter the data set after a Redis random command like RANDOMKEY, SRANDMEMBER, TIME. This means that if a script is read-only and does not modify the data set it is free to call those commands. Note that a random command does not necessarily mean a command that uses random numbers: any non-deterministic command is considered a random command (the best example in this regard is the TIME command).

关于为什么会这样,如何在不同的场景中处理这个问题,以及脚本可以使用哪些 Lua 库,有大量的信息。我建议您阅读整个文档!

关于time - redis:EVAL 和 TIME,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12244873/

有关time - redis:EVAL 和 TIME的更多相关文章

  1. ruby - 为什么在类上调用 instance_eval() 时定义类方法? - 2

    Foo=Class.newFoo.instance_evaldodefinstance_bar"instance_bar"endendputsFoo.instance_bar#=>"instance_bar"putsFoo.new.instance_bar#=>undefinedmethod‘instance_bar’我的理解是调用instance_eval在对象上应该允许您为该对象定义实例变量或方法。但是在上面的例子中,当你在类Foo上调用它来定义instance_bar方法时,instance_bar变成了一个可以用“Foo.instance_bar”调用的类方法。很明显这段代码没

  2. ruby - Time.to_i 是否总是以 UTC 返回自 EPOCH 以来的秒数? - 2

    无论时间在哪个时区表示,时区差异是否总是被忽略?直觉上,对于那些使用UTC+2的人来说,从EPOCH开始经过的秒数应该更高。然而,事实并非如此。 最佳答案 Epoch基于utc时区https://en.wikipedia.org/wiki/Unix_time它与您当前所在的时区无关。 关于ruby-Time.to_i是否总是以UTC返回自EPOCH以来的秒数?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.

  3. 带有 attr_accessor 的类上的 Ruby instance_eval - 2

    我了解instance_eval和class_eval之间的基本区别。我在玩弄时发现的是一些涉及attr_accessor的奇怪东西。这是一个例子:A=Class.newA.class_eval{attr_accessor:x}a=A.newa.x="x"a.x=>"x"#...expectedA.instance_eval{attr_accessor:y}A.y="y"=>NoMethodError:undefinedmethod`y='forA:Classa.y="y"=>"y"#WHATTT?这是怎么回事:instance_eval没有访问我们的A类(对象)然后它实际上将它添加到

  4. ruby - 为什么这个 eval 在 Ruby 中不起作用 - 2

    你能解释一下吗?我想评估来自两个不同来源的值和计算。一个消息来源为我提供了以下信息(以编程方式):'a=2'第二个来源给了我这个表达式来评估:'a+3'这个有效:a=2eval'a+3'这也有效:eval'a=2;a+3'但我真正需要的是这个,但它不起作用:eval'a=2'eval'a+3'我想了解其中的区别,以及如何使最后一个选项起作用。感谢您的帮助。 最佳答案 您可以创建一个Binding,并将相同的绑定(bind)与每个eval相关联调用:1.9.3p194:008>b=binding=>#1.9.3p194:009>eva

  5. ruby - 模块中的 instance_eval 与 class_eval - 2

    classFooincludeModule.new{class_eval"deflab;puts'm'end"}deflabsuperputs'c'endendFoo.new.lab#=>mc======================================================================classFooincludeModule.new{instance_eval"deflab;puts'm'end"}deflabsuperputs'c'endend注意这里我把class_eval改成了instance_evalFoo.new.labresc

  6. ruby - 无需 eval 即时创建 Ruby 类 - 2

    我需要动态创建一个Ruby类,即动态地从ActiveRecord::Base派生。我暂时使用eval:eval%Q{class::#{klass}是否有一种等效的、至少同样简洁的方法可以在不使用eval的情况下执行此操作? 最佳答案 您可以使用Class类,其中的类是实例。困惑了吗?;)cls=Class.new(ActiveRecord::Base)doself.table_name=table_nameendcls.new 关于ruby-无需eval即时创建Ruby类,我们在Stac

  7. ruby - 调用 instance_eval(&lambda) 传递当前上下文时出现错误 'wrong number of arguments' - 2

    要清楚-此代码运行完美-codewithproc但如果我将Proc.new更改为lambda,则会出现错误ArgumentError:wrongnumberofarguments(1for0)这可能是因为instance_eval想要将self作为参数传递,而lambda将其视为一种方法并且不接受未知参数?有两个例子-第一个是工作:classRuledefget_ruleProc.new{putsname}endendclassPersonattr_accessor:namedefinit_rule@name="ruby"instance_eval(&Rule.new.get_rule

  8. ruby - Ruby Time#dst 如何运行? - 2

    这可能是一个愚蠢的问题,表明我对夏令时的基础知识缺乏了解,但根据标题,Time.dst?如何知道时间对象确实是真还是假?大概这必须通过日期和时区的组合来辨别,但这没有意义,因为时区中的低纬度不使用夏令时?因此肯定它必须需要位置来识别#dst??我错过了什么? 最佳答案 为了处理时区和夏令时,Ruby和其他所有东西一样,是callinglocaltime_rCfunction.这将时间放入名为tm的C结构中其中包括一个名为isdst的字段。Ruby正在读取该标志。localtime_r首先通过从全局tzname变量获取您的时区来计算i

  9. 已解决socket.timeout : The read operation timed out - 2

    已解决(pip安装模块超时,利用四种国内镜像源完美解决)WARENTING:Retrying(Retry(total=4,connect=None,read=None,redirect=None,status=None))afterconnectionbrokenby‘ConnectTimeoutError(pip._vendor.urllib3.connection.HTTPSConnectionobjectatOx00001D6OE4F4A940>,‘Connectiontopypi.orgtimedout.(connecttimeout=15)’)’':/simple/pip/socke

  10. ruby - 如何从 eval 内部返回? - 2

    我有一个代码需要在eval中使用。有时我需要退出评估代码,但我的尝试会导致错误。例如:#expectedtosee1,2and5;not3nor4;andnoerrorseval"puts1;puts2;return;puts3;puts4"#=>Error:unexpectedreturnputs5我尝试了return、end、exit、break,但都没有成功。exit不会引发错误,但我没有得到5。(注意:我知道eval是邪恶的,但在这种情况下我需要使用它。) 最佳答案 谢谢大家,但我找到了最适合我的问题的解决方案:lambda

随机推荐