package main import ( "fmt" "time" "github.com/bolt-master" ) type boltdb struct { mdb *bolt.DB } var db boltdb func (m *boltdb) initBoltDb() bool { var err error var dbpath string dbPathLen := len(Gconfig.DB.DbPath) if Gconfig.DB.DbPath[dbPathLen-1] == '/' { dbpath = fmt.Sprintf("%scoinproxy.db", Gconfig.DB.DbPath) } else { dbpath = fmt.Sprintf("%s/coinproxy.db", Gconfig.DB.DbPath) } log.Debug("dbpath:%s", dbpath) m.mdb, err = bolt.Open(dbpath, 0600, &bolt.Options{Timeout: 1 * time.Second}) if err != nil { fmt.Println(err) return false } tx, err := m.mdb.Begin(true) if err != nil { fmt.Printf("mdb err:%s\n", err.Error()) return false } _, err = tx.CreateBucketIfNotExists([]byte(BucketSC)) if err != nil { fmt.Printf("create bucket err:%s\n", err.Error()) return false } if err := tx.Commit(); err != nil { return false } return true } func (m *boltdb) Put(bucket string, key, value []byte) error { err := m.mdb.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(bucket)) err := b.Put(key, value) return err }) return err } func (m *boltdb) Get(bucket string, key []byte) ([]byte, error) { var v []byte err := m.mdb.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(bucket)) v = b.Get(key) fmt.Printf("value:%s\n", string(v)) return nil }) return v, err } func (m *boltdb) Del(buckets string, key []byte) error { err := m.mdb.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(buckets)) err := b.Delete(key) return err }) return err }