redis

package module
v0.0.0-...-39ab993 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2018 License: MIT Imports: 6 Imported by: 0

README

gokv/redis

GoDoc Build Status

An abstraction over Redis that implements the Store interface defined in gokv/store.

Maturity

This package is not stable because gokv/store is not stable yet. The Store implementations are experimental software.

Use

Initialise calling New with the address and the (optional) password to Redis.

s := redis.New("localhost:6379", "secret")
defer s.Close()

In order to be stored, a type must implement json.Marshaler. Similarly, the Get method of a store accepts a pointer to a json.Unmarshaler. Here is a full example of storing and retrieving a simple User object.

type User struct {
	FirstName, LastName string
}

func (u *User) UnmarshalJSON(data []byte) error {
	var ujson struct {
		FirstName string `json:"first_name"`
		LastName  string `json:"last_name"`
	}

	if err := json.Unmarshal(data, &ujson); err != nil {
		return err
	}

	*u = User{
		FirstName: ujson.FirstName,
		LastName:  ujson.LastName,
	}

	return nil
}

func (u User) MarshalJSON() ([]byte, error) {
	return json.Marshal(struct {
		FirstName string `json:"first_name"`
		LastName  string `json:"last_name"`
	}{
		FirstName: u.FirstName,
		LastName:  u.LastName,
	})
}

func main() {

	// New instantiates a "github.com/go-redis/redis" connection
	s := redis.New(os.Getenv("REDIS_ADDR"), os.Getenv("REDIS_PASS"))
	defer s.Close()

	// Call Ping to check readiness
	if err := s.Ping(); err!=nil {
		panic(err)
	}

	given := User{"Giacomo", "Leopardi"}

	if err := s.Add(123, given); err != nil {
		panic(err)
	}

	var found User
	ok, err := s.Get(123, &found)

	if err != nil {
		panic(fmt.Errorf("failure: %s", err))
	}

	if !ok {
		panic(errors.New("user not found"))
	}

	// given == found
}

Test

An empty and disposable Redis instance must be running at REDIS_ADDR (default localhost:6379) with password REDIS_PASS (empty by default).

With Docker:

docker run -d --name redis -p 6379:6379 redis:4-alpine
go test -v .

Documentation

Overview

Package redis defines Store, a Redis driver that partially implements the Store interface defined in "github.com/gokv/store".

This package is a wrapper around "github.com/go-redis/redis".

Index

Constants

This section is empty.

Variables

View Source
var ErrDuplicateKey = errors.New("duplicate key")

Functions

This section is empty.

Types

type Store

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

func New

func New(address, password string) Store

func (Store) Add

func (s Store) Add(_ context.Context, v json.Marshaler) (string, error)

Add persists a new object with a new UUIDv4 key. Err is non-nil in case of failure.

func (Store) Close

func (s Store) Close() error

func (Store) Get

func (s Store) Get(_ context.Context, k string, v json.Unmarshaler) (bool, error)

Get returns the value corresponding the key, and a nil error. If no match is found, returns (false, nil).

func (Store) Ping

func (s Store) Ping(ctx context.Context) (err error)

func (Store) Set

func (s Store) Set(_ context.Context, k string, v json.Marshaler) error

Set assigns the given value to the given key, possibly overwriting.

func (Store) SetWithDeadline

func (s Store) SetWithDeadline(ctx context.Context, k string, v json.Marshaler, deadline time.Time) error

SetWithDeadline assigns the given value to the given key, possibly overwriting. The assigned key will clear after deadline.

func (Store) SetWithTimeout

func (s Store) SetWithTimeout(_ context.Context, k string, v json.Marshaler, timeout time.Duration) error

SetWithTimeout assigns the given value to the given key, possibly overwriting. The assigned key will clear after timeout.

Jump to

Keyboard shortcuts

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