Redis lua什么时候真正用上? [英] Redis lua when to really use it?

查看:48
本文介绍了Redis lua什么时候真正用上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始研究和使用 lua 并发现它在想要获取一系列键时非常棒.例如:

I've started to research and play a bit with lua and have found it to be great when wanting to grab ranges of keys. Ex:

business:5:visits:2013-11-12
business:5:visits:2013-11-13
etc

使用 lua 我只需要向 redis 发送一个命令,而不是完整的日期范围.

With lua I only have to send one command to redis instead of the complete range of dates.

现在我正在考虑转换更多的逻辑并将其移至 Redis.

Now I'm thinking about converting more of our logic and move it onto Redis.

以我们目前看起来像这样的消息存储过程为例:

Take our message storing process which currently looks like this:

// create a new unique id
redisClient.incr(Config.messageId, function(err, reply) {
    var messageId = reply.toString();
    var timestmp = Date.now();

    // push message
    redisClient.zadd(Config.history + ':' + obj.uid + ':' + obj.channel.replace(/s+/g, ''), timestmp, messageId);

    // store the message data by messageId
    redisClient.hmset(Config.messageHash + ':' + messageId, {
        'user_id': obj.uid,
        'text_body': "some text",
        'text_date': timestmp,
    });


    // set expires
    redisClient.expire(Config.history + ':' + obj.uid + ':' + obj.channel.replace(/s+/g, ''), Config.messageExpire);
    redisClient.expire(Config.messageHash + ':' + messageId, Config.messageExpire);


    // add to mysql-sync queue
    redisClient.RPUSH(Config.messageMySQLSyncQueue, Config.messageHash + ':' + messageId);

});

上面可以很容易地转换成lua,但是为了性能值得吗?

The above could easily be converted into lua, but is it worth it for performance?

在 Lua 中编写它是否会更快并且只需要向 Redis 发出 1 个命令?会不会导致阻塞其他命令的问题?

Would it be faster to write this in Lua instead and only have to issue 1 command to Redis? Would it cause problems with blocking other commands?

推荐答案

Lua 脚本 旨在像 MULTI 命令一样工作.实际上,您使用 Redis 客户端的 MULTI 命令开发的大多数命令都可以在 Lua 中实现.也就是说,您可以将一些复杂的操作封装在一个脚本中,您的数据层将执行原子写操作,而无需担心您在 Redis 上的数据建模策略.

Lua scripts are meant to work like MULTI commands. Actually most commands that you would develop using MULTI commands from a Redis client can be implemented in Lua. That is, you can encapsulate some complex operations in a script and your data layer will perform the atomic write operation without worrying about your data modeling strategy on Redis.

此外,当您想要执行快速但复杂的读取操作时,我发现它们很有用.例如,您可能希望按顺序获取对象.对象存储在散列键中,而顺序由排序集键定义.你得到一个所谓的有序集合的范围,你使用 hmget 得到散列中的对象.

Also, I find them useful when you want to perform quick but complex read operations. For example, you might want to get objects in order. Objects are stored in a hash key while the order is defined by a sorted set key. You get a range of the so-called sorted set and you get objects in hash using hmget.

最重要的一点是 Lua 脚本应该实现可以尽可能快地执行的东西,因为 Redis 会在 Lua 脚本运行时阻止其他操作.也就是说,您需要执行快速中断,否则您的整体 Redis 性能会下降很多.

The most important point is Lua scripts should implement things that can execute as fast as possible, since Redis will block other operations while a Lua script is running. That is, you need to perform quick interruptions or your overall Redis performance will decrease a lot.

我认为您应该在真正需要它们时使用它们.通常,客户端是使用 C#、Java、JavaScript、Ruby...等高级编程语言开发的,它们提供更好的开发体验:良好的调试器、IDE、代码完成...

I would argue that you should use them when you really need them. Usually, clients are developed using high-level programming languages like C#, Java, JavaScript, Ruby... and they provide better development experience: good debuggers, IDE, code-completion...

总结:如果您能证明将域逻辑的某些部分转换为 Redis Lua 脚本会带来实际好处(在性能方面),那么您应该使用它们.

Summary: you should use them if you can prove that there's an actual benefit (in performance) if you turn some part of your domain logic into Redis Lua scripts.

这篇关于Redis lua什么时候真正用上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