Code Monkey home page Code Monkey logo

Comments (1)

thisismz avatar thisismz commented on June 3, 2024

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)
}
`

from buntdb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.