kv

package
v0.0.0-...-33ef150 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2017 License: Apache-2.0 Imports: 14 Imported by: 0

README

KV GoDoc

Provides a high level abstraction for key-value stores.

Interface

type KV interface {
	Close() error
	Get(key string) (*Item, error)
	Del(key string) error
	Put(item *Item) error
	String() string
}

type Item struct {
	Key        string
	Value      []byte
	Expiration time.Duration
}

func NewKV(opts ...Option) KV {
	return newOS(opts...)
}

Supported Backends

Usage

KV provides a simple Key-Value abstraction. The default implementation a form of broadcast for announcement and in memory key-value via RPC. It's embedded within the service as the KV handler and has less than a second startup time. Ring participation is done via the go-micro/broker and a gossip topic which can be changed with the variable kv.GossipTopic. Alternatively you can use redis or memcached.

Here's an example using the in-memory distributed store.

package main

import (
        "fmt"
        "time"

        "github.com/micro/go-micro"
        "github.com/micro/go-os/kv"
)

func main() {
        // Create a new service. 
        service := micro.NewService()

        // Create KV instance
        keyval := kv.NewKV(
                kv.Client(service.Client()),
                kv.Server(service.Server()),
        )
        defer keyval.Close()

	// write some keys and try get them back
        go func() {
                for i := 0; i < 10; i++ {
                        time.Sleep(time.Second)
                        key := fmt.Sprintf("greeting-%d", i)
                        err := keyval.Put(&kv.Item{
                                Key:        key,
                                Value:      []byte(`hello`),
                                Expiration: time.Second * 20,
                        })
                        if err != nil {
                                fmt.Println("put err", err)
                                continue
                        }

                        item, err := keyval.Get(key)
                        if err != nil {
                                fmt.Println("get err", err)
                                continue
                        }
                        fmt.Println("got", item)
                }
        }()

        service.Run()
}

Documentation

Overview

Package kv is an interface for key-value storage.

Index

Constants

This section is empty.

Variables

View Source
var (
	GossipTopic = "go.micro.kv.announce"
	GossipEvent = time.Second * 1
	ReaperEvent = time.Second * 10
)
View Source
var (
	ErrNotFound = errors.New("not found")
)

Functions

func NewContext

func NewContext(ctx context.Context, c KV) context.Context

Types

type Announcement

type Announcement struct {
	// Which namespace does it belong to
	Namespace string
	Address   string
	Timestamp int64
}

func (*Announcement) ContentType

func (a *Announcement) ContentType() string

func (*Announcement) Message

func (a *Announcement) Message() interface{}

func (*Announcement) Topic

func (a *Announcement) Topic() string

type Item

type Item struct {
	Key        string
	Value      []byte
	Expiration time.Duration
}

type KV

type KV interface {
	Close() error
	Get(key string) (*Item, error)
	Del(key string) error
	Put(item *Item) error
	String() string
}

func FromContext

func FromContext(ctx context.Context) (KV, bool)

func NewKV

func NewKV(opts ...Option) KV

type Option

type Option func(o *Options)

func Client

func Client(c client.Client) Option

func Internal

func Internal(b bool) Option

Internal specifies whether to use advertise handlers to discovery

func Namespace

func Namespace(ns string) Option

func Server

func Server(s server.Server) Option

func Servers

func Servers(s ...string) Option

func Service

func Service(b bool) Option

Service specifies whether to use the KV service. Defaults to false.

type Options

type Options struct {
	// Servers that store values for access
	Servers []string
	// Limit the scope of queries to a namespace
	// In the case of platform it essentially
	// means the topic we pub to for "gossip"
	// For memcache and redis it could be key/prefix
	Namespace string
	// Number of replicas
	// Default is 1 ofcourse
	Replicas int

	// Use a service rather than gossip
	Service bool
	// Whether handlers should be internal
	Internal bool

	// Replace with go-micro.Service?
	Client client.Client
	Server server.Server

	// Alternative options set using Context
	Context context.Context
}

Directories

Path Synopsis
Package go_micro_os_kv is a generated protocol buffer package.
Package go_micro_os_kv is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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