nodis

package module
v1.5.5 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: GPL-3.0 Imports: 27 Imported by: 0

README

Nodis

GitHub top language GitHub Release

English | 简体中文

Redis re-implemented using golang. Simple way to embed in your application.

Supported Data Types

  • Bitmap
  • String
  • List
  • Hash
  • Set
  • Sorted Set

Features

  • Fast and embeddable
  • Low memory used, only hot data stored in memory
  • Snapshot and WAL for data storage.
  • Support custom data storage as backend.(e.g. S3, Browser, etc.)
  • Running on browser with WebAssembly. (^v1.2.0)
  • Support watch changes from remote. (^v1.2.0)
  • Support redis protocol. (^v1.5.0)

Supported Commands

Client Handling Configuration Key Commands String Commands Set Commands Hash Commands List Commands Sorted Set Commands
CLIENT FLUSHALL DEL GET SADD HSET LPUSH ZADD
PING FLUSHDB EXISTS SET SSCAN HGET RPUSH ZCARD
QUIT SAVE EXPIRE INCR SCARD HDEL LPOP ZRANK
ECHO INFO EXPIREAT DECR SPOP HLEN RPOP ZREVRANK
KEYS SETBIT SDIFF HKEYS LLEN ZSCORE
TTL GETBIT SINTER HEXISTS LINDEX ZINCRBY
RENAME INCR SISMEMBER HGETALL LINSERT ZRANGE
TYPE DESR SMEMBERS HINCRBY LPUSHX ZREVRANGE
SCAN SETEX SREM HICRBYFLOAT RPUSHX ZRANGEBYSCORE
INCRBY HSETNX LREM ZREVRANGEBYSCORE
DECRBY HMGET LSET ZREM
HMSET LRANGE ZREMRANGEBYRANK
HCLEAR LPOPRPUSH ZREMRANGEBYSCORE
HSCAN RPOPLPUSH ZCLEAR
HVALS BLPOP ZEXISTS
BRPOP

Get Started

 go get github.com/diiyw/nodis@latest

Or use test version

 go get github.com/diiyw/nodis@main
package main

import "github.com/diiyw/nodis"

func main() {
	// Create a new Nodis instance
	opt := nodis.DefaultOptions
	n := nodis.Open(opt)
	defer n.Close()
	// Set a key-value pair
	n.Set("key", []byte("value"))
	n.LPush("list", []byte("value1"))
}

Examples

Watch changes

Server:

package main

import (
	"fmt"
	"github.com/diiyw/nodis"
	"github.com/diiyw/nodis/pb"
	"github.com/diiyw/nodis/sync"
	"time"
)

func main() {
	var opt = nodis.DefaultOptions
	n := nodis.Open(opt)
	opt.Synchronizer = sync.NewWebsocket()
	n.Watch([]string{"*"}, func(op *pb.Operation) {
		fmt.Println("Server:", op.Key, string(op.Value))
	})
	go func() {
		for {
			time.Sleep(time.Second)
			n.Set("test", []byte(time.Now().Format("2006-01-02 15:04:05")))
		}
	}()
	err := n.Publish("127.0.0.1:6380", []string{"*"})
	if err != nil {
		panic(err)
	}
}
  • Browser client built with WebAssembly
GOOS=js GOARCH=wasm go build -o test.wasm
package main

import (
	"fmt"
	"github.com/diiyw/nodis"
	"github.com/diiyw/nodis/fs"
	"github.com/diiyw/nodis/pb"
	"github.com/diiyw/nodis/sync"
)

func main() {
	var opt = nodis.DefaultOptions
	opt.Filesystem = &fs.Memory{}
	opt.Synchronizer = sync.NewWebsocket()
	n := nodis.Open(opt)
	n.Watch([]string{"*"}, func(op *pb.Operation) {
		fmt.Println("Subscribe: ", op.Key)
	})
	err := n.Subscribe("ws://127.0.0.1:6380")
	if err != nil {
		panic(err)
	}
	select {}
}
Simple Redis Server
package main

import (
	"fmt"
	"net/http"

	"github.com/diiyw/nodis"
)

func main() {
	opt := nodis.DefaultOptions
	n := nodis.Open(opt)
	if err := n.Serve(":6380"); err != nil {
		fmt.Printf("Serve() = %v", err)
	}
}

You can use redis-cli to connect to the server.

redis-cli -p 6380
> set key value

Benchmark

Embed benchmark

