ConsulKvCache

package module
v0.0.0-...-1a9e357 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

README

goConsulKVCache

golang Consul KV cache library

import in your project

import (
    "github.com/HaesungSeo/goConsulKVCache"
)

example code

simple

package main

import (
    "flag"
    "fmt"
    "reflect"
    "time"

    goConsulKVCache "github.com/HaesungSeo/goConsulKVCache"
)

func isMapSame(src, dst map[string]string) bool {
    return reflect.DeepEqual(src, dst)
}

func diff(src, dst map[string]string) map[string]string {
    differ := make(map[string]string)
    for k, val1 := range src {
        val2, ok := dst[k]
        if !ok {
            differ[k] = fmt.Sprintf("[%s] removed", k)
        }
        if val1 != val2 {
            differ[k] = fmt.Sprintf("[%s] [%s] -->[%s]", k, val1, val2)
        }
    }
    for k, val2 := range dst {
        _, ok := src[k]
        if !ok {
            differ[k] = fmt.Sprintf("[%s] New [%s]", k, val2)
        }
        // maybe already compared
    }
    return differ
}

// Watch example
func main() {
    addr := flag.String("s", ":8080", "consul server address")
    keyType := flag.String("t", "keyprefix", "consul watch type, one of key keyprefix")
    key := flag.String("k", "/", "consul kv key")

    flag.Parse()

    cfg, err := goConsulKVCache.NewCache(*addr, *keyType, *key)
    if err != nil {
        fmt.Printf("ERROR: %s\n", err)
        return
    }

    old := cfg.KVCopy()
    fmt.Printf("%s\n", old)
    for {
        select {
        case <-time.After(1 * time.Second):
            cur := cfg.KVCopy()
            if !isMapSame(cur, old) {
                diff := diff(old, cur)
                for _, v := range diff {
                    fmt.Printf("%s\n", v)
                }
            }
            old = cur
        }
    }
}

with two terminal, Run binary simple at first terminal,
then Run consul cli at the other one. watch the result

first terminal

./simple -k foo -t key
map[]
[foo] New [hello]
[foo] [hello] -->[world]
[foo] [world] -->[and goodbye]
[foo] [and goodbye] -->[]
[foo] New [again]

second terminal

$ consul kv put foo hello
Success! Data written to: foo
$ consul kv put foo world
Success! Data written to: foo
$ consul kv put foo "and goodbye"
Success! Data written to: foo
$ consul kv delete foo
Success! Deleted key: foo
$ consul kv put foo again
Success! Data written to: foo
$

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWatch = errors.New("unsupported watch type")
)

Functions

This section is empty.

Types

type ConsulKvCache

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

func NewCache

func NewCache(addr, keyType, key string) (*ConsulKvCache, error)

Watch example

func (*ConsulKvCache) KV

func (c *ConsulKvCache) KV(key string) string

func (*ConsulKvCache) KVCopy

func (c *ConsulKvCache) KVCopy() map[string]string

func (*ConsulKvCache) KVFlush

func (c *ConsulKvCache) KVFlush()

func (*ConsulKvCache) KVSet

func (c *ConsulKvCache) KVSet(key, value string)

func (*ConsulKvCache) Stop

func (c *ConsulKvCache) Stop(key string)

type KeytypeError

type KeytypeError struct {
	Err error
	// contains filtered or unexported fields
}

func (*KeytypeError) Error

func (e *KeytypeError) Error() string

func (*KeytypeError) Unwrap

func (e *KeytypeError) Unwrap() error

type ParseError

type ParseError struct {
	Query string
	Err   error
}

func (*ParseError) Error

func (e *ParseError) Error() string

func (*ParseError) Unwrap

func (e *ParseError) Unwrap() error

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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