Skip to content

实践阅读

Redis 函数

https://redis.io/docs/latest/develop/interact/programmability/functions-intro/

Scripting with Redis 7 and beyond

以下是对“Scripting with Redis 7 and beyond”这个句子的成分分析:

主语(隐含)

在句子结构上,这是一个省略了明确主语的短语(通常在标题、说明性语句等场景中较为常见 )。如果补充完整,可以理解为类似 “(使用 Redis 7 及以上版本进行)脚本编写(这件事)” ,“脚本编写” 所涉及的行为主体(如开发者、用户等)被省略了。

谓语(隐含在核心动作概念中)

同样,由于是短语形式,核心动作 “编写脚本(scripting)” 承担了类似谓语的功能,它描述了主语(隐含)所进行的主要动作。

伴随状语

“with Redis 7 and beyond” 是伴随状语,用来补充说明 “scripting(脚本编写)” 这个动作所使用的工具或环境范围。其中:

  • “with” 是介词,引导伴随状语,表示 “使用;借助” 的意思。
  • “Redis 7 and beyond” 是介词 “with” 的宾语,表示 “Redis 7 版本及其之后(的版本)” 。“beyond” 是介词,意为 “在……之外;超出” ,在这里与 “Redis 7” 结合,强调版本范围不仅包括 Redis 7,还包括比它更新的版本。

Redis Functions is an API for managing code to be executed on the server. This feature, which became available in Redis 7, supersedes the use of EVAL in prior versions of Redis.

以下是对“Redis Functions is an API for managing code to be executed on the server.”这个句子的成分分析:

  1. 主语(Subject)
  • “Redis Functions”:整个句子的主语,表示所谈论的对象或主题。
  1. 谓语(Predicate)
  • “is”:系动词,连接主语和表语,表示主语的状态或性质。
  1. 表语(Predicative)
  • “an API for managing code to be executed on the server”:整个表语部分,进一步说明主语“Redis Functions”是什么。
  • “an API”:表语的核心部分,表示“Redis Functions”是一个API。
  • “for managing code”:介词短语作定语,修饰“API”,表示这个API的用途是“管理代码”。
  • “to be executed on the server”:不定式短语作后置定语,修饰“code”,表示这些代码是“将在服务器上执行的”。

句子成分总结

  • 主语:Redis Functions
  • 谓语:is
  • 表语:an API for managing code to be executed on the server
  • 定语1:for managing code(修饰API)
  • 定语2:to be executed on the server(修饰code)

以下是对“This feature, which became available in Redis 7, supersedes the use of EVAL in prior versions of Redis.”这个句子的成分分析:

  1. 主语(Subject)
  • “This feature”:整个句子的主语,表示所谈论的对象或主题。
  1. 谓语(Predicate)
  • “supersedes”:谓语动词,表示主语的动作或状态,即“取代”或“超越”。
  1. 宾语(Object)
  • “the use of EVAL”:宾语,表示主语“This feature”所取代的对象,即“EVAL的使用”。
  1. 定语从句(Relative Clause)
  • “which became available in Redis 7”:定语从句,修饰主语“This feature”,提供关于这个特性的额外信息,即“这个特性在Redis 7中变得可用”。
  1. 状语(Adverbial)
  • “in prior versions of Redis”:介词短语作状语,修饰动词“supersedes”,表示这个动作发生的背景或范围,即“在Redis的早期版本中”。

句子成分总结

  • 主语:This feature
  • 谓语:supersedes
  • 宾语:the use of EVAL
  • 定语从句:which became available in Redis 7(修饰This feature)
  • 状语:in prior versions of Redis(修饰supersedes)

句子结构重述

  • 主语“This feature”通过定语从句“which became available in Redis 7”得到进一步描述。
  • 谓语“supersedes”表示主语的动作,即“取代”。
  • 宾语“the use of EVAL”是主语所取代的对象。
  • 状语“in prior versions of Redis”提供了动作发生的背景信息。