Windows 11: 12C/32G

goos: windows
goarch: amd64
pkg: github.com/diiyw/nodis/bench
BenchmarkSet-12         	 1469863	        715.9 ns/op	     543 B/op	       7 allocs/op
BenchmarkGet-12         	12480278	        96.47 ns/op	       7 B/op	       0 allocs/op
BenchmarkLPush-12       	 1484466	        786.2 ns/op	     615 B/op	       9 allocs/op
BenchmarkLPop-12        	77275986	        15.10 ns/op	       0 B/op	       0 allocs/op
BenchmarkSAdd-12        	 1542252	        831.9 ns/op	     663 B/op	      10 allocs/op
BenchmarkSMembers-12    	12739020	        95.18 ns/op	       8 B/op	       1 allocs/op
BenchmarkZAdd-12        	 1000000	        1177 ns/op	     550 B/op	      10 allocs/op
BenchmarkZRank-12       	11430135	        104.1 ns/op	       7 B/op	       0 allocs/op
BenchmarkHSet-12        	 1341817	        863.5 ns/op	     743 B/op	      11 allocs/op
BenchmarkHGet-12        	 9801158	        105.9 ns/op	       7 B/op	       0 allocs/op

Linux VM: 4C/8GB

goos: linux
goarch: amd64
pkg: github.com/diiyw/nodis/bench             
BenchmarkSet-4        	  806912	      1658 ns/op	     543 B/op	       7 allocs/op
BenchmarkGet-4        	 5941904	       190.6 ns/op	       7 B/op	       0 allocs/op
BenchmarkLPush-4      	  852932	      1757 ns/op	     615 B/op	       9 allocs/op
BenchmarkLPop-4       	40668902	        27.22 ns/op	       0 B/op	       0 allocs/op
BenchmarkSAdd-4       	  706376	      1913 ns/op	     662 B/op	      10 allocs/op
BenchmarkSMembers-4   	 4819993	       208.1 ns/op	       8 B/op	       1 allocs/op
BenchmarkZAdd-4       	  729039	      2013 ns/op	     550 B/op	      10 allocs/op
BenchmarkZRank-4      	 4959448	       246.4 ns/op	       7 B/op	       0 allocs/op
BenchmarkHSet-4       	  735676	      1971 ns/op	     742 B/op	      11 allocs/op
BenchmarkHGet-4       	 4442625	       243.4 ns/op	       7 B/op	       0 allocs/op

Note

If you want to persist data, please make sure to call the Close() method when your application exits.

Documentation

Index

Constants

View Source
const (
	FileSizeKB = 1024
	FileSizeMB = 1024 * FileSizeKB
	FileSizeGB = 1024 * FileSizeMB
)

Variables

View Source
var DefaultOptions = &Options{
	Path:         "data",
	FileSize:     FileSizeGB,
	TidyDuration: 60 * time.Second,
	MetaPoolSize: 10240,
}
View Source
var (
	ErrCorruptedData = errors.New("corrupted data")
)
View Source
var (
	ErrUnknownOperation = errors.New("unknown operation")
)

Functions

This section is empty.

Types

type Key

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

type Nodis

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

func Open

func Open(opt *Options) *Nodis

func (*Nodis) BLPop

func (n *Nodis) BLPop(timeout time.Duration, keys ...string) (string, []byte)

func (*Nodis) BRPop

func (n *Nodis) BRPop(timeout time.Duration, keys ...string) (string, []byte)

func (*Nodis) BitCount added in v1.2.0

func (n *Nodis) BitCount(key string, start, end int64) int64

BitCount returns the number of bits set to 1

func (*Nodis) Clear

func (n *Nodis) Clear()

Clear removes all keys from the store

func (*Nodis) Close added in v1.0.8

func (n *Nodis) Close() error

Close the store

func (*Nodis) Decr added in v1.5.0

func (n *Nodis) Decr(key string) int64

Decr decrement the integer value of a key by one

func (*Nodis) DecrBy added in v1.5.2

func (n *Nodis) DecrBy(key string, decrement int64) int64

func (*Nodis) Del

func (n *Nodis) Del(keys ...string) int64

Del a key

func (*Nodis) Exists

func (n *Nodis) Exists(keys ...string) int64

func (*Nodis) Expire

func (n *Nodis) Expire(key string, seconds int64) int64

Expire the keys

func (*Nodis) ExpireAt

func (n *Nodis) ExpireAt(key string, timestamp time.Time) int64

ExpireAt the keys

func (*Nodis) ExpireAtGT added in v1.5.0

func (n *Nodis) ExpireAtGT(key string, timestamp time.Time) int64

ExpireAtGT the keys only when the new expiry is greater than current one

func (*Nodis) ExpireAtLT added in v1.5.0

func (n *Nodis) ExpireAtLT(key string, timestamp time.Time) int64

ExpireAtLT the keys only when the new expiry is less than current one

func (*Nodis) ExpireAtNX added in v1.5.0

func (n *Nodis) ExpireAtNX(key string, timestamp time.Time) int64

ExpireAtNX the keys only when the key has no expiry

func (*Nodis) ExpireAtXX added in v1.5.0

func (n *Nodis) ExpireAtXX(key string, timestamp time.Time) int64

ExpireAtXX the keys only when the key has an existing expiry

func (*Nodis) ExpireGT added in v1.5.0

func (n *Nodis) ExpireGT(key string, seconds int64) int64

ExpireGT the keys only when the new expiry is greater than current one

func (*Nodis) ExpireLT added in v1.5.0

func (n *Nodis) ExpireLT(key string, seconds int64) int64

ExpireLT the keys only when the new expiry is less than current one

func (*Nodis) ExpireNX added in v1.5.0

func (n *Nodis) ExpireNX(key string, seconds int64) int64

ExpireNX the keys only when the key has no expiry

func (*Nodis) ExpirePX added in v1.5.0

func (n *Nodis) ExpirePX(key string, milliseconds int64) int64

ExpirePX the keys in milliseconds

func (*Nodis) ExpireXX added in v1.5.0

func (n *Nodis) ExpireXX(key string, seconds int64) int64

ExpireXX the keys only when the key has an existing expiry

func (*Nodis) Get

func (n *Nodis) Get(key string) []byte

Get a key

func (*Nodis) GetBit added in v1.2.0

func (n *Nodis) GetBit(key string, offset int64) int64

GetBit get a bit in a key

func (*Nodis) GetEntry added in v1.2.0

func (n *Nodis) GetEntry(key string) []byte

GetEntry gets an entity

func (*Nodis) HClear

func (n *Nodis) HClear(key string)

func (*Nodis) HDel

func (n *Nodis) HDel(key string, fields ...string) int64

func (*Nodis) HExists

func (n *Nodis) HExists(key string, field string) bool

func (*Nodis) HGet

func (n *Nodis) HGet(key string, field string) []byte

func (*Nodis) HGetAll

func (n *Nodis) HGetAll(key string) map[string][]byte

func (*Nodis) HIncrBy

func (n *Nodis) HIncrBy(key string, field string, value int64) int64

func (*Nodis) HIncrByFloat

func (n *Nodis) HIncrByFloat(key string, field string, value float64) float64

func (*Nodis) HKeys

func (n *Nodis) HKeys(key string) []string

func (*Nodis) HLen

func (n *Nodis) HLen(key string) int64

func (*Nodis) HMGet

func (n *Nodis) HMGet(key string, fields ...string) [][]byte

func (*Nodis) HMSet

func (n *Nodis) HMSet(key string, fields map[string][]byte)

func (*Nodis) HScan

func (n *Nodis) HScan(key string, cursor int64, match string, count int64) (int64, map[string][]byte)

func (*Nodis) HSet

func (n *Nodis) HSet(key string, field string, value []byte)

func (*Nodis) HSetNX

func (n *Nodis) HSetNX(key string, field string, value []byte) bool

func (*Nodis) HVals

func (n *Nodis) HVals(key string) [][]byte

func (*Nodis) Incr added in v1.5.0

func (n *Nodis) Incr(key string) int64

Incr increment the integer value of a key by one

func (*Nodis) IncrBy added in v1.5.2

func (n *Nodis) IncrBy(key string, increment int64) int64

func (*Nodis) Keys

func (n *Nodis) Keys(pattern string) []string

Keys gets the keys

func (*Nodis) LIndex

func (n *Nodis) LIndex(key string, index int64) []byte

func (*Nodis) LInsert

func (n *Nodis) LInsert(key string, pivot, data []byte, before bool) int64

func (*Nodis) LLen

func (n *Nodis) LLen(key string) int64

func (*Nodis) LPop

func (n *Nodis) LPop(key string, count int64) [][]byte

func (*Nodis) LPopRPush

func (n *Nodis) LPopRPush(source, destination string) []byte

func (*Nodis) LPush

func (n *Nodis) LPush(key string, values ...[]byte) int64

func (*Nodis) LPushX

func (n *Nodis) LPushX(key string, data []byte) int64

func (*Nodis) LRange

func (n *Nodis) LRange(key string, start, stop int64) [][]byte

func (*Nodis) LRem

func (n *Nodis) LRem(key string, count int64, data []byte) int64

func (*Nodis) LSet

func (n *Nodis) LSet(key string, index int64, data []byte) bool

func (*Nodis) LTrim

func (n *Nodis) LTrim(key string, start, stop int64)

func (*Nodis) Patch added in v1.2.0

func (n *Nodis) Patch(ops ...*pb.Op) error

func (*Nodis) Publish added in v1.2.0

func (n *Nodis) Publish(addr string, pattern []string) error

func (*Nodis) RPop

func (n *Nodis) RPop(key string, count int64) [][]byte

func (*Nodis) RPopLPush

func (n *Nodis) RPopLPush(source, destination string) []byte

func (*Nodis) RPush

func (n *Nodis) RPush(key string, values ...[]byte) int64

func (*Nodis) RPushX

func (n *Nodis) RPushX(key string, data []byte) int64

func (*Nodis) Rename

func (n *Nodis) Rename(key, key2 string) error

Rename a key

func (*Nodis) SAdd

func (n *Nodis) SAdd(key string, members ...string) int64

SAdd adds the specified members to the set stored at key.

func (*Nodis) SCard

func (n *Nodis) SCard(key string) int64

SCard gets the set members count.

func (*Nodis) SDiff

func (n *Nodis) SDiff(keys ...string) []string

SDiff gets the difference between sets.

func (*Nodis) SInter

func (n *Nodis) SInter(keys ...string) []string

SInter gets the intersection between sets.

func (*Nodis) SIsMember

func (n *Nodis) SIsMember(key, member string) bool

SIsMember returns if member is a member of the set stored at key.

func (*Nodis) SMembers

func (n *Nodis) SMembers(key string) []string

SMembers returns all the members of the set value stored at key.

func (*Nodis) SPop added in v1.5.0

func (n *Nodis) SPop(key string, count int64) []string

SPop removes and returns a random element from the set value stored at key.

func (*Nodis) SRem added in v1.1.0

func (n *Nodis) SRem(key string, members ...string) int64

SRem removes the specified members from the set stored at key.

func (*Nodis) SScan added in v1.5.0

func (n *Nodis) SScan(key string, cursor int64, match string, count int64) (int64, []string)

SScan scans the set value stored at key.

func (*Nodis) Scan

func (n *Nodis) Scan(cursor int64, match string, count int64) (int64, []string)

Scan the keys

func (*Nodis) Serve added in v1.5.0

func (n *Nodis) Serve(addr string) error

func (*Nodis) Set

func (n *Nodis) Set(key string, value []byte)

Set a key with a value and a TTL

func (*Nodis) SetBit added in v1.2.0

func (n *Nodis) SetBit(key string, offset int64, value bool) int

SetBit set a bit in a key

func (*Nodis) SetEX added in v1.5.0

func (n *Nodis) SetEX(key string, value []byte, seconds int64)

SetEX set a key with specified expire time, in seconds (a positive integer).

func (*Nodis) SetEntry added in v1.2.0

func (n *Nodis) SetEntry(data []byte) error

SetEntry sets an entity

func (*Nodis) SetNX added in v1.5.0

func (n *Nodis) SetNX(key string, value []byte) bool

SetNX set a key with a value if it does not exist

func (*Nodis) SetPX added in v1.5.0

func (n *Nodis) SetPX(key string, value []byte, milliseconds int64)

SetPX set a key with specified expire time, in milliseconds (a positive integer).

func (*Nodis) SetXX added in v1.5.0

func (n *Nodis) SetXX(key string, value []byte) bool

SetXX set a key with a value if it exists

func (*Nodis) Snapshot added in v1.0.8

func (n *Nodis) Snapshot(path string)

Snapshot saves the data to disk

func (*Nodis) Subscribe added in v1.2.0

func (n *Nodis) Subscribe(addr string) error

func (*Nodis) TTL

func (n *Nodis) TTL(key string) time.Duration

TTL gets the TTL

func (*Nodis) Type

func (n *Nodis) Type(key string) string

