rdctx

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2023 License: MIT Imports: 5 Imported by: 0

README

rdctx

rdctx is a go-redis client wrapper with more features

Examples

Simple usage
package main

import (
 "context"
 "log"

 "github.com/dnjooiopa/rdctx"
)

func main() {
  host := "localhost:6379"
  pwd := ""
  db := 3

  // init
  client, ctx := rdctx.NewWithContext(context.Background(), host, pwd, db)
  defer client.Close()

  // Check if connection is ok
  if err := client.ConnOK(); err != nil {
    log.Println("cannot connect to redis:", err)
  }
  log.Println("redis connected")

  performTask(ctx)
}

func performTask(ctx context.Context) {
  // Set key-value
  _, err := rdctx.Set(ctx, "foo", "bar")
  if err != nil {
    log.Println("cannot set key to redis:", err)
  }

  // Get value from key
  v, err := rdctx.Get(ctx, "foo")
  if err != nil {
    log.Println("cannot get key from redis:", err)
  }
  log.Println("result is", v)
}
Inject to echo middleware
e.Use(echo.WrapMiddleware(rdctx.Middleware(client)))
Set multiple key-value pairs with a single command
pairs := []rdctx.KeyValue{
  {"foo1", "bar1"},
  {"foo2", "bar2"},
}
err := rdctx.MSetEx(ctx, pairs, 24*time.Hour)

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Del

func Del(ctx context.Context, keys ...string) (int64, error)

func Expire

func Expire(ctx context.Context, key string, exp time.Duration) (bool, error)

func Get

func Get(ctx context.Context, key string) (string, error)

func Incr

func Incr(ctx context.Context, key string) (int64, error)

func Keys

func Keys(ctx context.Context, pattern string) ([]string, error)

func MGet

func MGet(ctx context.Context, keys ...string) ([]interface{}, error)

func MSetEx

func MSetEx(ctx context.Context, keyValues []KeyValue, exp time.Duration) error

func Middleware

func Middleware(c *Client) func(h http.Handler) http.Handler

func NewContext

func NewContext(ctx context.Context, c *Client) context.Context

func Scan

func Scan(ctx context.Context, cursor uint64, match string, count int64) ([]string, uint64, error)

func Set

func Set(ctx context.Context, key string, value interface{}) (string, error)

func SetEx

func SetEx(ctx context.Context, key string, value interface{}, exp time.Duration) (string, error)

func SetKeyPrefix

func SetKeyPrefix(prefix string)

Types

type Client

type Client struct {
	*redis.Client
}

func New

func New(addr string, password string, db int) *Client

func NewWithContext

func NewWithContext(ctx context.Context, addr string, password string, db int) (*Client, context.Context)

func NewWithOption

func NewWithOption(opts ...Option) *Client

func (*Client) ConnOK

func (c *Client) ConnOK() error

connection ok if no error

type KeyValue

type KeyValue struct {
	Key   string
	Value interface{}
}

type Option

type Option func(*Client)

func WithOption

func WithOption(opt *redis.Options) Option

Jump to

Keyboard shortcuts

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