模块
模块有助于将相关的自定义命令、过程和事务捆绑到一个二进制文件中。加载后,模块会将其包含的所有自定义操作提供给用户。所有模块都必须派生自 ModuleBase
类并实现 OnLoad
方法以执行模块初始化。模块中实现的所有自定义命令、过程和事务都会在使用此方法初始化期间注册到 Garnet。
OnLoad(ModuleLoadContext context, string[] args)
通过 args
参数提供传递给模块的可选参数。ModuleLoadContext
公开以下 API 以注册模块及其组件:
-
ModuleActionStatus Initialize(string name, uint version)
这必须是使用模块名称和版本信息注册模块的第一步。所有其他注册都必须在此之后执行。返回的ModuleActionStatus
枚举指示模块初始化的状态,其中Success
状态确认模块已成功注册。 -
ModuleActionStatus RegisterCommand(string name, CustomRawStringFunctions customFunctions, CommandType type = CommandType.ReadModifyWrite, RespCommandsInfo commandInfo = null, long expirationTicks = 0)
任何自定义原始字符串命令都应使用此方法注册,其中包含其name
、customFunctions
中原始字符串命令的实现、指示它是 ReadModifyWrite 还是 Read 命令的type
(RMW 是默认类型)、用于提供 arity、键规范、acl 类别等详细信息的可选commandInfo
以及用于控制键何时过期的可选expirationTicks
。 -
ModuleActionStatus RegisterTransaction(string name, Func<CustomTransactionProcedure> proc, RespCommandsInfo commandInfo = null)
事务应使用此方法注册,其中包含其name
、返回事务实现proc
的方法以及可选的commandInfo
。 -
ModuleActionStatus RegisterType(CustomObjectFactory factory)
自定义数据类型使用此方法注册,其中factory
是可以创建自定义对象实例的自定义对象工厂的实现。 -
ModuleActionStatus RegisterCommand(string name, CustomObjectFactory factory, CustomObjectFunctions command, CommandType type = CommandType.ReadModifyWrite, RespCommandsInfo commandInfo = null)
自定义对象命令使用此方法注册,其中包含其name
、已使用RegisterType
注册的factory
实例、command
中自定义对象命令的实现、指示它是 ReadModifyWrite 还是 Read 命令的type
(RMW 是默认类型)以及可选的commandInfo
。 -
ModuleActionStatus RegisterProcedure(string name, CustomProcedure customScriptProc, RespCommandsInfo commandInfo = null)
自定义非事务性过程使用此方法注册,其中包含其name
、customScriptProc
中的实现以及可选的commandInfo
。
有关模块实现的参考,请参阅 playground\SampleModule 中的示例。