github.com/therealbill/libredis@v0.0.0-20161227004305-7d50abda5ccf/client/scripting.go (about)

     1  package client
     2  
     3  // ScriptExists returns information about the existence of the scripts in the script cache.
     4  // Multi-bulk reply The command returns an array of integers
     5  // that correspond to the specified SHA1 digest arguments.
     6  // For every corresponding SHA1 digest of a script that actually exists in the script cache.
     7  func (r *Redis) ScriptExists(scripts ...string) ([]bool, error) {
     8  	args := packArgs("SCRIPT", "EXISTS", scripts)
     9  	rp, err := r.ExecuteCommand(args...)
    10  	if err != nil {
    11  		return nil, err
    12  	}
    13  	return rp.BoolArrayValue()
    14  }
    15  
    16  // ScriptFlush flush the Lua scripts cache.
    17  // Please refer to the EVAL documentation for detailed information about Redis Lua scripting.
    18  func (r *Redis) ScriptFlush() error {
    19  	rp, err := r.ExecuteCommand("SCRIPT", "FLUSH")
    20  	if err != nil {
    21  		return err
    22  	}
    23  	return rp.OKValue()
    24  }
    25  
    26  // ScriptKill kills the currently executing Lua script,
    27  // assuming no write operation was yet performed by the script.
    28  func (r *Redis) ScriptKill() error {
    29  	rp, err := r.ExecuteCommand("SCRIPT", "KILL")
    30  	if err != nil {
    31  		return err
    32  	}
    33  	return rp.OKValue()
    34  }
    35  
    36  // ScriptLoad Load a script into the scripts cache, without executing it.
    37  // After the specified command is loaded into the script cache
    38  // it will be callable using EVALSHA with the correct SHA1 digest of the script,
    39  // exactly like after the first successful invocation of EVAL.
    40  // Bulk reply This command returns the SHA1 digest of the script added into the script cache.
    41  func (r *Redis) ScriptLoad(script string) (string, error) {
    42  	rp, err := r.ExecuteCommand("SCRIPT", "LOAD", script)
    43  	if err != nil {
    44  		return "", err
    45  	}
    46  	return rp.StringValue()
    47  }
    48  
    49  // Eval first argument of EVAL is a Lua 5.1 script.
    50  // The script does not need to define a Lua function (and should not).
    51  // It is just a Lua program that will run in the context of the Redis server.
    52  // The second argument of EVAL is the number of arguments that follows the script
    53  // (starting from the third argument) that represent Redis key names.
    54  // This arguments can be accessed by Lua using the KEYS global variable
    55  // in the form of a one-based array (so KEYS[1], KEYS[2], ...).
    56  func (r *Redis) Eval(script string, keys []string, args []string) (*Reply, error) {
    57  	cmds := packArgs("EVAL", script, len(keys), keys, args)
    58  	return r.ExecuteCommand(cmds...)
    59  }
    60  
    61  // EvalSha evaluates a script cached on the server side by its SHA1 digest.
    62  // Scripts are cached on the server side using the SCRIPT LOAD command.
    63  func (r *Redis) EvalSha(sha1 string, keys []string, args []string) (*Reply, error) {
    64  	cmds := packArgs("EVALSHA", sha1, len(keys), keys, args)
    65  	return r.ExecuteCommand(cmds...)
    66  }