Skip to content
roseduan edited this page May 23, 2022 · 3 revisions

Set

err = db.Set([]byte("name"), []byte("RoseDB"))
if err != nil {
   fmt.Printf("write data err: %v", err)
   return
}

SetEX

err = db.SetEX([]byte("type"), []byte("RoseDB-Strs"), time.Second*5)
if err != nil {
   fmt.Printf("write data err: %v", err)
   return
}

SetNX

err = db.SetNX([]byte("cmd"), []byte("SetNX"))
if err != nil {
   fmt.Printf("write data err: %v", err)
   return
}

MSet

err = db.MSet([]byte("key-1"), []byte("value-1"), []byte("key-2"), []byte("value-2"))
if err != nil {
   fmt.Printf("mset error: %v\n", err)
   return
}

MSetNX

err = db.MSetNX([]byte("key-11"), []byte("value-11"), []byte("key-22"), []byte("value-22"))
if err != nil {
   fmt.Printf("msetnx error: %v\n", err)
}
val, _ := db.Get([]byte("key-11"))
fmt.Printf("key-11: %v\n", string(val))

Get

v, err := db.Get([]byte("name"))
if err != nil {
   fmt.Printf("read data err: %v", err)
   return
}
fmt.Println("val = ", string(v))

MGet

keys := [][]byte{
   []byte("key-1"),
   []byte("not exist"),
   []byte("key-11"),
}
vals, err := db.MGet(keys)
if err != nil {
   fmt.Printf("mget err : %v\n", err)
} else {
   fmt.Printf("mget values : %v\n", vals)
}

Delete

err = db.Delete([]byte("name"))
if err != nil {
   fmt.Printf("delete data err: %v", err)
}

GetDel

_, err = db.GetDel([]byte("not-exist"))
if err != nil {
   fmt.Printf("getdel data err: %v", err)
}
gdVal, err := db.GetDel([]byte("key-22"))
if err != nil {
   fmt.Printf("getdel data err: %v", err)
} else {
   fmt.Println("getdel val : ", string(gdVal))
}
Clone this wiki locally