Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't understand example #88

Open
marcelloh opened this issue May 11, 2022 · 1 comment
Open

don't understand example #88

marcelloh opened this issue May 11, 2022 · 1 comment

Comments

@marcelloh
Copy link

In this example:

db.Update(func(tx *buntdb.Tx) error {
	tx.Set("user:0:name", "tom", nil)
	tx.Set("user:1:name", "Randi", nil)
	tx.Set("user:2:name", "jane", nil)
	tx.Set("user:4:name", "Janet", nil)
	tx.Set("user:5:name", "Paula", nil)
	tx.Set("user:6:name", "peter", nil)
	tx.Set("user:7:name", "Terri", nil)
	return nil
})

what is user exactly?

@thisismz
Copy link

I am sending you a complete example of how it works. It's not difficult or complicated, and using this project is very easy.
package main

import (
	"fmt"
	"time"

	"github.com/tidwall/buntdb"
)

var BUNT *buntdb.DB

func Bunt() *buntdb.DB {
	client, err := buntdb.Open(":memory:")
	if err != nil {
		return nil
	}
	fmt.Println("BuntDB connect")
	return client
}

type Store struct{}

// set key to db
func (s Store) SetToBuntWithExpireTime(key string, value string, expire int) (err error) {
	timer := time.Duration(expire) * time.Second
	err = BUNT.Update(func(tx *buntdb.Tx) error {
		tx.Set(key, value, &buntdb.SetOptions{Expires: true, TTL: timer})
		return nil
	})
	return err
}

// get key from db
func (s Store) GetFromBunt(key string) (value string, err error) {
	var val string
	err = BUNT.View(func(tx *buntdb.Tx) error {
		val, err = tx.Get(key)
		if err != nil {
			return err
		}
		return nil
	})
	return val, err
}

// set key to db
func (s Store) SetToBunt(key string, value string) (err error) {
	err = BUNT.Update(func(tx *buntdb.Tx) error {
		_, _, err := tx.Set(key, value, nil)
		return err
	})
	return err
}

// delete key from db
func (s Store) DeleteFromBunt(key string) (err error) {
	err = BUNT.Update(func(tx *buntdb.Tx) error {
		tx.Delete(key)
		return nil
	})
	return err
}
func main() {
	BUNT = Bunt()
	store := Store{}
	err := store.SetToBuntWithExpireTime("key", "value", 3600)
	if err != nil {
		panic(err)
	}
	val, err := store.GetFromBunt("key")
	if err != nil {
		fmt.Println("no key find")
	}
	fmt.Println(val)
}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants