reborn

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2020 License: MIT Imports: 6 Imported by: 0

README


Reborn

A redis-based configuration library developed using Go, ready to go, easy to use.

ci license


中文 | English


Install

go get github.com/jwma/reborn

Quick Start

package main

import (
	"fmt"
	"github.com/go-redis/redis"
	"github.com/jwma/reborn"
	"log"
)

func main() {
	// get your redis client here
	client := redis.NewClient(&redis.Options{
		Addr:        "127.0.0.1:6379",
		Password:    "",
		DB:          0,
		IdleTimeout: -1,
	})

	var err error

	// new empty config
	defaults := reborn.NewConfig()

	// set default config
	err = defaults.SetValue("websiteTitle", "Reborn")
	if err != nil {
		log.Printf("failed to set websiteTitle, error: %v\n", err)
	}

	err = defaults.SetValue("toggle", false)
	if err != nil {
		log.Printf("failed to set toggle, error: %v\n", err)
	}

	// create a Reborn instance using defaults config
	// this instance will store all the configs into YOUR_CONFIG_KEY Hash in Redis
	r, err := reborn.NewWithDefaults(client, "YOUR_CONFIG_KEY", defaults)

	// by passing the second parameter, 
	// you can get this default value when the key does not exists
	websiteTitle := r.GetValue("websiteTitle", "default value")
	fmt.Println(websiteTitle)                      // Output: Reborn
	fmt.Println(r.GetValue("noExistsKey", "oops")) // Output: oops

	toggle := r.GetBoolValue("toggle", false)
	fmt.Println(toggle)
}

More usage

You can use Reborn like below.

Create a Reborn instance without default configs

r, _ := reborn.New(client, "YOUR_CONFIG_KEY")

Save multiple Key-Value at one time

r, _ := reborn.New(client, "YOUR_CONFIG_KEY")

// SetValue() only update the Reborn instance configs
r.SetValue("discount", 8.8)
r.SetValue("websiteTitle", "Promotion")

// after SetValue(), you can call Persist() save you Key-Value into Redis.
r.Persist()

Start auto reload

After start auto reload function, Reborn will periodically use the database configs to override the config items of the Reborn instance. However, when the data of the Reborn instance changes, the auto reload function will skip the override operation and enter the next round of waiting until you call Persist (), the auto reload will continue the work of reload config items.

r, _ := reborn.New(client, "YOUR_CONFIG_KEY")
r.SetAutoReloadDuration(time.Second * 10)  // default duration: 5s
r.StartAutoReload()

// stop auto reload
r.StopAutoReload()

Supported types

The types you can pass when you call SetValue():

  • int
  • float64
  • string
  • bool
  • []int
  • []string
  • map[string]int
  • map[string]string

⚠️ You will get the UnsupportedValueTypeError when you pass unsupported types.

Call the below functions to get different types value:

  • GetValue() return string
  • GetIntValue() return int
  • GetFloat64Value() return float64
  • GetBoolValue() return bool
  • GetIntSliceValue() return []int
  • GetStringSliceValue() return []string
  • GetStringIntMapValue() return map[string]int
  • GetStringStringMapValue() return map[string]string

How to support the other types?

You can parse the variable to string, then you can call SetValue(). When you want to get this config item, you can call GetValue() to get string types variable, then you parse it back.

Not cool, but it works~

Notice

⚠️ DON'T FORGET CALL THE Persist()

After SetValue(), don't forget call Persist(), if not:

  1. Config items will not save into Redis.
  2. Auto reload function will not override the config items.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

func NewConfig

func NewConfig() *Config

func (*Config) GetBoolValue

func (c *Config) GetBoolValue(key string, defaults bool) bool

func (*Config) GetFloat64Value

func (c *Config) GetFloat64Value(key string, defaults float64) float64

func (*Config) GetIntSliceValue

func (c *Config) GetIntSliceValue(key string, defaults []int) []int

func (*Config) GetIntValue

func (c *Config) GetIntValue(key string, defaults int) int

func (*Config) GetStringIntMapValue

func (c *Config) GetStringIntMapValue(key string, defaults map[string]int) map[string]int

func (*Config) GetStringSliceValue

func (c *Config) GetStringSliceValue(key string, defaults []string) []string

func (*Config) GetStringStringMapValue

func (c *Config) GetStringStringMapValue(key string, defaults map[string]string) map[string]string

func (*Config) GetValue

func (c *Config) GetValue(key string, defaults string) string

func (*Config) SetValue

func (c *Config) SetValue(key string, value interface{}) error

type LoadFromDBError

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

func (LoadFromDBError) Error

func (l LoadFromDBError) Error() string

type Reborn

type Reborn struct {
	*Config
	// contains filtered or unexported fields
}

func New

func New(rdb *redis.Client, name string) (*Reborn, error)

func NewWithDefaults

func NewWithDefaults(rdb *redis.Client, name string, defaults *Config) (*Reborn, error)

func (*Reborn) Persist

func (r *Reborn) Persist() error

func (*Reborn) SetAutoReloadDuration

func (r *Reborn) SetAutoReloadDuration(d time.Duration)

func (*Reborn) StartAutoReload

func (r *Reborn) StartAutoReload()

func (*Reborn) StopAutoReload

func (r *Reborn) StopAutoReload()

type SyncDefaultsToDBError

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

func (SyncDefaultsToDBError) Error

func (s SyncDefaultsToDBError) Error() string

type UnsupportedValueTypeError

type UnsupportedValueTypeError struct {
	Value interface{}
}

func (UnsupportedValueTypeError) Error

Jump to

Keyboard shortcuts

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