jsonstore

package module
v0.0.0-...-93d3339 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2017 License: MIT Imports: 8 Imported by: 0

README ΒΆ

jsonstore πŸͺ

GoDoc

This package is a fork of schollz/jsonstore.

JSONStore is a Go-library for a simple thread-safe in-memory JSON key-store with persistent backend. It's made for those times where you don't need a RDBMS like MySQL, or a NoSQL like MongoDB - basically when you just need a simple keystore. A really simple keystore. JSONStore is used in those times you don't need a distributed keystore like etcd, or a remote keystore Redis or a local keystore like Bolt. Its really for those times where you just need a JSON file.

Usage

First, install the library using:

go get -u -v github.com/shogo82148/jsonstore

Then you can add it to your program. Check out the examples, or see below for basic usage:

ks := new(jsonstore.JSONStore)

// set a key to any object you want
type Human struct {
  Name   string
  Height float64
}
err := ks.Set("human:1", Human{"Dante", 5.4})
if err != nil {
  panic(err)
}

// Saving will automatically gzip if .gz is provided
if err = jsonstore.Save(ks, "humans.json.gz"); err != nil {
  panic(err)
}

// Load any JSON / GZipped JSON
ks2, err := jsonstore.Open("humans.json.gz")
if err != nil {
  panic(err)
}

// get the data back via an interface
var human Human
err = ks2.Get("human:1", &human)
if err != nil {
  panic(err)
}
fmt.Println(human.Name) // Prints 'Dante'

The datastore on disk is then contains:

$ zcat humans.json.gz
{"human:1":{"Name":"Dante","Height":5.4}}

License

MIT

Copyright (c) 2017 Zack Copyright (c) 2017 shogo82148

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func Save ΒΆ

func Save(ks *JSONStore, filename string) error

Save writes the jsonstore to disk.

func SaveAndRename ΒΆ

func SaveAndRename(ks *JSONStore, filename string) error

SaveAndRename writes the jsonstore to disk more safely. First, SaveAndRename writes the jsonstore to temporary file, and then rename it to filename. NOTE: os.Rename renames atomic on POSIX systems, but no guarantee on other systems.

Types ΒΆ

type JSONStore ΒΆ

type JSONStore struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

JSONStore is the basic store object.

func Open ΒΆ

func Open(filename string) (*JSONStore, error)

Open will load a jsonstore from a file.

func (*JSONStore) Delete ΒΆ

func (s *JSONStore) Delete(key string)

Delete removes a key from the store.

func (*JSONStore) Get ΒΆ

func (s *JSONStore) Get(key string, v interface{}) error

Get will return the value associated with a key.

func (*JSONStore) GetAll ΒΆ

func (s *JSONStore) GetAll(matcher func(key string) bool) *JSONStore

GetAll is like a filter with a regexp.

func (*JSONStore) Keys ΒΆ

func (s *JSONStore) Keys() []string

Keys returns all the keys currently in map

func (*JSONStore) Set ΒΆ

func (s *JSONStore) Set(key string, value interface{}) error

Set saves a value at the given key.

func (*JSONStore) Size ΒΆ

func (s *JSONStore) Size() int

Size returns the count element in the store.

func (*JSONStore) StartAutoSave ΒΆ

func (s *JSONStore) StartAutoSave(filename string, d time.Duration, count int64)

StartAutoSave starts auto saving.

func (*JSONStore) StopAutoSave ΒΆ

func (s *JSONStore) StopAutoSave()

StopAutoSave stops auto saving.

type NoSuchKeyError ΒΆ

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

NoSuchKeyError is thrown when calling Get with invalid key

func (NoSuchKeyError) Error ΒΆ

func (err NoSuchKeyError) Error() string

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

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