...

Source file src/github.com/redis/go-redis/v9/scripting_commands.go

Documentation: github.com/redis/go-redis/v9

     1  package redis
     2  
     3  import "context"
     4  
     5  type ScriptingFunctionsCmdable interface {
     6  	Eval(ctx context.Context, script string, keys []string, args ...interface{}) *Cmd
     7  	EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *Cmd
     8  	EvalRO(ctx context.Context, script string, keys []string, args ...interface{}) *Cmd
     9  	EvalShaRO(ctx context.Context, sha1 string, keys []string, args ...interface{}) *Cmd
    10  	ScriptExists(ctx context.Context, hashes ...string) *BoolSliceCmd
    11  	ScriptFlush(ctx context.Context) *StatusCmd
    12  	ScriptKill(ctx context.Context) *StatusCmd
    13  	ScriptLoad(ctx context.Context, script string) *StringCmd
    14  
    15  	FunctionLoad(ctx context.Context, code string) *StringCmd
    16  	FunctionLoadReplace(ctx context.Context, code string) *StringCmd
    17  	FunctionDelete(ctx context.Context, libName string) *StringCmd
    18  	FunctionFlush(ctx context.Context) *StringCmd
    19  	FunctionKill(ctx context.Context) *StringCmd
    20  	FunctionFlushAsync(ctx context.Context) *StringCmd
    21  	FunctionList(ctx context.Context, q FunctionListQuery) *FunctionListCmd
    22  	FunctionDump(ctx context.Context) *StringCmd
    23  	FunctionRestore(ctx context.Context, libDump string) *StringCmd
    24  	FunctionStats(ctx context.Context) *FunctionStatsCmd
    25  	FCall(ctx context.Context, function string, keys []string, args ...interface{}) *Cmd
    26  	FCallRo(ctx context.Context, function string, keys []string, args ...interface{}) *Cmd
    27  	FCallRO(ctx context.Context, function string, keys []string, args ...interface{}) *Cmd
    28  }
    29  
    30  func (c cmdable) Eval(ctx context.Context, script string, keys []string, args ...interface{}) *Cmd {
    31  	return c.eval(ctx, "eval", script, keys, args...)
    32  }
    33  
    34  func (c cmdable) EvalRO(ctx context.Context, script string, keys []string, args ...interface{}) *Cmd {
    35  	return c.eval(ctx, "eval_ro", script, keys, args...)
    36  }
    37  
    38  func (c cmdable) EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *Cmd {
    39  	return c.eval(ctx, "evalsha", sha1, keys, args...)
    40  }
    41  
    42  func (c cmdable) EvalShaRO(ctx context.Context, sha1 string, keys []string, args ...interface{}) *Cmd {
    43  	return c.eval(ctx, "evalsha_ro", sha1, keys, args...)
    44  }
    45  
    46  func (c cmdable) eval(ctx context.Context, name, payload string, keys []string, args ...interface{}) *Cmd {
    47  	cmdArgs := make([]interface{}, 3+len(keys), 3+len(keys)+len(args))
    48  	cmdArgs[0] = name
    49  	cmdArgs[1] = payload
    50  	cmdArgs[2] = len(keys)
    51  	for i, key := range keys {
    52  		cmdArgs[3+i] = key
    53  	}
    54  	cmdArgs = appendArgs(cmdArgs, args)
    55  	cmd := NewCmd(ctx, cmdArgs...)
    56  
    57  	// it is possible that only args exist without a key.
    58  	// rdb.eval(ctx, eval, script, nil, arg1, arg2)
    59  	if len(keys) > 0 {
    60  		cmd.SetFirstKeyPos(3)
    61  	}
    62  	_ = c(ctx, cmd)
    63  	return cmd
    64  }
    65  
    66  func (c cmdable) ScriptExists(ctx context.Context, hashes ...string) *BoolSliceCmd {
    67  	args := make([]interface{}, 2+len(hashes))
    68  	args[0] = "script"
    69  	args[1] = "exists"
    70  	for i, hash := range hashes {
    71  		args[2+i] = hash
    72  	}
    73  	cmd := NewBoolSliceCmd(ctx, args...)
    74  	_ = c(ctx, cmd)
    75  	return cmd
    76  }
    77  
    78  func (c cmdable) ScriptFlush(ctx context.Context) *StatusCmd {
    79  	cmd := NewStatusCmd(ctx, "script", "flush")
    80  	_ = c(ctx, cmd)
    81  	return cmd
    82  }
    83  
    84  func (c cmdable) ScriptKill(ctx context.Context) *StatusCmd {
    85  	cmd := NewStatusCmd(ctx, "script", "kill")
    86  	_ = c(ctx, cmd)
    87  	return cmd
    88  }
    89  
    90  func (c cmdable) ScriptLoad(ctx context.Context, script string) *StringCmd {
    91  	cmd := NewStringCmd(ctx, "script", "load", script)
    92  	_ = c(ctx, cmd)
    93  	return cmd
    94  }
    95  
    96  // ------------------------------------------------------------------------------
    97  
    98  // FunctionListQuery is used with FunctionList to query for Redis libraries
    99  //
   100  //	  	LibraryNamePattern 	- Use an empty string to get all libraries.
   101  //	  						- Use a glob-style pattern to match multiple libraries with a matching name
   102  //	  						- Use a library's full name to match a single library
   103  //		WithCode			- If true, it will return the code of the library
   104  type FunctionListQuery struct {
   105  	LibraryNamePattern string
   106  	WithCode           bool
   107  }
   108  
   109  func (c cmdable) FunctionLoad(ctx context.Context, code string) *StringCmd {
   110  	cmd := NewStringCmd(ctx, "function", "load", code)
   111  	_ = c(ctx, cmd)
   112  	return cmd
   113  }
   114  
   115  func (c cmdable) FunctionLoadReplace(ctx context.Context, code string) *StringCmd {
   116  	cmd := NewStringCmd(ctx, "function", "load", "replace", code)
   117  	_ = c(ctx, cmd)
   118  	return cmd
   119  }
   120  
   121  func (c cmdable) FunctionDelete(ctx context.Context, libName string) *StringCmd {
   122  	cmd := NewStringCmd(ctx, "function", "delete", libName)
   123  	_ = c(ctx, cmd)
   124  	return cmd
   125  }
   126  
   127  func (c cmdable) FunctionFlush(ctx context.Context) *StringCmd {
   128  	cmd := NewStringCmd(ctx, "function", "flush")
   129  	_ = c(ctx, cmd)
   130  	return cmd
   131  }
   132  
   133  func (c cmdable) FunctionKill(ctx context.Context) *StringCmd {
   134  	cmd := NewStringCmd(ctx, "function", "kill")
   135  	_ = c(ctx, cmd)
   136  	return cmd
   137  }
   138  
   139  func (c cmdable) FunctionFlushAsync(ctx context.Context) *StringCmd {
   140  	cmd := NewStringCmd(ctx, "function", "flush", "async")
   141  	_ = c(ctx, cmd)
   142  	return cmd
   143  }
   144  
   145  func (c cmdable) FunctionList(ctx context.Context, q FunctionListQuery) *FunctionListCmd {
   146  	args := make([]interface{}, 2, 5)
   147  	args[0] = "function"
   148  	args[1] = "list"
   149  	if q.LibraryNamePattern != "" {
   150  		args = append(args, "libraryname", q.LibraryNamePattern)
   151  	}
   152  	if q.WithCode {
   153  		args = append(args, "withcode")
   154  	}
   155  	cmd := NewFunctionListCmd(ctx, args...)
   156  	_ = c(ctx, cmd)
   157  	return cmd
   158  }
   159  
   160  func (c cmdable) FunctionDump(ctx context.Context) *StringCmd {
   161  	cmd := NewStringCmd(ctx, "function", "dump")
   162  	_ = c(ctx, cmd)
   163  	return cmd
   164  }
   165  
   166  func (c cmdable) FunctionRestore(ctx context.Context, libDump string) *StringCmd {
   167  	cmd := NewStringCmd(ctx, "function", "restore", libDump)
   168  	_ = c(ctx, cmd)
   169  	return cmd
   170  }
   171  
   172  func (c cmdable) FunctionStats(ctx context.Context) *FunctionStatsCmd {
   173  	cmd := NewFunctionStatsCmd(ctx, "function", "stats")
   174  	_ = c(ctx, cmd)
   175  	return cmd
   176  }
   177  
   178  func (c cmdable) FCall(ctx context.Context, function string, keys []string, args ...interface{}) *Cmd {
   179  	cmdArgs := fcallArgs("fcall", function, keys, args...)
   180  	cmd := NewCmd(ctx, cmdArgs...)
   181  	if len(keys) > 0 {
   182  		cmd.SetFirstKeyPos(3)
   183  	}
   184  	_ = c(ctx, cmd)
   185  	return cmd
   186  }
   187  
   188  // FCallRo this function simply calls FCallRO,
   189  // Deprecated: to maintain convention FCallRO.
   190  func (c cmdable) FCallRo(ctx context.Context, function string, keys []string, args ...interface{}) *Cmd {
   191  	return c.FCallRO(ctx, function, keys, args...)
   192  }
   193  
   194  func (c cmdable) FCallRO(ctx context.Context, function string, keys []string, args ...interface{}) *Cmd {
   195  	cmdArgs := fcallArgs("fcall_ro", function, keys, args...)
   196  	cmd := NewCmd(ctx, cmdArgs...)
   197  	if len(keys) > 0 {
   198  		cmd.SetFirstKeyPos(3)
   199  	}
   200  	_ = c(ctx, cmd)
   201  	return cmd
   202  }
   203  
   204  func fcallArgs(command string, function string, keys []string, args ...interface{}) []interface{} {
   205  	cmdArgs := make([]interface{}, 3+len(keys), 3+len(keys)+len(args))
   206  	cmdArgs[0] = command
   207  	cmdArgs[1] = function
   208  	cmdArgs[2] = len(keys)
   209  	for i, key := range keys {
   210  		cmdArgs[3+i] = key
   211  	}
   212  
   213  	cmdArgs = append(cmdArgs, args...)
   214  	return cmdArgs
   215  }
   216  

View as plain text