xredis

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2023 License: MIT Imports: 8 Imported by: 0

README

xredis

GoDoc

Handy wrapper around github.com/redis/go-redis/v9 package. All the Redis client methods are usable on the xredis client.

Usage

  • Script

    const lua = `local age=redis.call("get", KEYS[1])
    local result="Hello "..ARGV[1].." "..KEYS[1]..", you are "..age.." years old!"
    return result`
    
    func main() {
        ctx := context.Background()
        xclient := xredis.New(redisClient)
    
        script := xredis.NewScript(lua)
    
        err := xclient.Set(ctx, "Abitbol", 42, time.Hour).Err()
        if err != nil {
            panic(err)
        }
    
        value, err := xclient.Run(ctx, script, []string{"Abitbol"}, "George")
        if err != nil {
            panic(err)
        }
    
        fmt.Println(value.String())
        // => Hello George Abitbol, you are 42 years old!
    }
    
  • Value object is a JSON value?

    json.Unmarshal(value.Bytes(), &object)
    

License

MIT

Contributing

All PRs are welcome.

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotASlice is used when a value is not castable into a slice.
	ErrNotASlice = errors.New("value is not a slice")
)

Functions

This section is empty.

Types

type Client

type Client = redis.Cmdable

A Client defines all client Redis actions (Client or ClusterClient).

type Script

type Script interface {
	// Source returns the script data.
	Source() string
	// Hash returns the SHA1 of the script.
	Hash() string
}

A Script is a set of custom atomic actions to perform in the scache.

func NewScript

func NewScript(src string) Script

NewScript returns a new Script.

type Value

type Value interface {
	// IsNil returns true if the value is nil.
	IsNil() bool
	// Float32 tries to convert into float32.
	Float32() (float32, error)
	// MustFloat32 converts into float32 or panic.
	MustFloat32() float32
	// Float64 tries to convert into float64.
	Float64() (float64, error)
	// MustFloat64 converts into float64 or panic.
	MustFloat64() float64
	// Int tries to convert into int.
	Int() (int, error)
	// MustInt converts into int or panic.
	MustInt() int
	// Int64 tries to convert into int64.
	Int64() (int64, error)
	// MustInt64 converts into int64 or panic.
	MustInt64() int64
	// Uint64 tries to convert into uint64.
	Uint64() (uint64, error)
	// MustUint64 converts into uint64 or panic.
	MustUint64() uint64
	// Bytes tries to convert into []bytes.
	Bytes() []byte
	// String tries to convert into string.
	String() string
	// Values tries to convert into []Value.
	Values() ([]Value, error)
	// MustValues converts into []Value or panic.
	MustValues() []Value
}

A Value is an agnostic data converter. The conversion is based on your software knowledge.

func NewValue added in v0.1.1

func NewValue(v interface{}) Value

NewValue returns a new Value based on the given Redis result value.

type XClient

type XClient interface {
	Client
	Run(ctx context.Context, script Script, keys []string, args ...interface{}) (Value, error)
	RunOnce(ctx context.Context, script Script, keys []string, args ...interface{}) (Value, error)
}

A XClient wraps default Redis client and adds more features.

func New

func New(r Client) XClient

New returns a new Client.

Jump to

Keyboard shortcuts

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