goscriptor

package module
Version: v0.0.0-...-44b42e6 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2021 License: MIT Imports: 6 Imported by: 0

README

goscriptor

Packet GoScriptor implements a way to use redis script more easily

Install

go get -u -v github.com/yshengliao/goscriptor

Usage

Let's start with a trivial example:

package main

import (
    "github.com/yshengliao/goscriptor"
)

var (
    scriptDefinition = "scriptKey|0.0.0"

    hello               = "hello"
    _HelloworldTemplate = `
    return 'Hello, World!'
    `
)

type MyScriptor struct {
    Scriptor *goscriptor.Scriptor
}

// hello function
func (s *MyScriptor) hello() (string, error) {
    res, err := s.Scriptor.ExecSha(hello, []string{})
    if err != nil {
        return "", err
    }

    return res.(string), nil
}

func main() {
    opt := &goscriptor.Option{
        Host:     "127.0.0.1",
        Port:     6379,
        Password: "",
        DB:       0,
        PoolSize: 10,
    }

    scripts := map[string]string{
        hello: _HelloworldTemplate,
    }

    scriptor, err := goscriptor.NewDB(opt, 1, scriptDefinition, &scripts)
    if err != nil {
        panic(err)
    }

    myscript := &MyScriptor{
        Scriptor: scriptor,
    }
    res, err := myscript.hello()
    if err != nil {
        panic(err)
    }
    println(res)
}

Dependency
  • testify

      go get -u -v github.com/stretchr/testify
    
  • go-redis

      go get -u -v github.com/go-redis/redis/v8
    
  • miniredis

      go get -u -v github.com/alicebob/miniredis/v2
    
  • null.v3

      go get -u -v gopkg.in/guregu/null.v3
    

TODO

  1. Add test cases using "testify".
  2. Add redis script test method.
  3. script_test unit test.
  4. Improve or remove useless code.
  5. Check code formatting.

Documentation

Overview

Package doc is a generated GoDoc package. Packet goscriptor implements a way to use redis script more easily

Index

Constants

This section is empty.

Variables

View Source
var EmptyRedisReplyValue = &RedisReplyValue{value: nil}

EmptyRedisReplyValue -

Functions

func GetNullableInt

func GetNullableInt(value *RedisReplyValue) (null.Int, error)

GetNullableInt -

func GetNullableString

func GetNullableString(value *RedisReplyValue) null.String

GetNullableString -

Types

type Option

type Option struct {
	Host     string
	Port     int
	Password string
	DB       int
	PoolSize int
}

Option - Redis Option

func (*Option) Create

func (opt *Option) Create() *redis.Client

Create - create a new redis descriptor

type RedisArrayReplyReader

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

RedisArrayReplyReader -

func NewRedisArrayReplyReader

func NewRedisArrayReplyReader(redisReply []interface{}) *RedisArrayReplyReader

NewRedisArrayReplyReader -

func (*RedisArrayReplyReader) ForEach

func (r *RedisArrayReplyReader) ForEach(action func(i int, v *RedisReplyValue) error) error

ForEach -

func (*RedisArrayReplyReader) GetLength

func (r *RedisArrayReplyReader) GetLength() int

GetLength -

func (*RedisArrayReplyReader) HasNext

func (r *RedisArrayReplyReader) HasNext() bool

HasNext -

func (*RedisArrayReplyReader) ReadArray

ReadArray -

func (*RedisArrayReplyReader) ReadFloat64

func (r *RedisArrayReplyReader) ReadFloat64(defaultValue float64) (float64, error)

ReadFloat64 -

func (*RedisArrayReplyReader) ReadInt32

func (r *RedisArrayReplyReader) ReadInt32(defaultValue int32) (int32, error)

ReadInt32 -

func (*RedisArrayReplyReader) ReadInt64

func (r *RedisArrayReplyReader) ReadInt64(defaultValue int64) (int64, error)

ReadInt64 -

func (*RedisArrayReplyReader) ReadString

func (r *RedisArrayReplyReader) ReadString() string

ReadString -

func (*RedisArrayReplyReader) ReadValue

func (r *RedisArrayReplyReader) ReadValue() *RedisReplyValue

ReadValue -

func (*RedisArrayReplyReader) SkipValue

func (r *RedisArrayReplyReader) SkipValue()

SkipValue -

type RedisReplyValue

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

RedisReplyValue -

func NewRedisReplyValue

func NewRedisReplyValue(value interface{}) *RedisReplyValue

NewRedisReplyValue -

func (*RedisReplyValue) AsFloat64

func (v *RedisReplyValue) AsFloat64(defaultValue float64) (float64, error)

AsFloat64 -

func (*RedisReplyValue) AsInt32

func (v *RedisReplyValue) AsInt32(defaultValue int32) (int32, error)

AsInt32 -

func (*RedisReplyValue) AsInt64

func (v *RedisReplyValue) AsInt64(defaultValue int64) (int64, error)

AsInt64 -

func (*RedisReplyValue) AsString

func (v *RedisReplyValue) AsString() string

AsString -

func (*RedisReplyValue) IsNil

func (v *RedisReplyValue) IsNil() bool

IsNil -

func (*RedisReplyValue) ToArrayReplyReader

func (v *RedisReplyValue) ToArrayReplyReader() *RedisArrayReplyReader

ToArrayReplyReader -

func (*RedisReplyValue) Value

func (v *RedisReplyValue) Value() interface{}

Value -

type ScriptDescriptor

type ScriptDescriptor struct {
	Scripts map[string]string
	// contains filtered or unexported fields
}

Script is a script descriptor

func NewScriptDescriptor

func NewScriptDescriptor(ctx context.Context, client *redis.Client, scripts *map[string]string, redisScriptDefinition string, db int) (*ScriptDescriptor, error)

NewScriptDescriptor creates a new script descriptor

func (*ScriptDescriptor) LoadScripts

func (scriptDescriptor *ScriptDescriptor) LoadScripts(ctx context.Context, client *redis.Client, redisScriptDefinition string, db int) error

LoadScripts loads the scripts

func (*ScriptDescriptor) Register

func (scriptDescriptor *ScriptDescriptor) Register(ctx context.Context, client *redis.Client, scripts *map[string]string, redisScriptDefinition string, db int) error

Registers a script

type Scriptor

type Scriptor struct {
	Client *redis.Client

	CTX context.Context
	// contains filtered or unexported fields
}

Scriptor - the script manager

func New

func New(client *redis.Client, scriptDB int, redisScriptDefinition string, scripts *map[string]string) (*Scriptor, error)

New - create a new scriptor with the redis client

func NewDB

func NewDB(opt *Option, scriptDB int, redisScriptDefinition string, scripts *map[string]string) (*Scriptor, error)

NewDB - create a new Scriptor with a new redis client

func (*Scriptor) Exec

func (s *Scriptor) Exec(script string, keys []string, args ...interface{}) (interface{}, error)

Exec - execute the script

func (*Scriptor) ExecSha

func (s *Scriptor) ExecSha(scriptname string, keys []string, args ...interface{}) (interface{}, error)

ExecSha - execute the script

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL