redisLuaScriptUtils

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2022 License: MIT Imports: 5 Imported by: 2

README

redis-lua-script-utils-go

Utilities for composing and executing Redis LUA scripts

Motivation

Redis LUA scripts are great! They allow implementing complex functionality with logic as atomic operations. But what do you do when you want to execute several scripts as a single logical unit?

One way to do it is to compose longer LUA scripts manually, keeping close track of keys and arguments which should be passed to each resuling script variation.

Will this work? Yes. Is it maintainable? Likely not. So what do you do?

redis-lua-script-utils-go to the rescue!

This package allows maintaining (and testing) separate chunks of LUA scripts, merging them and their Keys and Arguments parameters into one single script unit in your program code.

Once merged, it reuses KEYS supplied during final script compilation stage and expects ARGV values to be supplied as a map, validating input and executing the complete, merged script against redis in an efficient manner.

Dynamic KEYS can be computed using ARGV values as input.

Example

package main

import (
  "context"
  "github.com/go-redis/redis/v8"
  redisLuaScriptUtils "github.com/zavitax/redis-lua-script-utils-go"
)

func main() {
	scriptText1 := `redis.call('SET', key1, arg1);`
	scriptText2 := `redis.call('SET', key2, arg2);`
	scriptText3 := `redis.call('SET', key2, arg2);`

	script1 := redisLuaScriptUtils.NewRedisScript([]string{"key1"}, []string{"arg1"}, scriptText1)
	script2 := redisLuaScriptUtils.NewRedisScript([]string{"key2"}, []string{"arg2"}, scriptText2)
	script3 := redisLuaScriptUtils.NewRedisScript([]string{"key2"}, []string{"arg2"}, scriptText3)

	compiled, err := redisLuaScriptUtils.CompileRedisScripts(
		[]*redisLuaScriptUtils.RedisScript{script1, script2, script3},
		[]*redisLuaScriptUtils.RedisKey{
			redisLuaScriptUtils.NewStaticKey("key1", "keyName1"),
			redisLuaScriptUtils.NewDynamicKey("key2", func(args *redisLuaScriptUtils.RedisScriptArguments) string {
				return (*args)["key2_value"].(string)
			},
		},
	)

	if err != nil {
		panic(err)
	}

	redisClient := redis.NewClient(&redis.Options{
		Addr: "127.0.0.1:6379",
		Password: "",
		DB: 0,
	})

	scriptArgs := make(redisLuaScriptUtils.RedisScriptArguments, 0)
	scriptArgs["arg1"] = "arg1_expected_value"
	scriptArgs["arg2"] = "arg2_expected_value"
	scriptArgs["key2_value"] = "keyName2"

	compiled.Run(context.TODO(), redisClient, &scriptArgs).Result()

	redisClient.Close()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompiledRedisScript

type CompiledRedisScript struct {
	// contains filtered or unexported fields
}

func CompileRedisScripts

func CompileRedisScripts(scripts []*RedisScript, keys []*RedisKey) (*CompiledRedisScript, error)

func (*CompiledRedisScript) Args

func (this *CompiledRedisScript) Args(args *RedisScriptArguments) ([]interface{}, error)

func (*CompiledRedisScript) Keys

func (this *CompiledRedisScript) Keys(args *RedisScriptArguments) []string

func (*CompiledRedisScript) Run

func (this *CompiledRedisScript) Run(ctx context.Context, client *redis.Client, args *RedisScriptArguments) *redis.Cmd

func (*CompiledRedisScript) RunDebug

func (this *CompiledRedisScript) RunDebug(ctx context.Context, client *redis.Client, args *RedisScriptArguments) *redis.Cmd

func (*CompiledRedisScript) String

func (this *CompiledRedisScript) String() string

type RedisKey

type RedisKey struct {
	// contains filtered or unexported fields
}

func NewDynamicKey

func NewDynamicKey(id string, generator RedisKeyValueGeneratorFunc) *RedisKey

func NewStaticKey

func NewStaticKey(id string, value string) *RedisKey

func (*RedisKey) Key

func (this *RedisKey) Key() string

func (*RedisKey) Value

func (this *RedisKey) Value(args *RedisScriptArguments) string

type RedisKeyValueGeneratorFunc

type RedisKeyValueGeneratorFunc func(args *RedisScriptArguments) string

type RedisScript

type RedisScript struct {
	// contains filtered or unexported fields
}

func NewRedisScript

func NewRedisScript(keys []string, args []string, scriptText string) *RedisScript

func (*RedisScript) Args

func (this *RedisScript) Args() []string

func (*RedisScript) Keys

func (this *RedisScript) Keys() []string

func (*RedisScript) String

func (this *RedisScript) String() string

type RedisScriptArguments

type RedisScriptArguments map[string]interface{}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL