Key-Value Storage written in Go.

kvs Go Reference Go Report Card GitHub go.mod Go version GitHub release (latest SemVer) API Doc LICENSE

kvs is an in-memory key-value storage written in Go. It has 2 different usage. It can be used as a package by importing it to your code or as a server by creating an HTTP server. kvs stores persistent data in local machine's /tmp/kvs directory. The file extension is .kvs. For example, if you create a database named as user, it would be stored in a file name as users.kvs. It loads the data from file into memory if the database is already exists. Also, kvs supports saving data from memory to disk in a given time interval periodically. You can specify the time interval while creating the database. Both keys and values are stored as string. That's why the methods accept only strings.

Installation

You can add package to your project with the following command.

go get github.com/gozeloglu/kvs

Package Usage

Firstly, you need to create a database by calling kvs.Open(). It creates a new database if not exists or loads the data from existing database if it exists. If you want to use kvs as a package, you don't need to specify addr as a first parameter. As a third parameter, you pass time interval to save data to database periodically.

// Creates a "users" database and saves the data from memory to file per 2 minutes.
db, err := kvs.Open("", "users", 2*time.Minute)  

Then, simply you can call Set() and Get() methods. Set() takes key and value as parameters and adds the key-value pair to memory. Get() takes key as a parameter and returns the value of the key. Both Set() and Get() methods takes string as parameters.

// "john" is stored as key with "23" as value in memory.
db.Set("john", "23")

// Returns "23" to age.
age := db.Get("john")

If you want to make sure that all data stores in memory would save to disk, you can call Close() method. It writes the data to disk and closes the database.

// Writes data in memory to disk
db.Close()

If you want to see full code, you can take a look /example/pkg/main.go.

Package Example

package main

import (
	"fmt"
	"github.com/gozeloglu/kvs"
	"log"
	"time"
)

func main() {
	db, err := kvs.Open("", "users", 2*time.Minute)
	if err != nil {
		log.Fatalf(err.Error())
	}

	db.Set("john", "23")
	db.Set("jack", "43")

	johnAge := db.Get("john")
	fmt.Println(johnAge)

	jackAge := db.Get("jack")
	fmt.Println(jackAge)

	db.Del("jack")

	jack = db.Get("jack")
	fmt.Println("Jack:", jack)

	newAge, err := db.Incr("john")
	if err != nil {
		log.Fatalf(err.Error())
	}
	fmt.Printf("John's new age is %s", newAge)

	newAge, err = db.IncrBy("john", 3)
	if err != nil {
		log.Fatalf(err.Error())
	}
	fmt.Printf("John's new age is %s\n", newAge)

	decrAge, err := db.Decr("john")
	if err != nil {
		log.Fatalf(err.Error())
	}
	fmt.Printf("John's decremented age is: %s\n", decrAge)

	decrAge, err = db.DecrBy("john", 3)
	if err != nil {
		log.Fatalf(err.Error())
	}
	fmt.Printf("John's decremented age is: %s\n", decrAge)

	exist := db.Exists("john")
	fmt.Println(exist)

	exist = db.Exists("jack")
	fmt.Println(exist)

	ok, err := db.Rename("john", "john john")
	if err != nil {
		log.Fatalf(err.Error())
	}
	fmt.Printf("key name changed: %v1\n", ok)

	keys := db.Keys()
	for _, k := range keys {
		fmt.Println(k)
	}

	err = db.Close() // Call while closing the database.
	if err != nil {
		log.Fatalf(err.Error())
	}
}

Server Usage

Server usage is so simple and short. You would call extra method, Open(), to start server. Default port is 1234 for kvs server. But, you can override it and specify another port number.

// Server runs on localhost:1234
db, _ := kvs.Create(":1234", "users", 2*time.Minute)

// The server is started
db.Open()

If you want to see full code, you can take a look /example/server/main.go. You can run this code directly without any configurations.

You can find the API Documentation from here .

Server Example

If you want to use as a server, you can just call two different functions. It creates endpoints for you.

package main

import (
	"github.com/gozeloglu/kvs"
	"log"
	"time"
)

func main() {
	db, err := kvs.Create(":1234", "users", 2*time.Minute)
	if err != nil {
		log.Fatalf(err.Error())
	}
	log.Printf("DB Created.")
	db.Open()
}

How to store data in file?

Key-value pairs store in files with .kvs extension. Data format is simple. There is = between key and value.

foo=bar
john=12
fizz=buzz

This is a sample data file.

NOTE

kvs is still under development stage, and it is created for experimental purposes.

LICENSE

MIT

Owner
Gökhan Özeloğlu
Graduated from @HacettepeUniversity CS - Software Engineer @yemeksepeti
Gökhan Özeloğlu
Similar Resources

An embedded key/value database for Go.

bbolt bbolt is a fork of Ben Johnson's Bolt key/value store. The purpose of this fork is to provide the Go community with an active maintenance and de

Jan 1, 2023

BuntDB is an embeddable, in-memory key/value database for Go with custom indexing and geospatial support

BuntDB is an embeddable, in-memory key/value database for Go with custom indexing and geospatial support

BuntDB is a low-level, in-memory, key/value store in pure Go. It persists to disk, is ACID compliant, and uses locking for multiple readers and a sing

Dec 30, 2022

ACID key-value database.

Coffer Simply ACID* key-value database. At the medium or even low latency it tries to provide greater throughput without losing the ACID properties of

Dec 7, 2022

A disk-backed key-value store.

What is diskv? Diskv (disk-vee) is a simple, persistent key-value store written in the Go language. It starts with an incredibly simple API for storin

Jan 7, 2023

An in-memory key:value store/cache (similar to Memcached) library for Go, suitable for single-machine applications.

go-cache go-cache is an in-memory key:value store/cache similar to memcached that is suitable for applications running on a single machine. Its major

Dec 29, 2022

LevelDB key/value database in Go.

This is an implementation of the LevelDB key/value database in the Go programming language. Installation go get github.com/syndtr/goleveldb/leveldb R

Jan 1, 2023

Low-level key/value store in pure Go.

Low-level key/value store in pure Go.

Description Package slowpoke is a simple key/value store written using Go's standard library only. Keys are stored in memory (with persistence), value

Jan 2, 2023

Key-value store for temporary items :memo:

Tempdb TempDB is Redis-backed temporary key-value store for Go. Useful for storing temporary data such as login codes, authentication tokens, and temp

Sep 26, 2022

A distributed key-value store. On Disk. Able to grow or shrink without service interruption.

Vasto A distributed high-performance key-value store. On Disk. Eventual consistent. HA. Able to grow or shrink without service interruption. Vasto sca

Jan 6, 2023
pure golang key database support key have value. 非常高效实用的键值数据库。
pure golang key database support key have value.  非常高效实用的键值数据库。

orderfile32 pure golang key database support key have value The orderfile32 is standard alone fast key value database. It have two version. one is thi

Apr 30, 2022
🔑A high performance Key/Value store written in Go with a predictable read/write performance and high throughput. Uses a Bitcask on-disk layout (LSM+WAL) similar to Riak.

bitcask A high performance Key/Value store written in Go with a predictable read/write performance and high throughput. Uses a Bitcask on-disk layout

Sep 26, 2022
A simple, fast, embeddable, persistent key/value store written in pure Go. It supports fully serializable transactions and many data structures such as list, set, sorted set.

NutsDB English | 简体中文 NutsDB is a simple, fast, embeddable and persistent key/value store written in pure Go. It supports fully serializable transacti

Jan 1, 2023
Embedded key-value store for read-heavy workloads written in Go
Embedded key-value store for read-heavy workloads written in Go

Pogreb Pogreb is an embedded key-value store for read-heavy workloads written in Go. Key characteristics 100% Go. Optimized for fast random lookups an

Jan 3, 2023
Fast and simple key/value store written using Go's standard library
Fast and simple key/value store written using Go's standard library

Table of Contents Description Usage Cookbook Disadvantages Motivation Benchmarks Test 1 Test 4 Description Package pudge is a fast and simple key/valu

Nov 17, 2022
BadgerDB is an embeddable, persistent and fast key-value (KV) database written in pure Go
BadgerDB is an embeddable, persistent and fast key-value (KV) database written in pure Go

BadgerDB BadgerDB is an embeddable, persistent and fast key-value (KV) database written in pure Go. It is the underlying database for Dgraph, a fast,

Dec 10, 2021
GalaxyDB is a hobbyist key-value database written in Go.

GalaxyDB GalaxyDB is a hobbyist key-value database written in Go Author: Andrew N ([email protected]) Features Data is stored via keys Operations Grafana

Mar 30, 2022
Eagle - Eagle is a fast and strongly encrypted key-value store written in pure Golang.

EagleDB EagleDB is a fast and simple key-value store written in Golang. It has been designed for handling an exaggerated read/write workload, which su

Dec 10, 2022
A SQLite-based hierarchical key-value store written in Go

camellia ?? A lightweight hierarchical key-value store camellia is a Go library that implements a simple, hierarchical, persistent key-value store, ba

Nov 9, 2022
Fast key-value DB in Go.
Fast key-value DB in Go.

BadgerDB BadgerDB is an embeddable, persistent and fast key-value (KV) database written in pure Go. It is the underlying database for Dgraph, a fast,

Dec 29, 2022