"github.com/xujiajun/nutsdb"   // 多文件kv支持事务
以下字符串操作kv
opt := nutsdb.DefaultOptions
    opt.Dir = "./nutsdb" //这边数据库会自动创建这个目录文件
    db, _ := nutsdb.Open(opt)
    defer db.Close()
//写
    db.Update(func(tx *nutsdb.Tx) error {
        key := []byte("name_001")
        val := []byte("测试数据")
        if err := tx.Put("bucket1", key, val, 0); err != nil {
            return err
        }//添加更新

        if err := tx.Delete("bucket1", key); err != nil {
            return err
        }//删除
        if err := tx.Put("bucket1", key, val, 60); err != nil {
            return err
        }//过期时间60s
        return nil
    })
    db.Update(
        func(tx *nutsdb.Tx) error {
            if err := tx.Put("bucket2", []byte("key"), []byte("val"), 0); err != nil {
                return err
            }
            return nil
        });
//读操作
    db.View(
        func(tx *nutsdb.Tx) error {
            key := []byte("name_001")
            bucket := "bucket1"
            if e, err := tx.Get(bucket, key); err != nil {
                return err
            } else {
                fmt.Println(string(e.Value))
            }
            prefix:=[]byte("name_")//前缀
            entries,_, _ := tx.PrefixScan(bucket, prefix, 0, 100)//前缀扫描,开始,条数
            //start := []byte("user_0010001");end := []byte("user_0010010");tx.RangeScan(bucket, start, end);//范围扫描
            //entries, err := tx.GetAll(bucket)//全部
            for _, entry := range entries {
                fmt.Println(string(entry.Key), string(entry.Value))
            }
            return nil
        })
//list
bucket := "bucketForList"
key := []byte("myList")
val := []byte("val1")
tx.RPush(bucket, key, val) //队列key的右边入队一个或者多个元素val
tx.LPush(bucket, key, val)
item, _ := tx.LPop(bucket, key);string(item)//左边出队一个并返回删除
item, _ := tx.LPeek(bucket, key);string(item)//左边出队一个返回不删除
item, err := tx.RPop(bucket, key)
item, err := tx.RPeek(bucket, key)
items, _ := tx.LRange(bucket, key, 0, -1);//取出从0开始,-1结束是最后一个
tx.LSet(bucket, key, 0, []byte("val11"));设置位置0的值
size,err := tx.LSize(bucket, key);大小
//set集合
bucket := "bucketForSet"
key := []byte("mySet")
tx.SAdd(bucket, key, []byte("a"), []byte("b"), []byte("c"))
num, err := tx.SCard(bucket, key) 返回集合数量
items, err := tx.SDiffByOneBucket(bucket, key1, key2);集合差集
items, err := tx.SDiffByTwoBuckets(bucket1, key1, bucket2, key2);差集在不同集合中
ok, err := tx.SHasKey(bucket, []byte("mySet"));判断key是否在集合中
ok, err := tx.SIsMember(bucket, []byte("mySet"), []byte("a"));是否在key中
items, err := tx.SMembers(bucket, []byte("mySet")); 返回key中所有集合元素
//Sorted Set 有序集合
tx.ZAdd(bucket, key, 1, []byte("val1"))添加
num, err := tx.ZCard(bucket); 返回个数
nodes, err := tx.ZMembers(bucket);获取所有key


"github.com/boltdb/bolt" //单文件kv存储
    db, _ := bolt.Open("my.db", 0600, nil)
    defer db.Close()
    db.Update(func(tx *bolt.Tx) (err error) {
        if _, err = tx.CreateBucketIfNotExists([]byte("kl")); err != nil {
            return err
        }
        b := tx.Bucket([]byte("kl"))
        err = b.Put([]byte("konglong"), []byte("恐龙"))//写入kv
        if err = tx.DeleteBucket([]byte("MyBucket")); err != nil {
            return err
        }//删除桶
        err = b.Delete([]byte("answer11"))//删除key
        if err != nil {
            return err
        }
        return err
    })
    db.View(func(tx *bolt.Tx) error {
        b := tx.Bucket([]byte("kl"))
        v := b.Get([]byte("konglong"))//读取key
        fmt.Printf("数据是:%s\n", v)
        b.ForEach(func(k, v []byte) error {
            fmt.Printf("key=%s, value=%s\n", k, v)
            return nil
        })
        return nil
    });
文档更新时间: 2021-11-04 19:14   作者:Yoby