Type gets the type of key

func (*Nodis) UnWatch added in v1.2.0

func (n *Nodis) UnWatch(id int)

func (*Nodis) Watch added in v1.2.0

func (n *Nodis) Watch(pattern []string, fn func(op *pb.Operation)) int

func (*Nodis) ZAdd

func (n *Nodis) ZAdd(key string, member string, score float64)

func (*Nodis) ZAddGT added in v1.5.0

func (n *Nodis) ZAddGT(key string, member string, score float64) int64

ZAddGT add member if score greater than the current score

func (*Nodis) ZAddLT added in v1.5.0

func (n *Nodis) ZAddLT(key string, member string, score float64) int64

ZAddLT add member if score less than the current score

func (*Nodis) ZAddNX added in v1.5.0

func (n *Nodis) ZAddNX(key string, member string, score float64) int64

func (*Nodis) ZAddXX added in v1.5.0

func (n *Nodis) ZAddXX(key string, member string, score float64) int64

func (*Nodis) ZCard

func (n *Nodis) ZCard(key string) int64

func (*Nodis) ZClear

func (n *Nodis) ZClear(key string)

func (*Nodis) ZExists

func (n *Nodis) ZExists(key string, member string) bool

func (*Nodis) ZIncrBy

func (n *Nodis) ZIncrBy(key string, member string, score float64) float64

func (*Nodis) ZRange

func (n *Nodis) ZRange(key string, start int64, stop int64) []string

func (*Nodis) ZRangeByScore

func (n *Nodis) ZRangeByScore(key string, min float64, max float64) []string

func (*Nodis) ZRangeByScoreWithScores

func (n *Nodis) ZRangeByScoreWithScores(key string, min float64, max float64) []*zset.Item

func (*Nodis) ZRangeWithScores

func (n *Nodis) ZRangeWithScores(key string, start int64, stop int64) []*zset.Item

func (*Nodis) ZRank

func (n *Nodis) ZRank(key string, member string) int64

func (*Nodis) ZRankWithScore added in v1.5.0

func (n *Nodis) ZRankWithScore(key string, member string) (int64, *zset.Item)

func (*Nodis) ZRem

func (n *Nodis) ZRem(key string, members ...string) int64

func (*Nodis) ZRemRangeByRank

func (n *Nodis) ZRemRangeByRank(key string, start int64, stop int64) int64

func (*Nodis) ZRemRangeByScore

func (n *Nodis) ZRemRangeByScore(key string, min float64, max float64) int64

func (*Nodis) ZRevRange

func (n *Nodis) ZRevRange(key string, start int64, stop int64) []string

func (*Nodis) ZRevRangeByScore

func (n *Nodis) ZRevRangeByScore(key string, min float64, max float64) []string

func (*Nodis) ZRevRangeByScoreWithScores

func (n *Nodis) ZRevRangeByScoreWithScores(key string, min float64, max float64) []*zset.Item

func (*Nodis) ZRevRangeWithScores

func (n *Nodis) ZRevRangeWithScores(key string, start int64, stop int64) []*zset.Item

func (*Nodis) ZRevRank

func (n *Nodis) ZRevRank(key string, member string) int64

func (*Nodis) ZRevRankWithScore added in v1.5.0

func (n *Nodis) ZRevRankWithScore(key string, member string) (int64, *zset.Item)

func (*Nodis) ZScore

func (n *Nodis) ZScore(key string, member string) float64

type Options

type Options struct {
	// Path is the path to the database.
	Path string

	// TidyDuration is the interval which the database is flushing unused keys to disk.
	// This is useful for reducing the risk of data loss in the event of a crash.
	// It is also used for refreshing hot keys.
	TidyDuration time.Duration

	// FileSize is the size of each file. The default value is 1GB.
	FileSize int64

	// SnapshotDuration is the interval at which the database is snapshotted.
	// Default 0 for disabling snapshot. and you can call Snapshot manually.
	SnapshotDuration time.Duration

	// Filesystem is the filesystem to use. The default is the memory filesystem.
	Filesystem fs.Fs

	// Synchronizer is the synchronizer to use. The default is nil and no synchronization is performed.
	Synchronizer sync.Synchronizer

	// MetaPoolSize is the key metadata pool size
	MetaPoolSize int
}

Options represents the configuration options for the database.

Directories

Path Synopsis
ds
set
str
examples

Jump to

Keyboard shortcuts

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