Prologue (or, what's wrong with Eval Scripts?)

“What's wrong with Eval Scripts?” 的句子成分分析如下

  1. 主语
  • What:疑问代词,作为句子的主语,表示“什么”。
  1. 谓语
  • is:系动词,连接主语和表语,表示“是”。
  1. 表语
  • wrong:形容词,作为表语,描述主语的状态,表示“有问题的”或“出错的”。
  1. 状语
  • with Eval Scripts:介词短语,修饰表语“wrong”,说明问题的范围或对象,表示“在Eval脚本中”或“关于Eval脚本”。

句子结构总结

  • 主系表结构:句子由主语“What”、系动词“is”、表语“wrong”构成核心结构,表达“什么有问题”。
  • 状语修饰:介词短语“with Eval Scripts”进一步说明问题的具体范围,完整表达“Eval脚本中出了什么问题”。

语义分析

  • 句子用于询问关于“Eval Scripts”(通常指使用eval()函数的脚本)的具体问题或错误。
  • eval()函数在JavaScript中用于动态执行字符串代码,但因其安全性风险(如代码注入)和性能问题,常被视为不推荐使用的功能。

Prior versions of Redis made scripting available only via the EVAL command, which allows a Lua script to be sent for execution by the server. The core use cases for Eval Scripts is executing part of your application logic inside Redis, efficiently and atomically. Such script can perform conditional updates across multiple keys, possibly combining several different data types.

首先,分析句子的结构: 这是一个复合句,主句是:Prior versions of Redis made scripting available only via the EVAL command。

然后,重点要做的是提炼出主句的主干: Prior versions of Redis(主语)+ made(谓语)+ scripting(宾语)+ available(宾语补足语)+ only via the EVAL command(方式状语),使用了“make sth + adj”的结构,表示“使某物……”。

再来对其他成分进行分析

  • which allows a Lua script to be sent for execution by the server 是定语从句,修饰先行词the EVAL command,表示“该命令允许发送Lua脚本供服务器执行” ;
  • 在这个定语从句中,which是关系代词,指代先行词the EVAL command,在从句中作主语;
  • allows是谓语动词,表示“允许”;
  • a Lua script是宾语,表示“一个Lua脚本”;
  • to be sent for execution by the server 是不定式短语作宾语补足语,表示“被服务器发送执行” ,其中by the server是方式状语,表示“由服务器”。

最后再总结下这句话的意思: 早期版本的Redis仅通过EVAL命令提供脚本功能,该命令允许发送Lua脚本供服务器执行。

首先,分析句子的结构: 这是一个复合句,主句是:The core use cases for Eval Scripts is executing part of your application logic inside Redis。

然后,重点要做的是提炼出主句的主干: The core use cases(主语)+ for Eval Scripts(后置定语,修饰主语)+ is(系动词)+ executing(表语),使用了“主系表”的结构,表示“Eval脚本的核心用例是执行……”。

再来对其他成分进行分析

  • part of your application logic 是executing的宾语,表示“应用程序逻辑的一部分”;
  • inside Redis 是地点状语,修饰executing,表示“在Redis内部”;
  • efficiently and atomically 是方式状语,修饰executing,表示“高效且原子性地”。

最后再总结下这句话的意思: Eval脚本的核心用例是在Redis内部高效且原子性地执行应用程序逻辑的一部分。

首先,分析句子的结构: 这是一个简单句,主语是:Such script,谓语是:can perform,宾语是:conditional updates。

然后,重点要做的是提炼出主句的主干: Such script(主语)+ can perform(谓语)+ conditional updates(宾语),使用了“主谓宾”的结构,表示“这样的脚本可以执行……”。

再来对其他成分进行分析

  • across multiple keys 是介词短语作状语,修饰perform,表示“跨多个键”;
  • possibly combining several different data types 是现在分词短语作状语,修饰整个句子,表示“可能结合几种不同的数据类型” ,其中combining是现在分词,表示“结合”,several different data types是其宾语。

最后再总结下这句话的意思: 这样的脚本可以跨多个键执行条件更新,并可能结合几种不同的数据类型。

Using EVAL requires that the application sends the entire script for execution every time. Because this results in network and script compilation overheads, Redis provides an optimization in the form of the EVALSHA command. By first calling SCRIPT LOAD to obtain the script's SHA1, the application can invoke it repeatedly afterward with its digest alone.

首先,分析句子的结构: 这是一个复合句,主句是:Using EVAL requires something。

然后,重点要做的是提炼出主句的主干: Using EVAL(动名词短语作主语)+ requires(谓语)+ that the application sends the entire script for execution every time(宾语从句),使用了“主谓宾(宾语从句)”的结构,表示“使用EVAL要求……”。

再来对其他成分进行分析

  • that the application sends the entire script for execution every time 是宾语从句,作requires的宾语;
  • 在这个宾语从句中,the application是主语,表示“应用程序”;
  • sends是谓语动词,表示“发送”;
  • the entire script是宾语,表示“整个脚本”;
  • for execution是介词短语作目的状语,修饰sends,表示“为了执行”;
  • every time是时间状语,修饰整个从句,表示“每次”。

最后再总结下这句话的意思: 使用EVAL要求应用程序每次发送整个脚本以供执行。

首先,分析句子的结构: 这是一个复合句,主句是:Redis provides an optimization。

然后,重点要做的是提炼出主句的主干: Redis(主语)+ provides(谓语)+ an optimization(宾语),使用了“主谓宾”的结构,表示“Redis提供了一个优化”。

再来对其他成分进行分析

  • Because this results in network and script compilation overheads 是原因状语从句,修饰整个主句,说明提供优化的原因;
  • 在这个原因状语从句中,this是主语,指代前面提到的某种情况或行为;
  • results in是谓语动词短语,表示“导致”;
  • network and script compilation overheads是宾语,表示“网络和脚本编译开销”。
  • in the form of the EVALSHA command 是介词短语作后置定语,修饰optimization,表示“以EVALSHA命令的形式”。

最后再总结下这句话的意思: 由于这会导致网络和脚本编译开销,Redis以EVALSHA命令的形式提供了一种优化。

首先,分析句子的结构: 这是一个复合句,主句是:the application can invoke it repeatedly afterward。

然后,重点要做的是提炼出主句的主干: the application(主语)+ can invoke(谓语)+ it(宾语)+ repeatedly afterward(方式状语),使用了“主谓宾(方式状语)”的结构,表示“应用程序之后可以重复调用它”。

再来对其他成分进行分析

  • By first calling SCRIPT LOAD to obtain the script's SHA1 是方式状语从句(或更准确地说是介词短语作方式状语,其中包含了一个非谓语动词短语作目的状语),修饰整个主句,说明调用脚本的方式;
  • 在这个方式状语中,By是介词,表示“通过”;
  • first calling SCRIPT LOAD是非谓语动词短语作介词By的宾语(实际上是动名词短语),表示“首先调用SCRIPT LOAD”;
  • to obtain the script's SHA1是不定式短语作目的状语,修饰calling SCRIPT LOAD,表示“以获取脚本的SHA1”。
  • with its digest alone 是介词短语作伴随状语,修饰invoke,表示“仅使用其摘要(SHA1)”。

最后再总结下这句话的意思: 通过首先调用SCRIPT LOAD来获取脚本的SHA1,应用程序之后可以仅使用其摘要(SHA1)来重复调用它。

By design, Redis only caches the loaded scripts. That means that the script cache can become lost at any time, such as after calling SCRIPT FLUSH, after restarting the server, or when failing over to a replica. The application is responsible for reloading scripts during runtime if any are missing. The underlying assumption is that scripts are a part of the application and not maintained by the Redis server.

By design:状语,表示方式或条件,意思是“按照设计” 。

Redis:主语,表示执行动作的主体,即“Redis(一个开源的、内存中的数据结构存储系统)” 。

only caches:谓语部分,其中“only”是副词,修饰动词“caches”(缓存),表示“仅仅缓存” 。

the loaded scripts:宾语,其中“the loaded”是过去分词短语作定语,修饰“scripts”(脚本),表示“已加载的脚本” 。

整个句子意思是“按照设计,Redis 仅缓存已加载的脚本”。

That means that the script cache can become lost at any time, such as after calling SCRIPT FLUSH, after restarting the server, or when failing over to a replica.

句子成分分析

  1. That:主语从句的引导词,本身不充当具体句子成分,但引导了整个主语从句。
  2. means:谓语动词,表示“意味着”。
  3. that the script cache can become lost at any time:宾语从句,作为“means”的宾语,表示“脚本缓存可能在任何时候丢失”。
  • the script cache:宾语从句中的主语,表示“脚本缓存”。
  • can become lost:宾语从句中的谓语部分,表示“可能丢失”。
  • at any time:宾语从句中的时间状语,表示“在任何时候”。
  1. such as after calling SCRIPT FLUSH, after restarting the server, or when failing over to a replica:介词短语作状语,对前面的宾语从句进行举例说明。
  • such as:介词,表示“例如”。
  • after calling SCRIPT FLUSH:介词短语,表示“在调用SCRIPT FLUSH之后”。
  • after restarting the server:介词短语,表示“在重启服务器之后”。
  • when failing over to a replica:省略了主语和谓语的状语从句(完整形式可能是“when it fails over to a replica”),表示“在故障转移到副本时”。

总结

整个句子表达的意思是:“这意味着脚本缓存可能在任何时候丢失,例如在调用SCRIPT FLUSH之后、在重启服务器之后或在故障转移到副本时。”

The application is responsible for reloading scripts during runtime if any are missing.

句子成分分析

  1. The application:主语,表示执行动作的主体,即“应用程序”。
  2. is responsible for:谓语部分,其中“is”是系动词,“responsible for”是形容词短语作表语,表示“对……负责”。
  3. reloading scripts:动名词短语作宾语,表示“重新加载脚本”这一动作。
  4. during runtime:介词短语作时间状语,表示“在运行时”。
  5. if any are missing:条件状语从句(省略了主语“scripts”),表示“如果有任何脚本丢失”。
  • if:引导词,表示条件。
  • any:代词,指代前面的“scripts”,表示“任何”。
  • are missing:谓语部分,表示“丢失”。

总结

整个句子表达的意思是:“如果有任何脚本丢失,应用程序负责在运行时重新加载它们。”

The underlying assumption is that scripts are a part of the application and not maintained by the Redis server.

句子成分分析

  1. The underlying assumption:主语,表示“基本假设”或“潜在假设”。
  2. is:系动词,连接主语和表语。
  3. that scripts are a part of the application and not maintained by the Redis server:表语从句,作为“is”的表语,详细说明假设的内容。
  • that:引导词,引导表语从句。
  • scripts:从句中的主语,表示“脚本”。
  • are:从句中的系动词,连接主语和表语。
  • a part of the application:表语部分,表示“应用程序的一部分”。
  • and:并列连词,连接两个并列的表语部分。
  • not maintained by the Redis server:另一个表语部分,表示“不由Redis服务器维护”。其中,“maintained”是过去分词作表语补足语,与“are”一起构成被动语态;“by the Redis server”是介词短语作状语,表示动作的执行者。

总结

整个句子表达的意思是:“基本假设是脚本是应用程序的一部分,而不是由Redis服务器维护的。”

This approach suits many light-weight scripting use cases, but introduces several difficulties once an application becomes complex and relies more heavily on scripting, namely:

  1. All client application instances must maintain a copy of all scripts. That means having some mechanism that applies script updates to all of the application's instances.
  2. Calling cached scripts within the context of a transaction increases the probability of the transaction failing because of a missing script. Being more likely to fail makes using cached scripts as building blocks of workflows less attractive.
  3. SHA1 digests are meaningless, making debugging the system extremely hard (e.g., in a MONITOR session).
  4. When used naively, EVAL promotes an anti-pattern in which scripts the client application renders verbatim scripts instead of responsibly using the KEYS and ARGV Lua APIs.
  5. Because they are ephemeral, a script can't call another script. This makes sharing and reusing code between scripts nearly impossible, short of client-side preprocessing (see the first point).

原句: This approach suits many light-weight scripting use cases, but introduces several difficulties once an application becomes complex and relies more heavily on scripting, namely:

分析

  1. 主句:
  • This approach(主语):指代“这种方法”。
  • suits(谓语):动词,表示“适合”。
  • many light-weight scripting use cases(宾语):指“许多轻量级脚本用例”。
  1. 转折结构:
  • but(连词):表示转折,引出后续的负面结果。
  1. 结果从句:
  • introduces several difficulties(谓语 + 宾语):动词短语,表示“引入了若干困难”。

  • once an application becomes complex and relies more heavily on scripting

    (条件状语从句):

    • once(连词):引导条件,表示“一旦”。
    • an application(主语):指“应用程序”。
    • becomes complex(谓语):动词短语,表示“变得复杂”。
    • and relies more heavily on scripting(并列谓语):表示“并且更依赖脚本”。
  1. 补充说明:
  • namely(副词):引出具体困难的内容(未完整呈现,可能后续有列举)。

简化理解

“这种方法适合轻量级脚本场景,但当应用复杂且依赖脚本时,会引入问题,具体如下……”

关键结构

  • 主句:This approach suits [用例]。
  • 转折:but introduces [困难] once [条件]。
  • 条件:application becomes complex + relies on scripting。

总结: 句子通过“but”转折,强调方法在复杂应用中的局限性,并通过“once”引导条件状语说明困难出现的背景。

原句: All client application instances must maintain a copy of all scripts. That means having some mechanism that applies script updates to all of the application's instances.

分析

  1. 第一句:
  • All client application instances(主语):指“所有客户端应用程序实例”。
  • must maintain(谓语):动词短语,表示“必须维护”。
  • a copy of all scripts(宾语):名词短语,表示“所有脚本的副本”。
  1. 第二句:
  • That(主语):指代前一句内容(即“所有实例维护脚本副本”这一要求)。

  • means(谓语):动词,表示“意味着”。

  • having some mechanism(动名词短语作宾语):表示“需要某种机制”。

  • that applies script updates to all of the application's instances

    (定语从句修饰“mechanism”):

    • that(关系代词):引导定语从句。
    • applies(谓语):动词,表示“应用”。
    • script updates(宾语):表示“脚本更新”。
    • to all of the application's instances(介词短语作状语):表示“对所有应用实例”。

简化理解

“所有客户端实例必须维护脚本副本,这意味着需要一种机制将脚本更新应用到所有实例。”

关键结构

  • 第一句:All [实例] must maintain [脚本副本]。
  • 第二句:That means having [机制] that applies [更新] to [所有实例]。

总结: 句子通过主从句结构,先提出要求(维护副本),再解释实现方式(通过某种机制分发更新)。定语从句进一步说明机制的功能。

To address these needs while avoiding breaking changes to already-established and well-liked ephemeral scripts, Redis v7.0 introduces Redis Functions.