redis

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2023 License: Apache-2.0 Imports: 11 Imported by: 4

README

Valkeyrie Redis

GoDoc Build Status Go Report Card

valkeyrie provides a Go native library to store metadata using Distributed Key/Value stores (or common databases).

Compatibility

A storage backend in valkeyrie implements (fully or partially) the Store interface.

Calls Redis
Put 🟢️
Get 🟢️
Delete 🟢️
Exists 🟢️
Watch 🟢️
WatchTree 🟢️
NewLock (Lock/Unlock) 🟢️
List 🟢️
DeleteTree 🟢️
AtomicPut 🟢️
AtomicDelete 🟢️

Supported Versions

Redis versions >= 3.2.6. Key space notification needs to be enabled to have access to Watch and Lock methods.

Examples

package main

import (
	"context"
	"log"

	"github.com/kvtools/redis"
	"github.com/kvtools/valkeyrie"
)

func main() {
	ctx := context.Background()

	config := &redis.Config{
		Bucket: "example",
	}

	kv, err := valkeyrie.NewStore(ctx, redis.StoreName, []string{"localhost:8500"}, config)
	if err != nil {
		log.Fatal("Cannot create store")
	}

	key := "foo"

	err = kv.Put(ctx, key, []byte("bar"), nil)
	if err != nil {
		log.Fatalf("Error trying to put value at key: %v", key)
	}

	pair, err := kv.Get(ctx, key, nil)
	if err != nil {
		log.Fatalf("Error trying accessing value at key: %v", key)
	}

	log.Printf("value: %s", string(pair.Value))

	err = kv.Delete(ctx, key)
	if err != nil {
		log.Fatalf("Error trying to delete key %v", key)
	}
}

Documentation

Overview

Package redis contains the Redis store implementation.

Index

Constants

View Source
const StoreName = "redis"

StoreName the name of the store.

Variables

View Source
var (
	// ErrMultipleEndpointsUnsupported is thrown when there are
	// multiple endpoints specified for Redis.
	ErrMultipleEndpointsUnsupported = errors.New("redis: does not support multiple endpoints")

	// ErrAbortTryLock is thrown when a user stops trying to seek the lock
	// by sending a signal to the stop chan,
	// this is used to verify if the operation succeeded.
	ErrAbortTryLock = errors.New("redis: lock operation aborted")

	// ErrMasterSetMustBeProvided is thrown when Redis Sentinel is enabled
	// and the MasterName option is undefined.
	ErrMasterSetMustBeProvided = errors.New("master set name must be provided")

	// ErrInvalidRoutesOptions is thrown when Redis Sentinel is enabled
	// with RouteByLatency & RouteRandomly options without the ClusterClient.
	ErrInvalidRoutesOptions = errors.New("RouteByLatency and RouteRandomly options are only allowed with the ClusterClient")
)

Functions

This section is empty.

Types

type Codec

type Codec interface {
	Encode(kv *store.KVPair) (string, error)
	Decode(b []byte, kv *store.KVPair) error
}

Codec KVPair persistence interface.

type Config

type Config struct {
	TLS      *tls.Config
	Username string
	Password string
	DB       int
	Sentinel *Sentinel
}

Config the Redis configuration.

type JSONCodec

type JSONCodec struct{}

JSONCodec is a simple codec to read and write valkeyrie JSON object.

func (JSONCodec) Decode

func (c JSONCodec) Decode(b []byte, kv *store.KVPair) error

Decode a byte slice of valkeyrie JSON object to a KVPair.

func (JSONCodec) Encode

func (c JSONCodec) Encode(kv *store.KVPair) (string, error)

Encode a KVPair to a valkeyrie JSON object.

type RawCodec

type RawCodec struct{}

RawCodec is a simple codec to read and write string.

func (RawCodec) Decode

func (c RawCodec) Decode(b []byte, kv *store.KVPair) error

Decode a byte slice to a KVPair.

func (RawCodec) Encode

func (c RawCodec) Encode(kv *store.KVPair) (string, error)

Encode a KVPair to a string.

type Sentinel added in v1.1.0

type Sentinel struct {
	MasterName string
	Username   string
	Password   string

	// ClusterClient indicates whether to use the NewFailoverClusterClient to build the client.
	ClusterClient bool

	// Allows routing read-only commands to the closest master or replica node.
	// This option only works with NewFailoverClusterClient.
	RouteByLatency bool

	// Allows routing read-only commands to the random master or replica node.
	// This option only works with NewFailoverClusterClient.
	RouteRandomly bool

	// Route all commands to replica read-only nodes.
	ReplicaOnly bool

	// Use replicas disconnected with master when cannot get connected replicas
	// Now, this option only works in RandomReplicaAddr function.
	UseDisconnectedReplicas bool
}

Sentinel holds the Redis Sentinel configuration.

type Store

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

Store implements the store.Store interface.

func New

func New(ctx context.Context, endpoints []string, options *Config) (*Store, error)

New creates a new Redis client.

func NewWithCodec

func NewWithCodec(ctx context.Context, endpoints []string, options *Config, codec Codec) (*Store, error)

NewWithCodec creates a new Redis client with codec config.

func (*Store) AtomicDelete

func (r *Store) AtomicDelete(ctx context.Context, key string, previous *store.KVPair) (bool, error)

AtomicDelete is an atomic delete operation on a single value the value will be deleted if previous matched the one stored in db.

func (*Store) AtomicPut

func (r *Store) AtomicPut(ctx context.Context, key string, value []byte, previous *store.KVPair, opts *store.WriteOptions) (bool, *store.KVPair, error)

AtomicPut is an atomic CAS operation on a single value. Pass previous = nil to create a new key. We introduced script on this page, so atomicity is guaranteed.

func (*Store) Close

func (r *Store) Close() error

Close the store connection.

func (*Store) Delete

func (r *Store) Delete(ctx context.Context, key string) error

Delete the value at the specified key.

func (*Store) DeleteTree

func (r *Store) DeleteTree(ctx context.Context, directory string) error

DeleteTree deletes a range of keys under a given directory. glitch: we list all available keys first and then delete them all it costs two operations on redis, so is not atomicity.

func (*Store) Exists

func (r *Store) Exists(ctx context.Context, key string, _ *store.ReadOptions) (bool, error)

Exists verify if a Key exists in the store.

func (*Store) Get

func (r *Store) Get(ctx context.Context, key string, _ *store.ReadOptions) (*store.KVPair, error)

Get a value given its key.

func (*Store) List

func (r *Store) List(ctx context.Context, directory string, _ *store.ReadOptions) ([]*store.KVPair, error)

List the content of a given prefix.

func (*Store) NewLock

func (r *Store) NewLock(_ context.Context, key string, opts *store.LockOptions) (store.Locker, error)

NewLock creates a lock for a given key. The returned Locker is not held and must be acquired with `.Lock`. The Value is optional.

func (*Store) Put

func (r *Store) Put(ctx context.Context, key string, value []byte, opts *store.WriteOptions) error

Put a value at the specified key.

func (*Store) Watch

func (r *Store) Watch(ctx context.Context, key string, _ *store.ReadOptions) (<-chan *store.KVPair, error)

Watch for changes on a key. glitch: we use notified-then-retrieve to retrieve *store.KVPair. so the responses may sometimes inaccurate.

func (*Store) WatchTree

func (r *Store) WatchTree(ctx context.Context, directory string, _ *store.ReadOptions) (<-chan []*store.KVPair, error)

WatchTree watches for changes on child nodes under a given directory.

Jump to

Keyboard shortcuts

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