command.go 871 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Author: simon (ynwdlxm@163.com)
  2. // Date: 2025/9/15 18:08
  3. // Desc: 命令列表
  4. package database
  5. import (
  6. "strings"
  7. "github.com/runningwater/go-redis/lib/logger"
  8. )
  9. // 命令表
  10. var cmdTable = make(map[string]*command)
  11. type command struct {
  12. executor ExecFunc // 执行函数
  13. arity int // 参数个数, >0表示确切数量, <0表示最少数量, =0表示无参数也不接受参数
  14. }
  15. // RegisterCommand 将命令注册到命令表中
  16. // 参数:
  17. //
  18. // name: 命令名称
  19. // executor: 命令执行函数
  20. // arity: 命令参数个数,=0表示无参数,>0表示确切的参数个数,<0表示至少有N个参数(N=-arity
  21. func RegisterCommand(name string, executor ExecFunc, arity int) {
  22. name = strings.ToLower(name)
  23. logger.Debug("Register command:", strings.ToUpper(name))
  24. cmdTable[name] = &command{
  25. executor: executor,
  26. arity: arity,
  27. }
  28. }