Redis lua何时真正使用它? [英] Redis lua when to really use it?

查看:191
本文介绍了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中实现.也就是说,您可以将一些复杂的操作封装在脚本中,并且您的数据层将执行原子性的 write操作,而无需担心您在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天全站免登陆