time - redis:EVAL 和 TIME

标签 time lua redis eval

我喜欢 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/

相关文章:

ruby-on-rails - Redis模型缓存,什么时候调用items_helper中的fetch_items?

heroku - 如何从 DelayedJob 迁移到 Sidekiq

ios - Twitter Api 返回不准确的 24 小时时间?在 swift 4 中解析

lua - 站点Lua : string comparison raises "attempt to call a string value"?

python - python无限循环和time.sleep()有什么问题吗?

function - 什么可以作为参数传递给 Lua 函数?

C - 相当于 Lua 的 math.rad

caching - 类似于标签缓存并查询它以建议他们使用 Redis

ruby-on-rails - 日期时间到时间转换中奇怪的 Ruby 行为

java - 在 Java 中向 Time 对象添加分钟