Skip to content

dnjooiopa/rdctx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rdctx

GoDoc

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)

Pub/Sub

// subscribe
sub := rdctx.NewSubscriber(ctx, "channel1")
defer sub.Close()
sub.OnMessage(ctx, func(msg string) {
  log.Println("Message received:", msg)
})

// publish
rdctx.Publish(ctx, "channel1", "hello from publisher")

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages