lua脚本 redis_Redis Lua脚本编制快速指南

lua脚本 redis

Redis is a popular in-memory grid used for interprocess communication and data storage. You might’ve heard that it lets you run Lua scripts, but you are still not sure why. If this sounds like you, read on.

Redis是一种流行的内存网格,用于进程间通信和数据存储。 您可能听说过它可以运行Lua脚本,但是您仍然不确定为什么。 如果听起来像您,请继续阅读。

先决条件 (Prerequisites)

You should have Redis installed on your system to follow this guide. It might be helpful to check Redis commands reference while reading.

您应该在系统安装Redis才能遵循本指南。 阅读时检查Redis命令参考可能会有所帮助。

为什么需要Lua脚本? (Why do I need Lua Scripts?)

In short: performance gain. Most tasks you do in Redis involve many steps. Instead of doing these steps in the language of your application, you can do it inside Redis with Lua.

简而言之:性能提升。 您在Redis中执行的大多数任务都涉及许多步骤。 您可以使用Lua在Redis内部进行操作,而不必使用应用程序语言来执行这些步骤。

  • This may result in better performance.

    这可能会导致更好的性能。
  • Also, all steps within a script are executed in an atomic way. No other Redis command can run while a script is executing.

    同样,脚本中的所有步骤都以原子方式执行。 执行脚本时,无法运行其他Redis命令。

For example, I use Lua scripts to change JSON strings stored in Redis. I describe this in detail closer to the end of this article.

例如,我使用Lua脚本来更改Redis中存储的JSON字符串。 我将在本文结尾处对此进行详细描述。

可是我什么都不知道 (But I don’t know any Lua)

Don’t worry, Lua is not very hard to understand. If you know any language of the C family, you should be okay with Lua. Also, I am providing working examples in this article.

别担心,Lua并不是很难理解。 如果您了解C族的任何语言 ,那么您应该对Lua满意。 另外,我在本文中提供了工作示例。

给我看个例子 (Show me an example)

Let’s start by running scripts via redis-cli. Start it with:

让我们开始通过redis-cli运行脚本。 从以下内容开始:

redis-cli

Now run the following command:

现在运行以下命令:

eval “redis.call(‘set’, KEYS[1], ARGV[1])” 1 key:name value

The EVAL command is what tells Redis to run the script which follows. The ”redis.call(‘set’, KEYS[1], ARGV[1])”string is our script which is functionally identical to the Redis’s set command. Three parameters follow the script text:

EVAL命令告诉Redis运行以下脚本。 ”redis.call('set', KEYS[1], ARGV[1])”字符串是我们的脚本,其功能与Redis的set命令相同。 脚本文本后面跟随三个参数:

  1. The number of provided keys

    提供的键数
  2. Key name

    键名
  3. First argument

    第一个论点

Script arguments fall

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值